当前位置:   article > 正文

OPENCV二值化图像内孔洞填充/小区域去除_消除二值图像中面积较小的离散点

消除二值图像中面积较小的离散点

来源:http://lib.csdn.net/article/opencv/28353

        原作者:robberjohn  博客已删除了,源码下载链接在

         http://download.csdn.net/download/robberjohn/8474913 

        http://blog.csdn.net/robberjohn/article/details/44081571


对于二值化图像,去除孔洞时采用的方法实际上与去除小区域相同,因此完全可以用同一个函数进行。

这两个功能可以采取区域生长法来实现。须注意,去除小区域时为保存有用信息,可采用8邻域探测,去除孔洞时则4邻域即可,否则容易泄露,出现靠边缘的孔洞未去除的情况。

效果(区域面积阈值为700): 

原图像:



小面积区域去除:



孔洞填充结果:



源码

  1. #include <cv.h>
  2. #include <highgui.h>
  3. #include <opencv2/imgproc/imgproc.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <iostream>
  6. #include <vector>
  7. using namespace cv;
  8. using namespace std;
  9. void RemoveSmallRegion(Mat& Src, Mat& Dst, int AreaLimit=50, int CheckMode=1, int NeihborMode=0);
  10. int main()
  11. {
  12. double t = (double)getTickCount();
  13. char* imagePath = "E:\\SVM\\局部.jpg";
  14. char* OutPath = "E:\\SVM\\局部_去除孔洞.jpg";
  15. Mat Src = imread(imagePath, CV_LOAD_IMAGE_GRAYSCALE);
  16. Mat Dst = Mat::zeros(Src.size(), CV_8UC1);
  17. //二值化处理
  18. for(int i = 0; i < Src.rows; ++i)
  19. {
  20. uchar* iData = Src.ptr<uchar>(i);
  21. for(int j = 0; j < Src.cols; ++j)
  22. {
  23. if(iData[j] == 0 || iData[j]==255) continue;
  24. else if (iData[j] < 10)
  25. {
  26. iData[j] = 0;
  27. //cout<<'#';
  28. }
  29. else if (iData[j] > 10)
  30. {
  31. iData[j] = 255;
  32. //cout<<'!';
  33. }
  34. }
  35. }
  36. cout<<"Image Binary processed."<<endl;
  37. RemoveSmallRegion(Src, Dst, 20, 1, 1);
  38. RemoveSmallRegion(Dst, Dst, 20, 0, 0);
  39. cout<<"Done!"<<endl;
  40. imwrite(OutPath, Dst);
  41. t = ((double)getTickCount() - t)/getTickFrequency();
  42. cout<<"Time cost: "<<t<<" sec."<<endl;
  43. return 0;
  44. }
  45. //CheckMode: 0代表去除黑区域,1代表去除白区域; NeihborMode:0代表4邻域,1代表8邻域;
  46. void RemoveSmallRegion(Mat& Src, Mat& Dst, int AreaLimit, int CheckMode, int NeihborMode)
  47. {
  48. int RemoveCount=0; //记录除去的个数
  49. //记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查
  50. Mat Pointlabel = Mat::zeros( Src.size(), CV_8UC1 );
  51. if(CheckMode==1)
  52. {
  53. cout<<"Mode: 去除小区域. ";
  54. for(int i = 0; i < Src.rows; ++i)
  55. {
  56. uchar* iData = Src.ptr<uchar>(i);
  57. uchar* iLabel = Pointlabel.ptr<uchar>(i);
  58. for(int j = 0; j < Src.cols; ++j)
  59. {
  60. if (iData[j] < 10)
  61. {
  62. iLabel[j] = 3;
  63. }
  64. }
  65. }
  66. }
  67. else
  68. {
  69. cout<<"Mode: 去除孔洞. ";
  70. for(int i = 0; i < Src.rows; ++i)
  71. {
  72. uchar* iData = Src.ptr<uchar>(i);
  73. uchar* iLabel = Pointlabel.ptr<uchar>(i);
  74. for(int j = 0; j < Src.cols; ++j)
  75. {
  76. if (iData[j] > 10)
  77. {
  78. iLabel[j] = 3;
  79. }
  80. }
  81. }
  82. }
  83. vector<Point2i> NeihborPos; //记录邻域点位置
  84. NeihborPos.push_back(Point2i(-1, 0));
  85. NeihborPos.push_back(Point2i(1, 0));
  86. NeihborPos.push_back(Point2i(0, -1));
  87. NeihborPos.push_back(Point2i(0, 1));
  88. if (NeihborMode==1)
  89. {
  90. cout<<"Neighbor mode: 8邻域."<<endl;
  91. NeihborPos.push_back(Point2i(-1, -1));
  92. NeihborPos.push_back(Point2i(-1, 1));
  93. NeihborPos.push_back(Point2i(1, -1));
  94. NeihborPos.push_back(Point2i(1, 1));
  95. }
  96. else cout<<"Neighbor mode: 4邻域."<<endl;
  97. int NeihborCount=4+4*NeihborMode;
  98. int CurrX=0, CurrY=0;
  99. //开始检测
  100. for(int i = 0; i < Src.rows; ++i)
  101. {
  102. uchar* iLabel = Pointlabel.ptr<uchar>(i);
  103. for(int j = 0; j < Src.cols; ++j)
  104. {
  105. if (iLabel[j] == 0)
  106. {
  107. //********开始该点处的检查**********
  108. vector<Point2i> GrowBuffer; //堆栈,用于存储生长点
  109. GrowBuffer.push_back( Point2i(j, i) );
  110. Pointlabel.at<uchar>(i, j)=1;
  111. int CheckResult=0; //用于判断结果(是否超出大小),0为未超出,1为超出
  112. for ( int z=0; z<GrowBuffer.size(); z++ )
  113. {
  114. for (int q=0; q<NeihborCount; q++) //检查四个邻域点
  115. {
  116. CurrX=GrowBuffer.at(z).x+NeihborPos.at(q).x;
  117. CurrY=GrowBuffer.at(z).y+NeihborPos.at(q).y;
  118. if (CurrX>=0&&CurrX<Src.cols&&CurrY>=0&&CurrY<Src.rows) //防止越界
  119. {
  120. if ( Pointlabel.at<uchar>(CurrY, CurrX)==0 )
  121. {
  122. GrowBuffer.push_back( Point2i(CurrX, CurrY) ); //邻域点加入buffer
  123. Pointlabel.at<uchar>(CurrY, CurrX)=1; //更新邻域点的检查标签,避免重复检查
  124. }
  125. }
  126. }
  127. }
  128. if (GrowBuffer.size()>AreaLimit) CheckResult=2; //判断结果(是否超出限定的大小),1为未超出,2为超出
  129. else {CheckResult=1; RemoveCount++;}
  130. for (int z=0; z<GrowBuffer.size(); z++) //更新Label记录
  131. {
  132. CurrX=GrowBuffer.at(z).x;
  133. CurrY=GrowBuffer.at(z).y;
  134. Pointlabel.at<uchar>(CurrY, CurrX) += CheckResult;
  135. }
  136. //********结束该点处的检查**********
  137. }
  138. }
  139. }
  140. CheckMode=255*(1-CheckMode);
  141. //开始反转面积过小的区域
  142. for(int i = 0; i < Src.rows; ++i)
  143. {
  144. uchar* iData = Src.ptr<uchar>(i);
  145. uchar* iDstData = Dst.ptr<uchar>(i);
  146. uchar* iLabel = Pointlabel.ptr<uchar>(i);
  147. for(int j = 0; j < Src.cols; ++j)
  148. {
  149. if (iLabel[j] == 2)
  150. {
  151. iDstData[j] = CheckMode;
  152. }
  153. else if(iLabel[j] == 3)
  154. {
  155. iDstData[j] = iData[j];
  156. }
  157. }
  158. }
  159. cout<<RemoveCount<<" objects removed."<<endl;
  160. }


一、对于二值图,0代表黑色,255代表白色。去除小连通区域与孔洞,小连通区域用8邻域,孔洞用4邻域。

 

  函数名字为:void RemoveSmallRegion(Mat &Src, Mat &Dst,int AreaLimit, int CheckMode, int NeihborMode)

     CheckMode: 0代表去除黑区域,1代表去除白区域; NeihborMode:0代表4邻域,1代表8邻域;  

如果去除小连通区域CheckMode=1,NeihborMode=1去除孔洞CheckMode=0,NeihborMode=0

     记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查 。

1.先对整个图像扫描,如果是去除小连通区域,则将黑色的背景图作为合格,像素值标记为3,如果是去除孔洞,则将白色的色素点作为合格,像素值标记为3。

2.扫面整个图像,对图像进行处理。


  1. void RemoveSmallRegion(Mat &Src, Mat &Dst,int AreaLimit, int CheckMode, int NeihborMode)
  2. {
  3. int RemoveCount = 0;
  4. //新建一幅标签图像初始化为0像素点,为了记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查
  5. //初始化的图像全部为0,未检查
  6. Mat PointLabel = Mat::zeros(Src.size(), CV_8UC1);
  7. if (CheckMode == 1)//去除小连通区域的白色点
  8. {
  9. cout << "去除小连通域.";
  10. for (int i = 0; i < Src.rows; i++)
  11. {
  12. for (int j = 0; j < Src.cols; j++)
  13. {
  14. if (Src.at<uchar>(i, j) < 10)
  15. {
  16. PointLabel.at<uchar>(i, j) = 3;//将背景黑色点标记为合格,像素为3
  17. }
  18. }
  19. }
  20. }
  21. else//去除孔洞,黑色点像素
  22. {
  23. cout << "去除孔洞";
  24. for (int i = 0; i < Src.rows; i++)
  25. {
  26. for (int j = 0; j < Src.cols; j++)
  27. {
  28. if (Src.at<uchar>(i, j) > 10)
  29. {
  30. PointLabel.at<uchar>(i, j) = 3;//如果原图是白色区域,标记为合格,像素为3
  31. }
  32. }
  33. }
  34. }
  35. vector<Point2i>NeihborPos;//将邻域压进容器
  36. NeihborPos.push_back(Point2i(-1, 0));
  37. NeihborPos.push_back(Point2i(1, 0));
  38. NeihborPos.push_back(Point2i(0, -1));
  39. NeihborPos.push_back(Point2i(0, 1));
  40. if (NeihborMode == 1)
  41. {
  42. cout << "Neighbor mode: 8邻域." << endl;
  43. NeihborPos.push_back(Point2i(-1, -1));
  44. NeihborPos.push_back(Point2i(-1, 1));
  45. NeihborPos.push_back(Point2i(1, -1));
  46. NeihborPos.push_back(Point2i(1, 1));
  47. }
  48. else cout << "Neighbor mode: 4邻域." << endl;
  49. int NeihborCount = 4 + 4 * NeihborMode;
  50. int CurrX = 0, CurrY = 0;
  51. //开始检测
  52. for (int i = 0; i < Src.rows; i++)
  53. {
  54. for (int j = 0; j < Src.cols; j++)
  55. {
  56. if (PointLabel.at<uchar>(i, j) == 0)//标签图像像素点为0,表示还未检查的不合格点
  57. { //开始检查
  58. vector<Point2i>GrowBuffer;//记录检查像素点的个数
  59. GrowBuffer.push_back(Point2i(j, i));
  60. PointLabel.at<uchar>(i, j) = 1;//标记为正在检查
  61. int CheckResult = 0;
  62. for (int z = 0; z < GrowBuffer.size(); z++)
  63. {
  64. for (int q = 0; q < NeihborCount; q++)
  65. {
  66. CurrX = GrowBuffer.at(z).x + NeihborPos.at(q).x;
  67. CurrY = GrowBuffer.at(z).y + NeihborPos.at(q).y;
  68. if (CurrX >= 0 && CurrX<Src.cols&&CurrY >= 0 && CurrY<Src.rows) //防止越界
  69. {
  70. if (PointLabel.at<uchar>(CurrY, CurrX) == 0)
  71. {
  72. GrowBuffer.push_back(Point2i(CurrX, CurrY)); //邻域点加入buffer
  73. PointLabel.at<uchar>(CurrY, CurrX) = 1; //更新邻域点的检查标签,避免重复检查
  74. }
  75. }
  76. }
  77. }
  78. if (GrowBuffer.size()>AreaLimit) //判断结果(是否超出限定的大小),1为未超出,2为超出
  79. CheckResult = 2;
  80. else
  81. {
  82. CheckResult = 1;
  83. RemoveCount++;//记录有多少区域被去除
  84. }
  85. for (int z = 0; z < GrowBuffer.size(); z++)
  86. {
  87. CurrX = GrowBuffer.at(z).x;
  88. CurrY = GrowBuffer.at(z).y;
  89. PointLabel.at<uchar>(CurrY,CurrX)+=CheckResult;//标记不合格的像素点,像素值为2
  90. }
  91. //********结束该点处的检查**********
  92. }
  93. }
  94. }
  95. CheckMode = 255 * (1 - CheckMode);
  96. //开始反转面积过小的区域
  97. for (int i = 0; i < Src.rows; ++i)
  98. {
  99. for (int j = 0; j < Src.cols; ++j)
  100. {
  101. if (PointLabel.at<uchar>(i,j)==2)
  102. {
  103. Dst.at<uchar>(i, j) = CheckMode;
  104. }
  105. else if (PointLabel.at<uchar>(i, j) == 3)
  106. {
  107. Dst.at<uchar>(i, j) = Src.at<uchar>(i, j);
  108. }
  109. }
  110. }
  111. cout << RemoveCount << " objects removed." << endl;
  112. }






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

闽ICP备14008679号