Search code examples
opencvjavacv

How to cvCopy with background transparent?


Using cvCopy I get the object without background (in webcam stream). I want to make a transparent with removed background, because I need to play another video in the background. How can Ido this?


Solution

  • You can follow these steps:

    1.) You said you have the object without the background. Hence it will be straightforward to achieve a binary mat for the object (Convert the image into Grayscale and then threshold it). Let's call this Binary mask as objectBinMask

    2.) Suppose each frame of your video is called vidFrame, in that case you can use the object mask to paste the object on the video frame, as so:

    vidFrame.copyTo(outputFrame,objectBinMask);

    here outputFrame should have the required object on each frame of the video. copyTo is a method available in C++, you can equivalently use cvCopy for your C code.