Getting masked area to be transparent ?

asked 2015-02-10 01:49:08 -0600

Rajind gravatar image

So far i have managed to use masks and get the second image from the first. But what i want is the black area in second image to be transparent (i.e the output i an trying to get is the third image) Here is the code so far. Please advice me on this.

    //imwrite parameters
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);

//reading image to be masked
image = imread(main_img, -1);

//CV_LOAD_IMAGE_COLOR

namedWindow("output", WINDOW_NORMAL);
//imshow("output", image);

//Creating mask image with same size as original image
Mat mask(image.rows, image.cols, CV_8UC1, Scalar(0));


// Create Polygon from vertices
ROI_Vertices.push_back(Point2f(float(3112),float(58)));
ROI_Vertices.push_back(Point2f(float(3515),float(58)));
ROI_Vertices.push_back(Point2f(float(3515),float(1332)));
ROI_Vertices.push_back(Point2f(float(3112),float(958)));

approxPolyDP(ROI_Vertices, ROI_Poly, 1, true);

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

//imshow("output", mask);

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

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

imwrite("masked.jpeg", imageDest, compression_params);
imshow("output", imageDest);

cvWaitKey(0);

image description

image description

image description

edit retag flag offensive close merge delete

Comments

firstly, you need 5 points for this mask that you want to create, look below:

|-----------------------------------------------|
| (pt5)                          (pt4) |        |
|                                      |        |
|                                      |        |
|                                      |        |
|                                      |        |
|                               (pt3)   \       |
|                                        \      |
|                                          \    |
|                                            \  |
| (pt1)                                (pt2)   \|
|-----------------------------------------------|

Moreover, the points need to be in the order that is shown above.

theodore gravatar imagetheodore ( 2015-02-10 21:20:39 -0600 )edit

The following code should work then:

Point pt1 = Point(x1, y1);
Point pt2 = Point(x2, y2);
Point pt3 = Point(x3, y3);
Point pt4 = Point(x4, y4);
Point pt5 = Point(x5, y5);

Point pts[] = {pt1, pt2, pt3, pt4, pt5, pt1};

int number_of_points = 5;
Scalar color = Scalar(255);
fillConvexPoly(mask, pts , number_of_points, color);
theodore gravatar imagetheodore ( 2015-02-10 21:27:40 -0600 )edit