I'm trying to merge a png with some transparency on a background:
Mat frg = Highgui.imread( "path/to/foreground/image.png" ), -1 );
Mat bkg = Highgui.imread( "path/to/background/image.png" ), -1 );
// Create mask from foreground.
Mat mask = new Mat( frg.width(), frg.height(), 24 );
double f[] = { 1, 1, 1, 0 };
double e[] = { 0, 0, 0, 0 };
for ( int y = 0; y < ( int )( frg.rows() ); ++y ) {
for ( int x = 0; x < ( int )( frg.cols() ); ++x ) {
double info[] = frg.get( y, x );
if ( info[3] > 0 ) {
mask.put( y, x, e );
} else {
mask.put( y, x, f );
}
}
}
// Copy foreground onto background using mask.
frg.copyTo( bkg, mask );
Highgui.imwrite( "path/to/result/image.png", bkg );
The resulting image is a kind of ghost showing the background image repeated various times and the foreground one in a corrupted way.
Any clues?