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;
}