openCV: adding transparency to IplImage

后端 未结 2 522
北恋
北恋 2021-01-06 16:56

I have a 3-channel IplImage. I would like to create a 4-channel image and set the alpha channel for it to a value less than 1.0 to make it semi-transparent.

First I

2条回答
  •  天涯浪人
    2021-01-06 17:20

    //This code help to make a transparency image But it take src image as one //single color background see![Removing background and added black background color  ][1]
    Mat dst;//(src.rows,src.cols,CV_8UC4);
    Mat tmp,alpha;
    
    cvtColor(src,tmp,CV_BGR2GRAY);
    threshold(tmp,alpha,0,255,THRESH_BINARY);
    
    Mat rgb[3];
    split(src,rgb);
    
    Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
    merge(rgba,4,dst);
    imwrite("dst.png",dst);
    //dst is transparency image see here![output image as transparency image][2]
    
    
      [1]: http://i.stack.imgur.com/9THqs.png
      [2]: http://i.stack.imgur.com/mpmgy.png
    

提交回复
热议问题