0

Am trying to create transparent mat but am getting black background image in return so any idea how to do it as i have to overlay this image with some content on camera video feed.

code i tried.

cv::Mat comp = cv::Mat::zeros( currentImage.size(), CV_8UC4 );
comp.setTo(cv::Scalar(0,0,0,0));
imshow( "transparent", comp );   // show black background image.
user889030
  • 4,353
  • 3
  • 48
  • 51
  • 2
    Your alpha channel is all `255`, which is fully opaque. For fully transparent pixels, you need an all `0` alpha channel, so leave out the `comp.setTo()` part. Also, `cv::imshow()` won't render proper transparency. Inspect some debug output (as `PNG` for example) or use some additional tool to inspect the image in memory (for example, there's Image Watch for Visual Studio). – HansHirse Jan 28 '20 at 11:20
  • Isn't `cv::Scalar(0, 0, 0, 255)` encoding an _opaque_ black pixel? Did you try `cv::Scalar(0, 0, 0, 0)` instead? – Scheff's Cat Jan 28 '20 at 11:21
  • @Scheff ya i tried cv::Scalar(0, 0, 0, 0) but same black image – user889030 Jan 28 '20 at 11:28
  • @HansHirse ok i will save image to disk to check , , you right the imshow may not render transparency – user889030 Jan 28 '20 at 11:29
  • 1
    While `cv::Scalar(0, 0, 0, 0)` seems to me the proper encoding for a transparent pixel, please, keep in mind what @HansHirse said about `cv::imshow()`. The proper way to check might be to dump the matrix as image file and to inspect with a suitable image viewer. – Scheff's Cat Jan 28 '20 at 11:30
  • 1
    E.g. using Qt, you could build one on your own with a few lines of code (and add more features for any inspection you need) as I did here: [SO: How to automate isolating line art from the background with code. Is there a way?](https://stackoverflow.com/a/59664016/7478597). ;-) – Scheff's Cat Jan 28 '20 at 11:33
  • 1
    @Scheff thanks :) . ya i dumped image to disk , HansHirse was right issue is in imshow it not render transparency – user889030 Jan 28 '20 at 11:36

1 Answers1

0

thanks to @HansHire and @Scheff we find out that problem is in cv::imshow() it not render transparency so to check Mat for transparency it need to dumped to disk to be checked using system image viewer which render transparency very well

cv::Mat comp = cv::Mat::zeros( currentImage.size(), CV_8UC4 );
comp.setTo(cv::Scalar(0,0,0,0));
// imshow( "transparent", comp );   // show black background image because it cant render transparency .
imwrite( "C:/opencv_dump.png", comp );  // transparent upon opening , thumbnail may look black.
user889030
  • 4,353
  • 3
  • 48
  • 51
  • 1
    You might like to look at this which overlays your image with transparency onto a checkerboard like Photoshop does... https://stackoverflow.com/a/58684791/2836621 – Mark Setchell Jan 28 '20 at 11:49