OpenCVSharp入门教程 基础篇⑥——Canny边缘检测算法

ゝ一世哀愁。 2023-01-23 14:49 148阅读 0赞

文章目录

  • 一、前文
  • 二、Canny边缘检测算法流程
  • 三、界面布局
  • 四、功能实现
    • 4.1 打开图片
    • 4.2 Canny边缘检测—源码
    • 4.3 Canny边缘检测—参数讲解
  • 五、运行效果图

一、前文

Canny边缘检测算法,Canny是一个人名
Canny 的目标是找到一个最优的边缘检测算法,最优边缘检测的含义是:
(1) 最优检测:算法能够尽可能多地标识出图像中的实际边缘,漏检真实边缘的概率和误检非边缘的概率都尽可能小;
(2) 最优定位准则:检测到的边缘点的位置距离实际边缘点的位置最近,或者是由于噪声影响引起检测出的边缘偏离物体的真实边缘的程度最小;
(3) 检测点与边缘点一一对应:算子检测的边缘点与实际边缘点应该是一一对应。

二、Canny边缘检测算法流程

  1. 应用高斯滤波来平滑图像,目的是去除噪声
  2. 找寻图像的强度梯度(intensity gradients)
  3. 应用非最大抑制(non-maximum suppression)技术来消除边误检(本来不是但检测出来是)
  4. 应用双阈值的方法来决定可能的(潜在的)边界
  5. 利用滞后技术来跟踪边界

三、界面布局

  • 一个Label
  • N个Button
  • 三个Picture

在这里插入图片描述

四、功能实现

4.1 打开图片

  1. private void openFileBtn_Click(object sender, EventArgs e)
  2. {
  3. OpenFileDialog openfiledialog = new OpenFileDialog();
  4. openfiledialog.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
  5. openfiledialog.RestoreDirectory = true;
  6. if (openfiledialog.ShowDialog() == DialogResult.OK)
  7. {
  8. Console.WriteLine(openfiledialog.FileName);
  9. fileName = openfiledialog.FileName;
  10. //Mat src = new Mat("foo.png", LoadMode.Color);
  11. Mat src = new Mat(fileName);
  12. //Mat src = new Mat(fileName, ImreadModes.Color);
  13. var frameBitmap = BitmapConverter.ToBitmap(src);
  14. pictureBox1.Image?.Dispose();
  15. pictureBox1.Image = frameBitmap;
  16. }
  17. }

4.2 Canny边缘检测—源码

  1. private void threshold1ScrollBar_Scroll(object sender, ScrollEventArgs e)
  2. {
  3. threshold1Label.Text = threshold1ScrollBar.Value + "";
  4. cannyBtn_Click(sender, e);
  5. }
  6. private void threshold2ScrollBar_Scroll(object sender, ScrollEventArgs e)
  7. {
  8. threshold2Label.Text = threshold2ScrollBar.Value + "";
  9. cannyBtn_Click(sender, e);
  10. }
  11. private void cannyBtn_Click(object sender, EventArgs e)
  12. {
  13. GaussianBlurBtn_Click(sender, e);
  14. edges = new Mat(mInput.Rows, mInput.Cols, MatType.CV_8UC4);
  15. double threshold1 = threshold1ScrollBar.Value;
  16. double threshold2 = threshold2ScrollBar.Value;
  17. int apertureSize = 3;
  18. Cv2.Canny(blur, edges, threshold1, threshold2, apertureSize); //边缘检测
  19. srcPictureBox.Image = BitmapConverter.ToBitmap(mInput);
  20. grayPictureBox.Image = BitmapConverter.ToBitmap(blur);
  21. dstPictureBox.Image = BitmapConverter.ToBitmap(edges);
  22. }

4.3 Canny边缘检测—参数讲解

  1. //
  2. // 摘要:
  3. // Finds edges in an image using Canny algorithm.
  4. //
  5. // 参数:
  6. // src:
  7. // Single-channel 8-bit input image
  8. //
  9. // edges:
  10. // The output edge map. It will have the same size and the same type as image
  11. //
  12. // threshold1:
  13. // The first threshold for the hysteresis procedure
  14. //
  15. // threshold2:
  16. // The second threshold for the hysteresis procedure
  17. //
  18. // apertureSize:
  19. // Aperture size for the Sobel operator [By default this is ApertureSize.Size3]
  20. //
  21. // L2gradient:
  22. // Indicates, whether the more accurate L2 norm should be used to compute the image
  23. // gradient magnitude (true), or a faster default L1 norm is enough (false). [By
  24. // default this is false]
  25. public static void Canny(InputArray src, OutputArray edges, double threshold1, double threshold2, int apertureSize = 3, bool L2gradient = false);
  • threshold1,滞后过程的第一个阈值,下限,如果低于下限,则被舍弃掉
  • threshold2,滞后过程的第二个阈值,上限,如果高于上限,则认为是边缘像素。如果在两个阈值之间,且跟边缘像素相连才会被接受。
  • apertureSize,Sobel计算用的孔径大小,只能选3/5/7
  • L2gradient,L2范数计算方式的指定

五、运行效果图

从左到右

  • 第一张是原图
  • 第二张是高斯模糊的结果图
  • 第三张是Canny边缘检测的结果图

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

觉得好,就一键三连呗(点赞+收藏+关注)

发表评论

表情:
评论列表 (有 0 条评论,148人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Canny边缘检测算法

    1、概述       图象的边缘是指图象局部区域亮度变化显著的部分,该区域的灰度剖面一般可以看作是一个阶跃,既从一个灰度值在很小的缓冲区域内急剧变化到另一个灰度相差较...

    相关 Canny 边缘检测

    Canny 边缘检测是一种非常流行的边缘检测算法,是 John F.Canny 在 1986 年提出的。它是一个有很多步构成的算法,我们接下来会逐步介绍。 1 噪声去

    相关 canny边缘检测

    Canny边缘检测算法是1986提出的多阶段的边缘检测算法。 预处理 由于边缘易受噪声影响,所以对图像利用高斯滤波器来去除噪声。 ![G\_\{0\}(x, y) =

    相关 Canny 边缘检测

    Canny 边缘检测主要步骤: 1. 先把图像变成灰色 2. 计算图像梯度,其中每个像素的亮度对应该店的梯度值。通过寻找最大的梯度值,就可以找到边缘。 首先,我们需要读

    相关 Canny边缘检测算法的实现

    转自:[canny边缘检测][canny] 图像边缘信息主要集中在高频段,通常说图像锐化或检测边缘,实质就是高频滤波。我们知道微分运算是求信号的变化率,具有加强高频分量的作用