R
R
rodion8362019-06-17 16:56:08
Java
rodion836, 2019-06-17 16:56:08

How to overlay image on opencv image (video)?

I am writing a small application: it adds a picture (for example, put on glasses) to the video received from the camera.

void addMask(Mat src,Rect face){

        Mat roi;
        Imgproc.resize(mask,mask,face.size());
       roi= src.submat(face);
                //.copyTo(roi);

        Mat maskInv= new Mat();
        Mat mat1_bg = new Mat();
        Mat mat2_fg = new Mat();
        Mat maskGray=new Mat();

        Core.extractChannel(mask,maskGray,3);
        Core.bitwise_not(maskGray,maskInv);

        ArrayList<Mat> roiChannels = new ArrayList<>(4);
        ArrayList<Mat> roiMask = new ArrayList<>(4);
        roiMask.add(new Mat());
        roiMask.add(new Mat());
        roiMask.add(new Mat());
        roiMask.add(new Mat());

        Core.split(roi,roiChannels);

        Core.bitwise_and(roiChannels.get(0),maskInv,roiMask.get(0));
        Core.bitwise_and(roiChannels.get(1),maskInv,roiMask.get(1));
        Core.bitwise_and(roiChannels.get(2),maskInv,roiMask.get(2));
        Core.bitwise_and(roiChannels.get(3),maskInv,roiMask.get(3));

        Core.merge(roiMask,mat1_bg);

        maskInv.release();
        roi.release();

        ArrayList<Mat> maskChannels = new ArrayList<>(4);
        ArrayList<Mat> maskList = new ArrayList<>(4);
        maskList.add(new Mat());
        maskList.add(new Mat());
        maskList.add(new Mat());
        maskList.add(new Mat());

        Core.split(mask,maskChannels);

        Core.bitwise_and(maskChannels.get(0),maskGray,maskList.get(0));
        Core.bitwise_and(maskChannels.get(1),maskGray,maskList.get(1));
        Core.bitwise_and(maskChannels.get(2),maskGray,maskList.get(2));
        Core.bitwise_and(maskChannels.get(3),maskGray,maskList.get(3));

        Core.merge(maskList,mat2_fg);

        maskGray.release();
        Mat dst = new Mat();
        Core.add(mat1_bg,mat2_fg,dst);
       dst.copyTo(src.submat(face));

       mat1_bg.release();
       mat2_fg.release();
       dst.release();
    }

If applied to a static image, then there is no problem. But if this is a video, then with each frame, the upper image starts to blur and as a result, just a white spot on top of the video. How to fix?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rodion836, 2019-06-18
@rodion836

In general, I found a solution. Since copyTo supports copying by mask, bitwise operations can be dispensed with. We make a mask from the alpha channel (PNG).

void addMask(Mat src, Rect face) {

        Mat mask = new Mat();
        Mat matResize= new Mat();

        Imgproc.resize(imgIn,matResize,face.size());

        Core.extractChannel(matResize, mask, 3);

        matResize.copyTo(src.submat(face),mask);

        mask.release();
        matResize.release();

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question