Java based OpenCV in Android : Want to overlap an image onto another image only where the mask has non-zero values -


i know question rather basic question, tried several days solve problem, , can't find exact answer question on stackoverflow too.

i have background image, , want overlap foreground image onto it, on regions mask has non-zero values. code wrote below. foreground image appears in regions mask has non-zero values (in upper left quarter of whole image region), in rest of image, background not appear, appears 'black'.

i appreciate if problem. similar code works opencv c language.

the background image (background.bmp) , foreground image (foreground.bmp), being read in without problem (this have verified).


setcontentview(r.layout.activity_main); string pathtobackground               = environment.getexternalstoragedirectory()+"/background.bmp"; string pathtoforeground               = environment.getexternalstoragedirectory()+"/foreground.bmp";  mat foreground;    mat background;   background = imread(pathtobackground);  // read in background image foreground = imread(pathtoforeground); // read in foreground image  bitmap back_bitmap = bitmapfactory.decodefile(path+"/background.bmp");  utils.bitmaptomat(back_bitmap, background);  // convert bitmap mat   mat mask2 =   new mat( new size(foreground.cols(), foreground.rows() ), cvtype.cv_8uc1);  mask2.setto( new scalar( 0 ) );    // set mask 0   size sizemask = mask2.size();      (int i=0;i<sizemask.height/2;i++ )  // making upper left quarter          (int j=0;j<sizemask.width/2;j++ )  //of whole mask image                                                             mask2.put(i, j, 10);              // non-zero     foreground.copyto(background, mask2); // copy foreground image on                                           // background image                                          // mask2 has non-zero value                                          // --> not working expected     utils.mattobitmap(background, back_bitmap);  // convert mat bitmap    imageview v = (imageview)findviewbyid(r.id.imageview);    v.setimagebitmap(back_bitmap); // foreground image appears in                                   // upper left region. remaining region                                   // black.                                   // remaining region should show                                   // background image.  

thanks comments of 'rick m.', solve problem following code (though not neat). made empty bitmap('back_bitmap') instead of putting initial image in it. result shown in here. before not see background image.

    setcontentview(r.layout.activity_main);      string pathtobackground  =                  environment.getexternalstoragedirectory()+"/background.bmp";     string pathtoforeground  =                  environment.getexternalstoragedirectory()+"/foreground.bmp";      mat foreground;     mat background;     background = imread(pathtobackground);     foreground = imread(pathtoforeground);      bitmap back_bitmap      = bitmap.createbitmap(foreground.cols(), foreground.rows(),        bitmap.config.argb_8888);  // make empty bitmap     mat mask2     = new mat( new size( foreground.cols(), foreground.rows() ),                     cvtype.cv_8uc1 );     mask2.setto( new scalar( 0 ) );      size sizemask = mask2.size();     double[] data;        (int i=0;i<sizemask.height;i++ )          (int j=0;j<sizemask.width;j++ ) {              data = foreground.get(i, j);              if (data[0] + data[1] + data[2] > 0)                  mask2.put(i, j, 10);          }     foreground.copyto(background, mask2);     utils.mattobitmap(background, back_bitmap);     imageview v = (imageview)findviewbyid(r.id.imageview);     v.setimagebitmap(back_bitmap);     }    } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -