2

I want to create a separate image using Contours from an image.

I have already seen the answers here and here, but by using them the background becomes black.

But i want to have a transparent background because i have to process those images further where black will create problem.

Question: How can i get a transparent background for the extracted image.

Currently i am using the following code though which i am able to create a separate imge but with black background:

Mat findRect::extractImage( int min_x, int min_y , int rows, int cols , Mat frame, vector<Point> ROI_Poly)
{
    Mat mask = Mat::zeros(frame.rows, frame.cols, CV_8UC1);

    // Fill polygon white
    fillConvexPoly(mask, &ROI_Poly[0], ROI_Poly.size(), 255, 8, 0);                 

    // Create new image for result storage
    Mat imageDest = cvCreateMat(frame.rows, frame.cols, CV_8UC3);

    // Cut out ROI and store it in imageDest
    frame.copyTo(imageDest, mask);  

    // Extracting the ROI only 
    roi = Rect (min_x, min_y, cols, rows);
    Mat detectedSquare;


    if( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= frame.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= frame.rows )
        detectedSquare= imageDest(roi);

    //imshow("extracted image" , detectedSquare);
    return detectedSquare;
}
Community
  • 1
  • 1
user2756695
  • 676
  • 1
  • 7
  • 22
  • you mean like saving it as a png file with transparent background? OpenCV doesn't know transparent "color", but you could name one color value which you interpret as being transparent and/or use masks in your further computations. – Micka Mar 10 '14 at 13:38

1 Answers1

1

So you have already the contour and 3 channel source image.

  1. Now just create single channel image by drawing contour and inverting it, this will reprent your alpha channel.

  2. Suppose you already copied source image to new Mat with black background, just split this image to a Mat array of size 3.

  3. Now you got three primary channel R, G, B, and Alpha.

  4. Now just merge all these to a new Mat.

Done! .

See below code, here I created alpha using thresholding, instead you have to use contour drawing.

Mat src=imread("src.png",1);
Mat dst;
Mat tmp,alpha;

cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,alpha,10,255,THRESH_BINARY);

Mat rgb[3];
split(src,rgb);

Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
merge(rgba,4,dst);

BGR source

BGR source

Alpha for background(0 = full transparency, and 255 = no transparency )

enter image description here

Result with background alpha

Result with background alpha

Haris
  • 13,645
  • 12
  • 90
  • 121
  • i am a bit confused about this alpha channel. The `Mat alpha` will contain either `0` or `255`...isn't it? then how come it will be transparent? – user2756695 Mar 11 '14 at 11:19
  • @Harris: I have updated the code which i am using. I am trying to use your method but i am doing some mistake. Can you suggest me, where should i make changes in my code? – user2756695 Mar 11 '14 at 11:36
  • You can vary alpha from 0 to 255 in the case of 8 bit image, where 0 gives maximum transparency, and as the value increases to 255 image will become more opaque. – Haris Mar 11 '14 at 11:44
  • @user2756695 See updated alpha image hope you will get more clearer. – Haris Mar 11 '14 at 11:53
  • I have a doubt. Actually i want to create the histogram of the extracted image (with transparent BG). The transparent background is becuase of the alpha channel....but that background still have some RGB values ..right?....so those values will contribute in Hue based histogram?...or the pixel where the alpha=0 will be skipped? – user2756695 Mar 11 '14 at 17:03
  • As far as I know cvtColor does'nt handle alpha and your transparent back ground can have any RGB value, even if it zero while converting to HSV the hue can have any value by keeping V and S zero – Haris Mar 11 '14 at 18:36
  • ahh ok..then there is no benefit of using this method. I just want to completly remove that area so that it won't contribute in any kind of further calculations – user2756695 Mar 11 '14 at 19:34