原理:
Two-Pass方法检测连通域的原理可参见这篇博客:http://blog.csdn.net/lichengyu/article/details/13986521。
参考下面动图,一目了然。
代码:
代码中标记图的数据类型要注意,如果first pass中标记数多于255,就不要用uchar类型,我直接设置为int类型。
1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include
结果:
使用OpenCV的logo为素材图,如下:
(1)转为灰度图然后阈值化
(2)寻找连通域
(3)不同连通区域不同颜色显示
封装后的代码见我的码云code:https://gitee.com/rxdj/twoPassMethod.git