当前位置:   article > 正文

C++实现查找连通域_c++连通区域算法

c++连通区域算法

目录

一、概述

1.1、四连通域算法

1.2、八连通域算法

1.3、种子填充法

二、代码


一、概述

图像处理中,查找连通域的算法是图像分割的重要方法之一。它能够将一幅图像分成若干个不重叠的区域,每个区域内部像素具有相似的性质,而不同区域之间的像素性质有明显的差异。在实际应用中,查找连通域的算法被广泛应用于数字图像处理、计算机视觉、医学影像分析等领域。

1.1、四连通域算法

四连通域算法是一种基于4邻域搜索的连通域查找算法。该算法认为每个像素与其上下左右四个相邻像素属于同一个连通域。具体步骤如下:

  1. 初始化:选择一个起始点作为种子点,将其标记为已访问;
  2. 搜索:从当前种子点出发,依次搜索其4邻域内的像素点,若该像素点未被访问过,则将其标记为已访问,并将其加入当前连通域;
  3. 更新:将当前连通域内的所有像素点的标签值更新为与种子点相同的标签值;
  4. 重复步骤2和3,直到所有像素点都被访问过。

四连通域算法简单直观,但容易产生过分割的问题。为了解决这个问题,可以采用八连通域算法。

1.2、八连通域算法

八连通域算法是一种基于8邻域搜索的连通域查找算法。该算法认为每个像素与其上下左右以及四个对角线方向上的相邻像素属于同一个连通域。具体步骤与四连通域算法类似,只是在搜索过程中需要考虑更多的相邻像素。

八连通域算法相对于四连通域算法来说,能够更好地避免过分割的问题,但计算量较大。因此,在实际应用中需要根据具体情况选择合适的算法。

1.3、种子填充法

种子填充法是一种基于种子点的连通域查找算法。它首先选择一个或多个种子点作为起始点,然后根据一定的规则(如颜色相似性)向周围扩展,直到所有满足条件的像素都被加入同一连通域。具体步骤如下:

  1. 初始化:选择一个或多个种子点作为起始点,将其标记为已访问;
  2. 搜索:从当前种子点出发,依次搜索其8邻域内的像素点,若该像素点未被访问过且满足条件(如颜色相似),则将其标记为已访问,并将其加入当前连通域;
  3. 更新:将当前连通域内的所有像素点的标签值更新为与种子点相同的标签值;
  4. 重复步骤2和3,直到所有像素点都被访问过。

种子填充法具有较强的灵活性和适应性,可以根据实际需求选择不同的起始点和扩展规则。但容易受到噪声的影响,因此在实际应用中需要进行预处理和后处理。

综上所述,查找连通域的算法是图像处理中的重要方法之一。不同的算法适用于不同的场景和需求,需要根据具体情况进行选择和应用。

二、代码

以下代码依赖opencv读取图片,仅此而已。代码中算法是基于种子填充算法实现,其实还有形成编码法,在不同上下文环境,算法性能不同。例如:图片连通域数量巨大,每个连通区域面积很小,可能形成编码法效率更高。这里把原图丢出来:

以下是完整代码:

  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:src: 待检测连通域的二值化图像
  15. Output:dst: 标记后的图像 featherList: 连通域特征的清单
  16. return:
  17. 连通域数量。
  18. */
  19. int bwLabel(Mat& src, Mat& dst, vector<Feather>& featherList)
  20. {
  21. int rows = src.rows;
  22. int cols = src.cols;
  23. int labelValue = 0;
  24. Point seed, neighbor;
  25. stack<Point> pointStack; // 堆栈
  26. int area = 0; // 用于计算连通域的面积
  27. int leftBoundary = 0; // 连通域的左边界,即外接最小矩形的左边框,横坐标值,依此类推
  28. int rightBoundary = 0;
  29. int topBoundary = 0;
  30. int bottomBoundary = 0;
  31. Rect box; // 外接矩形框
  32. Feather feather;
  33. featherList.clear(); // 清除数组
  34. dst.release();
  35. dst = src.clone();
  36. for (int i = 0; i < rows; i++)
  37. {
  38. uchar* pRow = dst.ptr<uchar>(i);
  39. for (int j = 0; j < cols; j++)
  40. {
  41. //【注:一旦连通域赋值为1;遍历当前行的下一行时候,就会自动跳过下面if判断】
  42. if (pRow[j] == 255)
  43. {
  44. area = 0;
  45. labelValue++; // labelValue最大为254,最小为1.
  46. seed = Point(j, i); //(731,49) // Point(横坐标,纵坐标)(x,y)
  47. //int p = dst.ptr<uchar>(i)[j];
  48. dst.at<uchar>(seed) = labelValue;//1
  49. pointStack.push(seed);//坐标压栈
  50. area++;
  51. leftBoundary = seed.x;//731
  52. rightBoundary = seed.x;//731
  53. topBoundary = seed.y;//49
  54. bottomBoundary = seed.y;//49
  55. while (!pointStack.empty())
  56. {
  57. //<1>右邻像素点
  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;//【注:】赋值便签1,保证不走回头路和分类连通域
  62. pointStack.push(neighbor);
  63. area++;
  64. if (rightBoundary < neighbor.x)//更新边界
  65. rightBoundary = neighbor.x;
  66. }
  67. //<2>下邻像素点
  68. neighbor = Point(seed.x, seed.y + 1);
  69. if ((seed.y != (rows - 1)) && (dst.at<uchar>(neighbor) == 255))
  70. {
  71. dst.at<uchar>(neighbor) = labelValue;
  72. pointStack.push(neighbor);
  73. area++;
  74. if (bottomBoundary < neighbor.y)
  75. bottomBoundary = neighbor.y;
  76. }
  77. //<3>左邻像素点
  78. neighbor = Point(seed.x - 1, seed.y);
  79. //int testx = seed.x;
  80. //int testp = dst.at<uchar>(neighbor);
  81. if ((seed.x != 0) && (dst.at<uchar>(neighbor) == 255))
  82. {
  83. dst.at<uchar>(neighbor) = labelValue;
  84. pointStack.push(neighbor);
  85. area++;
  86. if (leftBoundary > neighbor.x)
  87. leftBoundary = neighbor.x;
  88. }
  89. //<4>上邻边界点
  90. neighbor = Point(seed.x, seed.y - 1);
  91. if ((seed.y != 0) && (dst.at<uchar>(neighbor) == 255))
  92. {
  93. dst.at<uchar>(neighbor) = labelValue;
  94. pointStack.push(neighbor);
  95. area++;
  96. if (topBoundary > neighbor.y)
  97. topBoundary = neighbor.y;
  98. }
  99. seed = pointStack.top();
  100. pointStack.pop();
  101. }
  102. box = Rect(leftBoundary, topBoundary, rightBoundary - leftBoundary, bottomBoundary - topBoundary);
  103. rectangle(src, box, 255);
  104. feather.area = area;
  105. feather.boundingbox = box;
  106. feather.label = labelValue;
  107. featherList.push_back(feather);
  108. }
  109. }
  110. }
  111. return labelValue;
  112. }
  113. int main(int argc, char* argv[])
  114. {
  115. Mat src(imread("d:/Data/123.png", 0));
  116. if (src.empty())
  117. exit(-1);
  118. threshold(src, src, 127, 255, THRESH_BINARY); // 二值化图像
  119. vector<Feather> featherList; // 存放连通域特征
  120. Mat dst;
  121. cout << "连通域数量: " << bwLabel(src, dst, featherList) << endl;
  122. // 为了方便观察,可以将label“放大”
  123. for (int i = 0; i < dst.rows; i++)
  124. {
  125. uchar* p = dst.ptr<uchar>(i);
  126. for (int j = 0; j < dst.cols; j++)
  127. {
  128. p[j] = 30 * p[j];
  129. }
  130. }
  131. cout << "标号" << "\t" << "面积" << endl;
  132. for (vector<Feather>::iterator it = featherList.begin(); it < featherList.end(); it++)
  133. {
  134. cout << it->label << "\t" << it->area << endl;
  135. rectangle(dst, it->boundingbox, 255);
  136. }
  137. cv::namedWindow("dst");
  138. imshow("src", src);
  139. imshow("dst", dst);
  140. waitKey();
  141. destroyAllWindows();
  142. system("pause");
  143. return 0;
  144. }

执行之后的效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/407229
推荐阅读
相关标签
  

闽ICP备14008679号