Create transparent color in OpenCV

I have a basic png file with two colors in it, green and magenta. What I want to do is take all the magenta pixels and make them transparent so that I can combine the image into another image.

An example would be if I have a two-dimensional character image file with a magenta background. I would remove all the magenta background in the background so that it is transparent. From there, I just take the character’s image and add it as a layer to another image so that it looks like the character was placed on Wednesday.

Thanks in advance.

+5
source share
2 answers

To the code that I would use

First upload the image:

IplImage *myImage;
myImage = cvLoadImage("/path/of/your/image.jpg");

, , , . ( , OpenCV BGR, 125,0,0 ( ), 255,127,127 - - . , , , ...

cvInRangeS(image, 
           cvScalar(125.0, 0.0, 0.0), 
           cvScalar(255.0, 127.0, 127.0), 
           mask
           );

, ( , )

cvNot(mask, mask);

,

IplImage *myImageWithTransparency; //You may need to initialize it before
cvCopy(myImage,myImageWithTransparency,mask);

, ,

. OpenCVDocumentation.

,

+9

All Articles