当前位置:   article > 正文

【OpenCV】超详细边缘提取算法流程(附详细代码)_opencv 边缘提取

opencv 边缘提取

在传统的计算机视觉领域,经常需要使用一些传统的图像处理算法完成对图像的边缘提取功能,通过对图像的边缘进行提取完成对目标对象的分割,目标分割技术又包括语义分割与实例分割,比较高端的鲁棒性较强的还是需要卷积神经网络算法进行相关的训练,如fcn全连接网络,mask-rcnn实例分割网络。本案例旨在采用传统的图像处理技术完成对图像的边缘检测任务,并通过膨胀腐蚀操作进行连通域的提取,之后通过连通域的填充以及掩膜操作完成目标对象的分割。

具体需要达到的目标如下:

                         

 上图所示,可以很好的将河道信息进行提取。

1. 具体算法流程

首先对采集的图片进行灰度化处理(方便进行数据处理),然后对灰度图像进行中值滤波操作去除湖面上的细小杂质,之后通过x方向和y方向上的Sobel梯度算子分别获取梯度图像,并将梯度图像转换成CV_8UC1类型,并对转换后的x,y方向上的梯度图像进行OTSU二值化操作获取二值图像,并对两幅二值图像按对应像素位置进行与运算,目的是为了去除河道上的波纹干扰。(可以继续对与运算之后的结果图进行中值滤波去除湖面上的细小杂质)。最后对二值图进行多次迭代的膨胀腐蚀操作以及小区域块的填充操作(用到findContours与drawContours接口进行轮廓查找与填充)获取河道连通区域。

2.流程图

可以通过如下流程图进行展示。

                                                   

3. 涉及到的代码

  1. int main() {
  2. Mat srcImage, srcImage2, srcImage3;
  3. for (int j = 1; j <= 172; j++)
  4. {
  5. //白天总共172张图片。1-172;验证:1,162,146;
  6. //(挑选最好的效果作为模板。对比108与117进行分析)。
  7. //if (j == 108 || j == 117)
  8. //{//对比分析之后选择第108张图片作为模板图片
  9. char ch[4096] = { 0 };
  10. sprintf(ch, "..\\findriveredge\\day\\2 (%d).jpg", j);
  11. srcImage = imread(ch, IMREAD_ANYCOLOR);
  12. srcImage2 = srcImage.clone();
  13. srcImage3 = srcImage.clone();
  14. if (srcImage.empty())
  15. {
  16. return -1;
  17. }
  18. if (srcImage.channels() == 3)
  19. {
  20. cvtColor(srcImage, srcImage, COLOR_BGR2GRAY);
  21. }
  22. Mat outImage;
  23. medianBlur(srcImage, outImage, g_nKrenel);//首先对灰度图进行中值滤波操作,去除一些杂质。
  24. Mat grad_x, abs_grad_x, grayImage_x;
  25. Sobel(outImage, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
  26. convertScaleAbs(grad_x, abs_grad_x);
  27. abs_grad_x.convertTo(grayImage_x, CV_8U);
  28. Mat grad_y, abs_grad_y, grayImage_y;
  29. Sobel(outImage, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
  30. convertScaleAbs(grad_y, abs_grad_y);
  31. abs_grad_y.convertTo(grayImage_y, CV_8U);
  32. Mat XBin, YBin;
  33. int n_thresh_x = myOtsu(grayImage_x);
  34. threshold(grayImage_x, XBin, n_thresh_x, 255, THRESH_BINARY);
  35. int n_thresh_y = myOtsu(grayImage_y);
  36. threshold(grayImage_y, YBin, n_thresh_y, 255, THRESH_BINARY);
  37. Mat Bin;
  38. bitwise_and(XBin, YBin, Bin);
  39. Mat outBin;
  40. medianBlur(Bin, outBin, 3);//去除一些杂质点。
  41. Mat outBin2 = outBin.clone();
  42. int n_iterations = 5;
  43. Mat element = getStructuringElement(MORPH_ELLIPSE, Size(15, 15));//膨胀变亮形成连通域。
  44. Mat element2 = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));//腐蚀操作断开一些连通域。
  45. dilate(outBin2, outBin2, element, Point(-1, -1), n_iterations);
  46. erode(outBin2, outBin2, element2, Point(-1, -1), 3);//腐蚀操作,断开河流区域内部的连接区域,方便后续的填充处理。
  47. //将河流ROI区域小块连通域填黑。
  48. Mat outBin3 = outBin2.clone();
  49. vector<vector<Point>> contours;
  50. vector<Vec4i> hie;
  51. findContours(outBin3, contours, hie, RETR_LIST, CHAIN_APPROX_SIMPLE);
  52. float f_area = 0.0;
  53. for (int i = 0; i < contours.size(); i++)
  54. {
  55. f_area = contourArea(contours[i]);
  56. if (f_area < 250000)
  57. {
  58. drawContours(outBin3, contours, i, Scalar(0), -1);
  59. }
  60. }
  61. //由于final bin2在腐蚀过程中存在部分背景区域为黑色空洞,需要将其填白。
  62. Mat outBin4_tmp = ~outBin3;
  63. Mat outBin4;
  64. contours.clear();
  65. hie.clear();
  66. findContours(outBin4_tmp, contours, hie, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  67. for (unsigned int i = 0; i < contours.size(); i++)
  68. {
  69. f_area = contourArea(contours[i]);
  70. if (f_area < 250000)
  71. {
  72. drawContours(outBin4_tmp, contours, i, Scalar(0), -1);
  73. }
  74. }
  75. outBin4 = ~outBin4_tmp;
  76. //迭代腐蚀突出河流边界区域。
  77. erode(outBin4, outBin4, element, Point(-1, -1), 10);
  78. //(可以对腐蚀图在进行一次外轮廓填充)。
  79. Mat outBin5_tmp = ~outBin4.clone();
  80. Mat outBin5;
  81. contours.clear();
  82. hie.clear();
  83. findContours(outBin5_tmp, contours, hie, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
  84. for (unsigned int i = 0; i < contours.size(); i++)
  85. {
  86. f_area = contourArea(contours[i]);
  87. if (f_area < 100000)
  88. {
  89. drawContours(outBin5_tmp, contours, i, Scalar(0), -1);
  90. }
  91. }
  92. outBin5 = ~outBin5_tmp;
  93. //根据outBin4,对原rgb图取感兴趣区域(即河流区域)
  94. for (int i = 0; i < outBin5.rows; i++)
  95. {
  96. for (int j = 0; j < outBin5.cols; j++)
  97. {
  98. if (outBin5.at<uchar>(i, j) == 255)
  99. {
  100. srcImage2.at<Vec3b>(i, j)[0] = 0;
  101. srcImage2.at<Vec3b>(i, j)[1] = 0;
  102. srcImage2.at<Vec3b>(i, j)[2] = 0;
  103. }
  104. }
  105. }
  106. namedWindow("finalImage", 0);
  107. imshow("finalImage", srcImage2);//这里的srcImage2表示最后所需效果图
  108. cout << j << endl;
  109. waitKey(30);
  110. }
  111. return 0;
  112. }

 

 

 

 

 

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

闽ICP备14008679号