好像没有什么现实意义,单纯觉得好玩。。。。。
填充前的图片:
第一步取阈值,这阈值取的大小直接影响填充后的效果,大家可以不断的尝试。
取阈值后的效果图如下:
第三步就是用findContours与drawContours进行填充啦
最终效果图:
其实效果并不是很好,有时间再改进吧
完整代码如下:
void findDrawContours(){ Mat srcImg = imread("MiTest.jpg", 0);//二值图模式载入 imshow("原始图", srcImg); Mat dstImg = Mat::zeros(srcImg.rows, srcImg.cols, CV_8UC3); srcImg = srcImg > 200;//srcImage取大于阈值119的那部分 imshow("取阈值后的原始图", srcImg);//黑板画效果 //定义轮廓和层次结构 vector<vector<Point>>contours; vector<Vec4i>hier; findContours(srcImg, contours, hier, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); int index = 0; for (; index >= 0; index = hier[index][0]) { Scalar color(rand() & 255, rand() & 255, rand() & 255); drawContours(dstImg, contours, index, color, CV_FILLED, 8, hier); } imshow("轮廓", dstImg); waitKey(0); }