版权声明:本文为博主原创文章,可以随意共享转载,注明来源即可 https://blog.csdn.net/qq_37059483/article/details/78018539

通域分析对于图像处理后面涉及到模式识别的内容来说是基础

连通区域(Connected Component)一般是指图像中具有相同像素值且位置相邻的前景像素点组成的图像区域(Region,Blob)。连通区域分析(Connected Component Analysis,Connected Component Labeling)是指将图像中的各个连通区域找出并标记。
连通区域分析是一种在CVPR和图像分析处理的众多应用领域中较为常用和基本的方法。例如:OCR识别中字符分割提取(车牌识别、文本识别、字幕识别等)、视觉跟踪中的运动前景目标分割与提取(行人入侵检测、遗留物体检测、基于视觉的车辆检测与跟踪等)、医学图像处理(感兴趣目标区域提取)、等等。也就是说,在需要将前景目标提取出来以便后续进行处理的应用场景中都能够用到连通区域分析方法,通常连通区域分析处理的对象是一张二值化后的图像。

看了几篇博客总结如下:

其中最主要的参考博客 :404了,so what?  http://blog.csdn.net/icvpr/article (这是该作者的主页)

很多人转载了,给出一个转载的链接:http://blog.csdn.net/shanx_s/article/details/52860896

参考博客2:http://www.cnblogs.com/ronny/p/img_aly_01.html(博主将过程拆分讲解的很清楚)

参考博客3:http://blog.csdn.net/YU_Xianguo/article/details/49743147(两步法的一种加速版)

参考博客4:http://blog.csdn.net/menghuanxiy/article/details/45741027#reply (种子填充法的一种实现)

参考博客5:http://blog.csdn.net/augusdi/article/details/9008921(很快,代码很短,排名很高(第7))

参考博客6:http://blog.csdn.net/xjt2015/article/details/51283387 (种子填充法的实现)

理论充能任务

任务一:

二值图像,顾名思义就是图像的亮度值只有两个状态:黑(0)和白(255)。二值图像在图像分析与识别中有着举足轻重的地位,因为其模式简单,对像素在空间上的关系有着极强的表现力。在实际应用中,很多图像的分析最终都转换为二值图像的分析,比如:医学图像分析、前景检测、字符识别,形状识别。二值化+数学形态学能解决很多计算机识别工程中目标提取的问题。

二值图像分析最重要的方法就是连通区域标记,它是所有二值图像分析的基础,它通过对二值图像中白色像素(目标)的标记,让每个单独的连通区域形成一个被标识的块,进一步的我们就可以获取这些块的轮廓、外接矩形、质心、不变矩等几何参数。

任务二:

在我们讨论连通区域标记的算法之前,我们先要明确什么是连通区域,怎样的像素邻接关系构成连通。在图像中,最小的单位是像素,每个像素周围有8个邻接像素,常见的邻接关系有2种:4邻接与8邻接。4邻接一共4个点,即上下左右,如下左图所示。8邻接的点一共有8个,包括了对角线位置的点,如下右图所示。

                                                                                                  

如果像素点A与B邻接,我们称A与B连通,于是我们不加证明的有如下的结论:

如果A与B连通,B与C连通,则A与C连通。

在视觉上看来,彼此连通的点形成了一个区域,而不连通的点形成了不同的区域。这样的一个所有的点彼此连通点构成的集合,我们称为一个连通区域。

下面这符图中,如果考虑4邻接,则有3个连通区域;如果考虑8邻接,则有2个连通区域。(注:图像是被放大的效果,图像正方形实际只有4个像素)

任务三:

二、连通区域分析的算法
从连通区域的定义可以知道,一个连通区域是由具有相同像素值的相邻像素组成像素集合,因此,我们就可以通过这两个条件在图像中寻找连通区域,对于找到的每个连通区域,我们赋予其一个唯一的标识(Label),以区别其他连通区域。
连通区域分析有基本的算法,也有其改进算法,本文介绍其中的两种常见算法:
1)Two-Pass法;2)Seed-Filling种子填充法;

1)Two-Pass(两遍扫描法)
两遍扫描法,正如其名,指的就是通过扫描两遍图像,就可以将图像中存在的所有连通区域找出并标记。思路:第一遍扫描时赋予每个像素位置一个label,扫描过程中同一个连通区域内的像素集合中可能会被赋予一个或多个不同label,因此需要将这些属于同一个连通区域但具有不同值的label合并,也就是记录它们之间的相等关系;第二遍扫描就是将具有相等关系的equal_labels所标记的像素归为一个连通区域并赋予一个相同的label(通常这个label是equal_labels中的最小值)。

下面给出Two-Pass算法的简单步骤:
(1)第一次扫描:
访问当前像素B(x,y),如果B(x,y) == 1:
a、如果B(x,y)的领域中像素值都为0,则赋予B(x,y)一个新的label:
label += 1, B(x,y) = label;
b、如果B(x,y)的领域中有像素值 > 1的像素Neighbors:
1)将Neighbors中的最小值赋予给B(x,y):
B(x,y) = min{Neighbors} 
2)记录Neighbors中各个值(label)之间的相等关系,即这些值(label)同属同一个连通区域;
 labelSet[i] = { label_m, .., label_n },labelSet[i]中的所有label都属于同一个连通区域(注:这里可以有多种实现方式,只要能够记录这些具有相等关系的label之间的关系即可)
(2)第二次扫描:
访问当前像素B(x,y),如果B(x,y) > 1:
a、找到与label = B(x,y)同属相等关系的一个最小label值,赋予给B(x,y);
完成扫描后,图像中具有相同label值的像素就组成了同一个连通区域。

下面这张图动态地演示了Two-pass算法:

具体实现还是得一步一步的看代码

2)Seed Filling(种子填充法)
种子填充方法来源于计算机图形学,常用于对某个图形进行填充。思路:选取一个前景像素点作为种子,然后根据连通区域的两个基本条件(像素值相同、位置相邻)将与种子相邻的前景像素合并到同一个像素集合中,最后得到的该像素集合则为一个连通区域。

下面给出基于种子填充法的连通区域分析方法:
(1)扫描图像,直到当前像素点B(x,y) == 1:
a、将B(x,y)作为种子(像素位置),并赋予其一个label,然后将该种子相邻的所有前景像素都压入栈中;
b、弹出栈顶像素,赋予其相同的label,然后再将与该栈顶像素相邻的所有前景像素都压入栈中;
c、重复b步骤,直到栈为空;
此时,便找到了图像B中的一个连通区域,该区域内的像素值被标记为label;
(2)重复第(1)步,直到扫描结束;
扫描结束后,就可以得到图像B中所有的连通区域;

下面这张图动态地演示了Seed-Filling算法:

完整测试代码如下:

  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <vector>
  5. #include <map>
  6. #include <stack>
  7. #include <opencv2/core/core.hpp>
  8. #include <opencv2/highgui/highgui.hpp>
  9. #include <opencv2/imgproc/imgproc.hpp>
  10. using namespace std;
  11. using namespace cv;
  12. //------------------------------【两步法新改进版】----------------------------------------------
  13. // 对二值图像进行连通区域标记,从1开始标号
  14. void Two_PassNew( const Mat &bwImg, Mat &labImg )
  15. {
  16. assert( bwImg.type()==CV_8UC1 );
  17. labImg.create( bwImg.size(), CV_32SC1 ); //bwImg.convertTo( labImg, CV_32SC1 );
  18. labImg = Scalar(0);
  19. labImg.setTo( Scalar(1), bwImg );
  20. assert( labImg.isContinuous() );
  21. const int Rows = bwImg.rows - 1, Cols = bwImg.cols - 1;
  22. int label = 1;
  23. vector<int> labelSet;
  24. labelSet.push_back(0);
  25. labelSet.push_back(1);
  26. //the first pass
  27. int *data_prev = (int*)labImg.data; //0-th row : int* data_prev = labImg.ptr<int>(i-1);
  28. int *data_cur = (int*)( labImg.data + labImg.step ); //1-st row : int* data_cur = labImg.ptr<int>(i);
  29. for( int i = 1; i < Rows; i++ )
  30. {
  31. data_cur++;
  32. data_prev++;
  33. for( int j=1; j<Cols; j++, data_cur++, data_prev++ )
  34. {
  35. if( *data_cur!=1 )
  36. continue;
  37. int left = *(data_cur-1);
  38. int up = *data_prev;
  39. int neighborLabels[2];
  40. int cnt = 0;
  41. if( left>1 )
  42. neighborLabels[cnt++] = left;
  43. if( up > 1)
  44. neighborLabels[cnt++] = up;
  45. if( !cnt )
  46. {
  47. labelSet.push_back( ++label );
  48. labelSet[label] = label;
  49. *data_cur = label;
  50. continue;
  51. }
  52. int smallestLabel = neighborLabels[0];
  53. if( cnt==2 && neighborLabels[1]<smallestLabel )
  54. smallestLabel = neighborLabels[1];
  55. *data_cur = smallestLabel;
  56. // 保存最小等价表
  57. for( int k=0; k<cnt; k++ )
  58. {
  59. int tempLabel = neighborLabels[k];
  60. int& oldSmallestLabel = labelSet[tempLabel]; //这里的&不是取地址符号,而是引用符号
  61. if( oldSmallestLabel > smallestLabel )
  62. {
  63. labelSet[oldSmallestLabel] = smallestLabel;
  64. oldSmallestLabel = smallestLabel;
  65. }
  66. else if( oldSmallestLabel<smallestLabel )
  67. labelSet[smallestLabel] = oldSmallestLabel;
  68. }
  69. }
  70. data_cur++;
  71. data_prev++;
  72. }
  73. //更新等价队列表,将最小标号给重复区域
  74. for( size_t i = 2; i < labelSet.size(); i++ )
  75. {
  76. int curLabel = labelSet[i];
  77. int prelabel = labelSet[curLabel];
  78. while( prelabel != curLabel )
  79. {
  80. curLabel = prelabel;
  81. prelabel = labelSet[prelabel];
  82. }
  83. labelSet[i] = curLabel;
  84. }
  85. //second pass
  86. data_cur = (int*)labImg.data;
  87. for( int i = 0; i < Rows; i++ )
  88. {
  89. for( int j = 0; j < bwImg.cols-1; j++, data_cur++)
  90. *data_cur = labelSet[ *data_cur ];
  91. data_cur++;
  92. }
  93. }
  94. //-------------------------------【老版两步法】-------------------------------------------
  95. void Two_PassOld(const cv::Mat& _binImg, cv::Mat& _lableImg)
  96. {
  97. //connected component analysis (4-component)
  98. //use two-pass algorithm
  99. //1. first pass: label each foreground pixel with a label
  100. //2. second pass: visit each labeled pixel and merge neighbor label
  101. //
  102. //foreground pixel: _binImg(x,y) = 1
  103. //background pixel: _binImg(x,y) = 0
  104. if(_binImg.empty() || _binImg.type() != CV_8UC1)
  105. {
  106. return;
  107. }
  108. // 1. first pass
  109. _lableImg.release();
  110. _binImg.convertTo(_lableImg, CV_32SC1 );
  111. int label = 1; // start by 2
  112. std::vector<int> labelSet;
  113. labelSet.push_back(0); //background: 0
  114. labelSet.push_back(1); //foreground: 1
  115. int rows = _binImg.rows - 1;
  116. int cols = _binImg.cols - 1;
  117. for( int i = 1; i < rows; i++)
  118. {
  119. int* data_preRow = _lableImg.ptr<int>(i-1);
  120. int* data_curRow = _lableImg.ptr<int>(i);
  121. for(int j = 1; j < cols; j++)
  122. {
  123. if(data_curRow[j] == 1)
  124. {
  125. std::vector<int> neighborLabels;
  126. neighborLabels.reserve(2); //reserve(n) 预分配n个元素的存储空间
  127. int leftPixel = data_curRow[j-1];
  128. int upPixel = data_preRow[j];
  129. if( leftPixel > 1)
  130. {
  131. neighborLabels.push_back(leftPixel);
  132. }
  133. if( upPixel > 1)
  134. {
  135. neighborLabels.push_back(upPixel);
  136. }
  137. if(neighborLabels.empty())
  138. {
  139. labelSet.push_back(++label); //assign to a new label
  140. data_curRow[j] = label;
  141. labelSet[label] = label;
  142. }
  143. else
  144. {
  145. std::sort(neighborLabels.begin(),neighborLabels.end());
  146. int smallestLabel = neighborLabels[0];
  147. data_curRow[j] = smallestLabel;
  148. //save equivalence
  149. for(size_t k = 1; k < neighborLabels.size();k++)
  150. {
  151. int tempLabel = neighborLabels[k];
  152. int& oldSmallestLabel = labelSet[tempLabel];
  153. if(oldSmallestLabel > smallestLabel)
  154. {
  155. labelSet[oldSmallestLabel] = smallestLabel;
  156. oldSmallestLabel = smallestLabel;
  157. }
  158. else if(oldSmallestLabel < smallestLabel)
  159. {
  160. labelSet[smallestLabel] = oldSmallestLabel;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. //update equivalent labels
  168. //assigned with the smallest label in each equivalent label set
  169. for(size_t i = 2; i < labelSet.size();i++)
  170. {
  171. int curLabel = labelSet[i];
  172. int prelabel = labelSet[curLabel];
  173. while (prelabel != curLabel )
  174. {
  175. curLabel = prelabel;
  176. prelabel = labelSet[prelabel];
  177. }
  178. labelSet[i] = curLabel;
  179. }
  180. //2. second pass
  181. for( int i = 0; i < rows; i++ )
  182. {
  183. int *data = _lableImg.ptr<int>(i);
  184. for(int j = 0; j < cols; j++ )
  185. {
  186. int& pixelLabel = data[j];
  187. pixelLabel = labelSet[pixelLabel];
  188. }
  189. }
  190. }
  191. //---------------------------------【种子填充法老版】-------------------------------
  192. void SeedFillOld(const cv::Mat& binImg, cv::Mat& lableImg) //种子填充法
  193. {
  194. // 4邻接方法
  195. if (binImg.empty() ||
  196. binImg.type() != CV_8UC1)
  197. {
  198. return;
  199. }
  200. lableImg.release();
  201. binImg.convertTo(lableImg, CV_32SC1);
  202. int label = 1;
  203. int rows = binImg.rows - 1;
  204. int cols = binImg.cols - 1;
  205. for (int i = 1; i < rows-1; i++)
  206. {
  207. int* data= lableImg.ptr<int>(i);
  208. for (int j = 1; j < cols-1; j++)
  209. {
  210. if (data[j] == 1)
  211. {
  212. std::stack<std::pair<int,int>> neighborPixels;
  213. neighborPixels.push(std::pair<int,int>(i,j)); // 像素位置: <i,j>
  214. ++label; // 没有重复的团,开始新的标签
  215. while (!neighborPixels.empty())
  216. {
  217. std::pair<int,int> curPixel = neighborPixels.top(); //如果与上一行中一个团有重合区域,则将上一行的那个团的标号赋给它
  218. int curX = curPixel.first;
  219. int curY = curPixel.second;
  220. lableImg.at<int>(curX, curY) = label;
  221. neighborPixels.pop();
  222. if (lableImg.at<int>(curX, curY-1) == 1)
  223. {//左边
  224. neighborPixels.push(std::pair<int,int>(curX, curY-1));
  225. }
  226. if (lableImg.at<int>(curX, curY+1) == 1)
  227. {// 右边
  228. neighborPixels.push(std::pair<int,int>(curX, curY+1));
  229. }
  230. if (lableImg.at<int>(curX-1, curY) == 1)
  231. {// 上边
  232. neighborPixels.push(std::pair<int,int>(curX-1, curY));
  233. }
  234. if (lableImg.at<int>(curX+1, curY) == 1)
  235. {// 下边
  236. neighborPixels.push(std::pair<int,int>(curX+1, curY));
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. //-------------------------------------------【种子填充法新版】---------------------------
  244. void SeedFillNew(const cv::Mat& _binImg, cv::Mat& _lableImg )
  245. {
  246. // connected component analysis(4-component)
  247. // use seed filling algorithm
  248. // 1. begin with a forgeground pixel and push its forground neighbors into a stack;
  249. // 2. pop the pop pixel on the stack and label it with the same label until the stack is empty
  250. //
  251. // forground pixel: _binImg(x,y)=1
  252. // background pixel: _binImg(x,y) = 0
  253. if(_binImg.empty() ||
  254. _binImg.type()!=CV_8UC1)
  255. {
  256. return;
  257. }
  258. _lableImg.release();
  259. _binImg.convertTo(_lableImg,CV_32SC1);
  260. int label = 0; //start by 1
  261. int rows = _binImg.rows;
  262. int cols = _binImg.cols;
  263. Mat mask(rows, cols, CV_8UC1);
  264. mask.setTo(0);
  265. int *lableptr;
  266. for(int i=0; i < rows; i++)
  267. {
  268. int* data = _lableImg.ptr<int>(i);
  269. uchar *masKptr = mask.ptr<uchar>(i);
  270. for(int j = 0; j < cols; j++)
  271. {
  272. if(data[j] == 255&&mask.at<uchar>(i,j)!=1)
  273. {
  274. mask.at<uchar>(i,j)=1;
  275. std::stack<std::pair<int,int>> neighborPixels;
  276. neighborPixels.push(std::pair<int,int>(i,j)); // pixel position: <i,j>
  277. ++label; //begin with a new label
  278. while(!neighborPixels.empty())
  279. {
  280. //get the top pixel on the stack and label it with the same label
  281. std::pair<int,int> curPixel =neighborPixels.top();
  282. int curY = curPixel.first;
  283. int curX = curPixel.second;
  284. _lableImg.at<int>(curY, curX) = label;
  285. //pop the top pixel
  286. neighborPixels.pop();
  287. //push the 4-neighbors(foreground pixels)
  288. if(curX-1 >= 0)
  289. {
  290. if(_lableImg.at<int>(curY,curX-1) == 255&&mask.at<uchar>(curY,curX-1)!=1) //leftpixel
  291. {
  292. neighborPixels.push(std::pair<int,int>(curY,curX-1));
  293. mask.at<uchar>(curY,curX-1)=1;
  294. }
  295. }
  296. if(curX+1 <=cols-1)
  297. {
  298. if(_lableImg.at<int>(curY,curX+1) == 255&&mask.at<uchar>(curY,curX+1)!=1)
  299. // right pixel
  300. {
  301. neighborPixels.push(std::pair<int,int>(curY,curX+1));
  302. mask.at<uchar>(curY,curX+1)=1;
  303. }
  304. }
  305. if(curY-1 >= 0)
  306. {
  307. if(_lableImg.at<int>(curY-1,curX) == 255&&mask.at<uchar>(curY-1,curX)!=1)
  308. // up pixel
  309. {
  310. neighborPixels.push(std::pair<int,int>(curY-1, curX));
  311. mask.at<uchar>(curY-1,curX)=1;
  312. }
  313. }
  314. if(curY+1 <= rows-1)
  315. {
  316. if(_lableImg.at<int>(curY+1,curX) == 255&&mask.at<uchar>(curY+1,curX)!=1)
  317. //down pixel
  318. {
  319. neighborPixels.push(std::pair<int,int>(curY+1,curX));
  320. mask.at<uchar>(curY+1,curX)=1;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. //---------------------------------【颜色标记程序】-----------------------------------
  329. //彩色显示
  330. cv::Scalar GetRandomColor()
  331. {
  332. uchar r = 255 * (rand()/(1.0 + RAND_MAX));
  333. uchar g = 255 * (rand()/(1.0 + RAND_MAX));
  334. uchar b = 255 * (rand()/(1.0 + RAND_MAX));
  335. return cv::Scalar(b,g,r);
  336. }
  337. void LabelColor(const cv::Mat& labelImg, cv::Mat& colorLabelImg)
  338. {
  339. int num = 0;
  340. if (labelImg.empty() ||
  341. labelImg.type() != CV_32SC1)
  342. {
  343. return;
  344. }
  345. std::map<int, cv::Scalar> colors;
  346. int rows = labelImg.rows;
  347. int cols = labelImg.cols;
  348. colorLabelImg.release();
  349. colorLabelImg.create(rows, cols, CV_8UC3);
  350. colorLabelImg = cv::Scalar::all(0);
  351. for (int i = 0; i < rows; i++)
  352. {
  353. const int* data_src = (int*)labelImg.ptr<int>(i);
  354. uchar* data_dst = colorLabelImg.ptr<uchar>(i);
  355. for (int j = 0; j < cols; j++)
  356. {
  357. int pixelValue = data_src[j];
  358. if (pixelValue > 1)
  359. {
  360. if (colors.count(pixelValue) <= 0)
  361. {
  362. colors[pixelValue] = GetRandomColor();
  363. num++;
  364. }
  365. cv::Scalar color = colors[pixelValue];
  366. *data_dst++ = color[0];
  367. *data_dst++ = color[1];
  368. *data_dst++ = color[2];
  369. }
  370. else
  371. {
  372. data_dst++;
  373. data_dst++;
  374. data_dst++;
  375. }
  376. }
  377. }
  378. printf("color num : %d \n", num );
  379. }
  380. //------------------------------------------【测试主程序】-------------------------------------
  381. int main()
  382. {
  383. cv::Mat binImage = cv::imread("ltc2.jpg", 0);
  384. //cv::threshold(binImage, binImage, 50, 1, CV_THRESH_BINARY);
  385. cv::Mat labelImg;
  386. double time;
  387. time= getTickCount();
  388. //对应四种方法,需要哪一种,则调用哪一种
  389. //Two_PassOld(binImage, labelImg);
  390. //Two_PassNew(binImage, labelImg);
  391. //SeedFillOld(binImage, labelImg);
  392. //SeedFillNew(binImage, labelImg);
  393. time = 1000*((double)getTickCount() - time)/getTickFrequency();
  394. cout<<std::fixed<<time<<"ms"<<endl;
  395. //彩色显示
  396. cv::Mat colorLabelImg;
  397. LabelColor(labelImg, colorLabelImg);
  398. cv::imshow("colorImg", colorLabelImg);
  399. //灰度显示
  400. cv::Mat grayImg;
  401. labelImg *= 10;
  402. labelImg.convertTo(grayImg, CV_8UC1);
  403. cv::imshow("labelImg", grayImg);
  404. double minval, maxval;
  405. minMaxLoc(labelImg,&minval,&maxval);
  406. cout<<"minval"<<minval<<endl;
  407. cout<<"maxval"<<maxval<<endl;
  408. cv::waitKey(0);
  409. return 0;
  410. }

上面是一个综合代码示例,有四种方法,但是有些缺陷,表述如下:

老的两步法:速度很慢

新的两步法:速度很快 (Debug下。快了至少有10倍,与图片的大小有关系)

种子填充法无论是新的还是老的都有缺陷

对于图一:

可以看一下几种方法的运行结果:

1.two_passOld

可以很明显的看出新版方法快很多

其他方法得到的图片类似,注意新版种子填充法并不能得到这样的结果,有问题。

使用新版种子填充法则需要注意屏蔽阈值化那一句语句

对于这样的图片

对于这样一张测试图:

侧视图经过matlab 的bwlabel函数测是,在4连通情况下有3087个连通域,8连通下有2471个连通域。

对于新版与旧版的两步法,没啥区别,就是时间上有很大差别

使用老版的种子填充法会出现边界问题,使用新版种子填充法则需要注意屏蔽阈值化那一句语句


好的,这些方法到此就结束了,其中细节我也需要好好弄弄,但是时间有限,希望有大神朋友留言告知
接下来再给出几个连通域相关的代码:
代码一:
  1. #include <stdio.h>
  2. #include <cv.h>
  3. #include <highgui.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <Windows.h>
  7. int main( int argc, char** argv )
  8. {
  9. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
  10. IplImage* src = cvLoadImage("test.jpg", CV_LOAD_IMAGE_GRAYSCALE);
  11. IplImage* dst = cvCreateImage(cvGetSize(src), 8, 3);
  12. CvMemStorage* storage = cvCreateMemStorage(0);
  13. CvSeq* contour = 0;
  14. cvThreshold(src, src,120, 255, CV_THRESH_BINARY); // 二值化
  15. cvNamedWindow("Source", 1);
  16. cvShowImage("Source", src);
  17. // 提取轮廓
  18. clock_t start, finish;
  19. start = clock();
  20. int contour_num = cvFindContours(src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
  21. cvZero(dst); // 清空数组
  22. CvSeq *_contour = contour;
  23. double maxarea = 0;
  24. double minarea = 100;
  25. int m = 0;
  26. for( ; contour != 0; contour = contour->h_next )
  27. {
  28. double tmparea = fabs(cvContourArea(contour));
  29. if(tmparea < minarea)
  30. {
  31. cvSeqRemove(contour, 0); // 删除面积小于设定值的轮廓
  32. continue;
  33. }
  34. CvRect aRect = cvBoundingRect( contour, 0 );
  35. if ((aRect.width/aRect.height)<1)
  36. {
  37. cvSeqRemove(contour, 0); //删除宽高比例小于设定值的轮廓
  38. continue;
  39. }
  40. if(tmparea > maxarea)
  41. {
  42. maxarea = tmparea;
  43. }
  44. m++;
  45. // 创建一个色彩值
  46. CvScalar color = CV_RGB( 0, 255, 255 );
  47. //max_level 绘制轮廓的最大等级。如果等级为0,绘制单独的轮廓。如果为1,绘制轮廓及在其后的相同的级别下轮廓
  48. //如果值为2,所有的轮廓。如果等级为2,绘制所有同级轮廓及所有低一级轮廓,诸此种种
  49. //如果值为负数,函数不绘制同级轮廓,但会升序绘制直到级别为abs(max_level)-1的子轮廓
  50. cvDrawContours(dst, contour, color, color, -1, 1, 8); //绘制外部和内部的轮廓
  51. }
  52. contour = _contour;
  53. int count = 0;
  54. for(; contour != 0; contour = contour->h_next)
  55. {
  56. count++;
  57. double tmparea = fabs(cvContourArea(contour));
  58. if (tmparea == maxarea)
  59. {
  60. CvScalar color = CV_RGB( 255, 0, 0);
  61. cvDrawContours(dst, contour, color, color, -1, 1, 8);
  62. }
  63. }
  64. finish = clock();
  65. double duration;
  66. duration = (double)(finish - start) / CLOCKS_PER_SEC;
  67. printf("The total number of contours is:%d \n", count);
  68. printf("The total time is:%f seconds\n", duration);
  69. cvNamedWindow("Components", 1);
  70. cvShowImage("Components", dst);
  71. cvWaitKey(0);
  72. cvDestroyWindow("Source");
  73. cvReleaseImage(&src);
  74. cvDestroyWindow("Components");
  75. cvReleaseImage(&dst);
  76. return 0;
  77. }
第一张比较简单的测试图,时间如下:

第二张比较复杂的侧视图八连通,时间如下:

速度之快,令人恐怖
代码二:
  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5. using namespace std;
  6. using namespace cv;
  7. typedef struct _Feather
  8. {
  9. int label; // 连通域的label值
  10. int area; // 连通域的面积
  11. Rect boundingbox; // 连通域的外接矩形框
  12. } Feather;
  13. /*
  14. Input:
  15. src: 待检测连通域的二值化图像
  16. Output:
  17. dst: 标记后的图像
  18. featherList: 连通域特征的清单
  19. return:
  20. 连通域数量。
  21. */
  22. int bwLabel(Mat & src, Mat & dst, vector<Feather> & featherList)
  23. {
  24. int rows = src.rows;
  25. int cols = src.cols;
  26. int labelValue = 0;
  27. Point seed, neighbor;
  28. stack<Point> pointStack; // 堆栈
  29. int area = 0; // 用于计算连通域的面积
  30. int leftBoundary = 0; // 连通域的左边界,即外接最小矩形的左边框,横坐标值,依此类推
  31. int rightBoundary = 0;
  32. int topBoundary = 0;
  33. int bottomBoundary = 0;
  34. Rect box; // 外接矩形框
  35. Feather feather;
  36. featherList.clear(); // 清除数组
  37. dst.release();
  38. dst = src.clone();
  39. for( int i = 0; i < rows; i++)
  40. {
  41. uchar *pRow = dst.ptr<uchar>(i);
  42. for( int j = 0; j < cols; j++)
  43. {
  44. if(pRow[j] == 255)
  45. {
  46. area = 0;
  47. labelValue++; // labelValue最大为254,最小为1.
  48. seed = Point(j, i); // Point(横坐标,纵坐标)
  49. dst.at<uchar>(seed) = labelValue;
  50. pointStack.push(seed);
  51. area++;
  52. leftBoundary = seed.x;
  53. rightBoundary = seed.x;
  54. topBoundary = seed.y;
  55. bottomBoundary = seed.y;
  56. while(!pointStack.empty())
  57. {
  58. neighbor = Point(seed.x+1, seed.y);
  59. if((seed.x != (cols-1)) && (dst.at<uchar>(neighbor) == 255))
  60. {
  61. dst.at<uchar>(neighbor) = labelValue;
  62. pointStack.push(neighbor);
  63. area++;
  64. if(rightBoundary < neighbor.x)
  65. rightBoundary = neighbor.x;
  66. }
  67. neighbor = Point(seed.x, seed.y+1);
  68. if((seed.y != (rows-1)) && (dst.at<uchar>(neighbor) == 255))
  69. {
  70. dst.at<uchar>(neighbor) = labelValue;
  71. pointStack.push(neighbor);
  72. area++;
  73. if(bottomBoundary < neighbor.y)
  74. bottomBoundary = neighbor.y;
  75. }
  76. neighbor = Point(seed.x-1, seed.y);
  77. if((seed.x != 0) && (dst.at<uchar>(neighbor) == 255))
  78. {
  79. dst.at<uchar>(neighbor) = labelValue;
  80. pointStack.push(neighbor);
  81. area++;
  82. if(leftBoundary > neighbor.x)
  83. leftBoundary = neighbor.x;
  84. }
  85. neighbor = Point(seed.x, seed.y-1);
  86. if((seed.y != 0) && (dst.at<uchar>(neighbor) == 255))
  87. {
  88. dst.at<uchar>(neighbor) = labelValue;
  89. pointStack.push(neighbor);
  90. area++;
  91. if(topBoundary > neighbor.y)
  92. topBoundary = neighbor.y;
  93. }
  94. seed = pointStack.top();
  95. pointStack.pop();
  96. }
  97. box = Rect(leftBoundary, topBoundary, rightBoundary-leftBoundary, bottomBoundary-topBoundary);
  98. rectangle(src, box, 255);
  99. feather.area = area;
  100. feather.boundingbox = box;
  101. feather.label = labelValue;
  102. featherList.push_back(feather);
  103. }
  104. }
  105. }
  106. return labelValue;
  107. }
  108. int main(int argc, char *argv[])
  109. {
  110. Mat src(imread("ltc2.jpg", 0));
  111. if(src.empty())
  112. exit(-1);
  113. threshold(src, src, 127, 255, THRESH_BINARY); // 二值化图像
  114. vector<Feather> featherList; // 存放连通域特征
  115. Mat dst;
  116. cout << "连通域数量: " << bwLabel(src, dst, featherList) << endl;
  117. // 为了方便观察,可以将label“放大”
  118. for( int i = 0; i < dst.rows; i++)
  119. {
  120. uchar *p = dst.ptr<uchar>(i);
  121. for( int j = 0; j < dst.cols; j++)
  122. {
  123. p[j] = 30*p[j];
  124. }
  125. }
  126. cout << "标号" << "\t" << "面积" << endl;
  127. for(vector<Feather>::iterator it = featherList.begin(); it < featherList.end(); it++)
  128. {
  129. cout << it->label << "\t" << it->area << endl;
  130. rectangle(dst, it->boundingbox, 255);
  131. }
  132. imshow("src", src);
  133. imshow("dst", dst);
  134. waitKey();
  135. destroyAllWindows();
  136. system("pause");
  137. return 0;
  138. }
对于第一张测试图,结果如下
第二张,就跑不出来了,毕竟画外包络太复杂了,哈哈!

由于生病几天了,躺在医院一个星期,不得不感慨,程序员必须好好的照顾自己的身体。

OpenCV二值图像连通域分析相关推荐

  1. OpenCV——二值图像连通域分析

    通域分析对于图像处理后面涉及到模式识别的内容来说是基础 连通区域(Connected Component)一般是指图像中具有相同像素值且位置相邻的前景像素点组成的图像区域(Region,Blob).连 ...

  2. opencv 图像连通域分析

    一.简介 图像的连通域是指图像中具有相同像素值并且位置相邻的像素组成的区域,连通域分析是指在图像中寻找出彼此互相独立的连通域并将其标记出来.提取图像中不同的连通域是图像处理中较为常用的方法,例如在车牌 ...

  3. opencv [c++] 连通域分析connectedComponentsWithStats() 和 connectedComponents()

    1. API相关参数介绍: labels :对原始图中的每一个像素都打上标签,背景为0,连通域打上1,2,3...的标签,同一个连通域的像素打上同样的标签.相当与对每一个像素进行了分类(分割) int ...

  4. 基于连通域字符分割的流程_基于OpenCV及连通域分析进行文本块分割

    上一次通过投影的方式进行了文本块分割,但这种方法有很大的局限性,要求分行清晰.不能有字符跨多行.不能倾斜,而且对噪声比较敏感.还是拿上一回的图片,但是我在上面加了一个比较大的字,得出的结果就有问题了: ...

  5. matlab 连通域分割,基于OpenCV.Net连通域分析进行文本块分割

    上一次通过投影的方式进行了文本块分割,(见 https://www.cnblogs.com/BoyTNT/p/11812323.html )但这种方法有很大的局限性,要求分行清晰.不能有字符跨多行.不 ...

  6. python opencv 连通域_基于OpenCV及连通域分析进行文本块分割

    上一次通过投影的方式进行了文本块分割,但这种方法有很大的局限性,要求分行清晰.不能有字符跨多行.不能倾斜,而且对噪声比较敏感.还是拿上一回的图片,但是我在上面加了一个比较大的字,得出的结果就有问题了: ...

  7. NI Vision:二值图像连通域标记算法

    前面说到,要使用Labwindows + NI Vision(IMAQ Vision)这套商用开发框架来做数图课设.很明显,这套虚拟仪器开发平台由NI Instrument(美国国家仪器公司)开发的. ...

  8. 【OpenCV 4开发详解】图像连通域分析

    本文首发于"小白学视觉"微信公众号,欢迎关注公众号 本文作者为小白,版权归人民邮电出版社发行所有,禁止转载,侵权必究! 经过几个月的努力,小白终于完成了市面上第一本OpenCV 4 ...

  9. cv2.error: opencv(4.4.0)_【OpenCV 4开发详解】图像连通域分析

    本文首发于"小白学视觉"微信公众号,欢迎关注公众号 本文作者为小白,版权归人民邮电出版社发行所有,禁止转载,侵权必究! 经过几个月的努力,小白终于完成了市面上第一本OpenCV 4 ...

最新文章

  1. crypto++使用DES加密(ECB pkcs7padding )
  2. EasyUI中分页的简单使用
  3. AJPFX实例集合嵌套之ArrayList嵌套ArrayList
  4. 换种方式去分页(转)
  5. java 如何发提示_消息提醒-如何实现收到待办给QQ发送提醒?
  6. 关于CNN图像分类的一份综合设计指南
  7. Bootstrap清除/清理浮动
  8. es6 迭代器对象的 return(),throw()
  9. 60-300-022-使用-延迟数据-Flink中allowedLateness详细介绍
  10. C++安全方向(三)3.4 使用哈希列表验证文件的完整性
  11. java 大数 list_Java后台通过Collections获取list集合中最大数,最小数代码
  12. 小学网上报名显示网上服务器错误,网上申请了小学学位 区教育局找不到信息...
  13. 分数化简java_中国MOOC分数——Java
  14. 平衡二叉树(C++实现)
  15. android 图片虚化代码,Android模糊图片技术
  16. C#求学之路MVC《医院管理系统》项目研发文献
  17. Java Logging之JUL系列——Logger Hierarchy
  18. 如何解密Token?
  19. Jetpack:Room配合LiveData/Flow使用优化,Room+Flow使用原理解析。
  20. Presto RBO之broadcast join与partitioned join类型的选择优化

热门文章

  1. html里p标签里面如何让首行缩进两格?
  2. java 汉字转换全拼、首字母拼音
  3. 机器学习 | MATLAB实现BP神经网络newff参数设定(下)
  4. mysql reorg 命令_DB2 runstats、reorgchk、reorg 命令
  5. oceanus-58总体框架理解
  6. 次坐标从0开始_中考热点专题突破精讲精练3: 与坐标系中的有关热点问题
  7. python正负数排序_带有负值的Python sort()问题
  8. 边缘计算简介以及几款边缘计算开源平台
  9. PaddleRec与Milvus深度结合,手把手带你体验工业级推荐系统召回速度
  10. STM8L051F3单片机竟然没有TIM1定时器,却有TIM2,3,4!!!害我调了一天没调出来