首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用轮廓和图像创建单独的图像?

使用轮廓和图像创建单独的图像?
EN

Stack Overflow用户
提问于 2014-03-10 13:26:16
回答 1查看 1.3K关注 0票数 2

我想要创建一个单独的图像使用轮廓从图像。

我已经看到了答案herehere,但是使用它们,背景变成了黑色。

但我想要一个透明的背景,因为我必须进一步处理这些图像,在那里黑色将造成问题。

问题:如何为提取的图像获得透明的背景。

目前,我正在使用以下代码,但我可以创建一个单独的imge,但背景是黑色的:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-10 16:34:46

所以你已经有了轮廓和3通道的源图像。

  1. 现在只需创建单通道图像通过绘制等高线和反转它,这将代表你的阿尔法通道。
  2. 假设您已经将源图像复制到具有黑色背景的新Mat中,只需将该图像分割为大小为3的Mat数组即可。
  3. 现在你有三个主频道R,G,B和Alpha。
  4. 现在把所有这些都合并到一个新的Mat。

完成了!。

参见下面的代码,这里我使用阈值创建了alpha,取而代之的是使用等高线绘图。

代码语言:javascript
复制
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 =无透明度)

具有背景α的结果

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22301430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档