Change background of ellipse mask into transparent

asked 2015-04-01 02:41:28 -0500

orochi gravatar image

updated 2015-04-07 02:37:01 -0500

Hi, based on answer from this question, i try to make an ellipse mask from my real time face detection and then change the background of the ellipse into transparent.

Using an answer suggested in this question i manage to change the background into transparent, but color of the main image looks weird. here the result: image description and this is the snipped of my code

public boolean userFace(Mat matFace)
{           

    // init new matrices
    Mat dst = new Mat(matFace.rows(), matFace.cols(), CvType.CV_8UC4);
    Mat tmp = new Mat(matFace.rows(), matFace.cols(), CvType.CV_8UC4);
    Mat alpha = new Mat(matFace.rows(), matFace.cols(), CvType.CV_8UC4);

    // convert image to grayscale
    Imgproc.cvtColor(matFace, tmp, Imgproc.COLOR_BGR2GRAY);

    // threshold the image to create alpha channel with complete transparency in black background region and zero transparency in foreground object region.
    Imgproc.threshold(tmp, alpha, 100, 255, Imgproc.THRESH_BINARY);

    // split the original image into three single channel.
    List<Mat> rgb = new ArrayList<Mat>(3);
    Core.split(matFace, rgb);

    // Create the final result by merging three single channel and alpha(BGRA order)
    List<Mat> rgba = new ArrayList<Mat>(4);
    rgba.add(rgb.get(0));
    rgba.add(rgb.get(1));
    rgba.add(rgb.get(2));
    rgba.add(alpha);
    Core.merge(rgba, dst);

    int width = dst.width(), height = dst.height(), channels = dst.channels() ;  
    byte[] sourcePixels = new byte[width * height * channels];  
    dst.get(0, 0, sourcePixels);  
    // create new image and get reference to backing data  
    faceimage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);  

    final byte[] targetPixels = ((DataBufferByte) faceimage.getRaster().getDataBuffer()).getData();  
    System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);

    return true;
}

Please, any tutorial, article or something to point me into the solution, i really appreciate it

Btw, i'm using java and opencv 2.4.9

edit retag flag offensive close merge delete

Comments

You can refer the answer here might be helpful.

Haris gravatar imageHaris ( 2015-04-01 05:59:47 -0500 )edit

woah thanks a lot!!! its look like helpful, i'll try to do like the answer suggested

orochi gravatar imageorochi ( 2015-04-01 08:24:04 -0500 )edit

hi @Haris, i've manage to make the background transparent using your solution. but there is another problem, color of the main image is look weird, i update my question for reference.

orochi gravatar imageorochi ( 2015-04-07 02:31:35 -0500 )edit
1

Might be the channels are interchanged, probably blue and red.

Haris gravatar imageHaris ( 2015-04-07 03:11:07 -0500 )edit

Hi @Haris, thanks for the comment, i've try to change the order of rgba.add , but it doesnt do anything, can you point me to article or something that can give some idea to solve this problem. i've try to search it, but cant find something relevant with this problem yet.

orochi gravatar imageorochi ( 2015-04-07 09:48:13 -0500 )edit

Try, ABGR, BGRA, ARGB, RGBA, means change the channel order, as OpenCV uses BGR space and some times it make confusion. Also first make sure the BGR channels are in order by excluding alpha.

Haris gravatar imageHaris ( 2015-04-07 14:23:22 -0500 )edit