我想要创建一个单独的图像使用轮廓从图像。
我已经看到了答案here和here,,但是使用它们,背景变成了黑色。
但我想要一个透明的背景,因为我必须进一步处理这些图像,在那里黑色将造成问题。
问题:如何为提取的图像获得透明的背景。
目前,我正在使用以下代码,但我可以创建一个单独的imge,但背景是黑色的:
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;
}发布于 2014-03-10 16:34:46
所以你已经有了轮廓和3通道的源图像。
完成了!。
参见下面的代码,这里我使用阈值创建了alpha,取而代之的是使用等高线绘图。
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源

阿尔法表示背景(0=完全透明,255 =无透明度)

具有背景α的结果

https://stackoverflow.com/questions/22301430
复制相似问题