OpenCVSharp入门教程 特征提取③——HoughLinesP直线寻找,直线提取

忘是亡心i 2023-01-24 04:25 108阅读 0赞

文章目录

  • 一、前文
  • 二、算法流程
  • 三、界面布局
  • 四、功能实现
    • 4.1 打开图片
    • 4.2 HoughLinesP直线提取—源码
    • 4.3 HoughLinesP直线提取—参数讲解
  • 五、运行效果图
  • 六、参考

一、前文

Hough变换
经典的直线检测算法

二、算法流程

  1. 高斯模糊
  2. Canny边缘检测
  3. HoughLinesP直线寻找,直线提取

三、界面布局

  • 一个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 HoughLinesP直线提取—源码

  1. private void houghLinesBtn_Click(object sender, EventArgs e)
  2. {
  3. cannyBtn_Click(sender, e);
  4. mOutput = new Mat(mInput.Rows, mInput.Cols, MatType.CV_8UC4);
  5. mInput.CopyTo(mOutput);
  6. double rho = 1;
  7. double theta = 1;
  8. int threshold = 10;
  9. double minLineLength = 0;
  10. double maxLineGap = 0;
  11. var res = Cv2.HoughLinesP(edges,
  12. rho: rho,
  13. theta: theta / 100.0,
  14. threshold: threshold,
  15. minLineLength: minLineLength,
  16. maxLineGap: maxLineGap);
  17. for (int i = 0; i < res.Length; i++)
  18. {
  19. Scalar color = Scalar.RandomColor();
  20. Cv2.Line(mOutput, res[i].P1, res[i].P2,
  21. color: color,
  22. thickness: 2,
  23. lineType: LineTypes.Link8);
  24. }
  25. srcPictureBox.Image = BitmapConverter.ToBitmap(mInput);
  26. grayPictureBox.Image = BitmapConverter.ToBitmap(edges);
  27. dstPictureBox.Image = BitmapConverter.ToBitmap(mOutput);
  28. }

4.3 HoughLinesP直线提取—参数讲解

  1. //
  2. // 摘要:
  3. // Finds lines segments in a binary image using probabilistic Hough transform.
  4. //
  5. // 参数:
  6. // image:
  7. //
  8. // rho:
  9. // Distance resolution of the accumulator in pixels
  10. //
  11. // theta:
  12. // Angle resolution of the accumulator in radians
  13. //
  14. // threshold:
  15. // The accumulator threshold parameter. Only those lines are returned that get enough
  16. // votes ( > threshold )
  17. //
  18. // minLineLength:
  19. // The minimum line length. Line segments shorter than that will be rejected. [By
  20. // default this is 0]
  21. //
  22. // maxLineGap:
  23. // The maximum allowed gap between points on the same line to link them. [By default
  24. // this is 0]
  25. //
  26. // 返回结果:
  27. // The output lines. Each line is represented by a 4-element vector (x1, y1, x2,
  28. // y2)
  29. public static LineSegmentPoint[] HoughLinesP(InputArray image, double rho, double theta, int threshold, double minLineLength = 0, double maxLineGap = 0);
  • image,灰度图片输入
  • rho,步长为1的半径
  • theta,步长为π/180的角来,来搜索所有可能的直线
  • threshold,阈值,大于阈值 threshold 的线段才可以被确认为直线。该值越小,判定出的直线越多;值越大,判定出的直线就越少。
  • minLineLength,线段的最短长度,长度小于该值的线段会被忽略
  • maxLineGap,两条线段之间的最大间隔,间隔小于该值的两条线会被当成一条线

五、运行效果图

从左到右

  • 第一张是原图
  • 第二张是高斯模糊的结果图
  • 第三张是HoughLinesP直线提取的结果图

在这里插入图片描述

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

六、参考

OpenCV—直线检测

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

发表评论

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

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

相关阅读