使用特定颜色opencv填充图像中的形状

时间:2016-01-01 05:00:37

标签: c++ c opencv image-processing image-segmentation

我有这个图片

enter image description here

我想用白色填充多边形。我试过fillpoly但是无法让它工作。有任何想法吗?我在c ++中使用opencv 3.0。

2 个答案:

答案 0 :(得分:1)

尝试下面的代码用白色填充封闭的对象。

 cv::Mat edgesIn; 
 cv::Mat edgesNeg =temp.clone();
 //  imshow( "edgesNeg", edgesNeg );
 cv::floodFill(edgesNeg, cv::Point(0,0), CV_RGB(255,255,255));
 imshow( "edgesNeg", edgesNeg );
 bitwise_not(edgesNeg, edgesNeg);
 filledEdgesOut = (edgesNeg | temp);
 imshow("Filled region",filledEdgesOut);

答案 1 :(得分:0)

这是一个有趣的问题,我想我会在命令行中使用ImageMagick尝试它,但你可以很容易地将技术适应OpenCV。我怀疑它可能类似于@ Arjun的代码,但我无法轻易阅读......

我将分三步完成,每一步都建在最后一步,但实际上你最后只需要最后的一行命令。

首先,从左上角开始用黄色填充图像:

convert shapes.png -fill yellow -draw 'color 0,0 floodfill' result.png

enter image description here

现在将填充颜色设置为白色并使用白色填充颜色覆盖所有黑色区域:

convert shapes.png -fill yellow -draw 'color 0,0 floodfill' -fill white -opaque black result.png

enter image description here

现在将填充颜色设置为黑色,并用黑色填充颜色覆盖黄色区域:

convert shapes.png -fill yellow -draw 'color 0,0 floodfill' -fill white -opaque black -fill black -opaque yellow result.png

enter image description here