Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Change background of ellipse mask into transparent

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.

i have read the documentation and from it i understand my mask matrix must be type CV_8UC4 and scalar(255,255,255,0). But when i do bitwise_and operation, i got an error, because my first matrix has different type with my mask matrix. My first matrix which is the video capture from my webcam has CV_8UC3 type. But if i change my first matrix type into CV_8UC4, i got an unknown exception. So, What should i do to make the background of my ellipse mask transparent? 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

Change background of ellipse mask into transparent

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 have read the documentation and from it i understand my mask matrix must be type CV_8UC4 and scalar(255,255,255,0). But when i do bitwise_and operation, i got an error, because my first matrix has different type with my mask matrix. My first matrix which manage to change the background into transparent, but color of the main image looks weird. here the result: image description and this is the video capture from my webcam has CV_8UC3 type. But if i change my first matrix type into CV_8UC4, i got an unknown exception. So, What should i do to make the background snipped of my ellipse mask transparent? 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