0

I am using OpenCV 's implementation of GrabCut in order to remove the background from images. But for now the new background is black. Is there any way to make it transparent? For now this part of the code looks like this:

Mat binMask( image.size(), CV_8UC1 );
binMask = mask & 1;
image.copyTo( result, binMask );

Can I somehow fill the binMask with transparent pixels? I've read some tutorials for overlaying images, but I don't need a transparent image in front of my picture but behind. I hope someone could help. Many thanks!

nena
  • 147
  • 1
  • 3
  • 11

1 Answers1

3

Since you are using a 8UC1 image type, it's not possible to have transparent pixels. These are allowed only if you have an alpha channel, with alpha set to 0: you must use a 4-channel image (3 for colors, 1 for alpha channel). The alpha channel is supported in file formats such as PNG, but not in JPG.

In case of masking, you don't need by the way the usage of transparent pixels, since the black ones actually correspond to 0 and they don't influence the result when you're blending two images (addWeighted for example, or also in case of bitwise_or operation).

madduci
  • 2,635
  • 1
  • 32
  • 51
  • I can't consider all the black pixels as a part of the background, because some of them may be part of the foreground. – nena Jul 10 '14 at 07:56
  • If you use a contours extraction method, you just have to define the extracted parts as ROI and fill the contours with white pixels. In this case, you will have the complete foreground. Refer to this answer http://stackoverflow.com/a/16877055/2369389 – madduci Jul 10 '14 at 08:01
  • I use GrabCut to find the background. Isn't there any way to reuse the results? Or I have to make more calculations to find the contours? – nena Jul 18 '14 at 08:17
  • If you have the full foreground extracted, (with absolute difference, for example), still you need to define contours and fill the area – madduci Jul 22 '14 at 11:01