OpenCVSharp入门教程 特征提取③——HoughLinesP直线寻找,直线提取
生活随笔
收集整理的這篇文章主要介紹了
OpenCVSharp入门教程 特征提取③——HoughLinesP直线寻找,直线提取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、前文
- 二、算法流程
- 三、界面布局
- 四、功能實現
- 4.1 打開圖片
- 4.2 HoughLinesP直線提取—源碼
- 4.3 HoughLinesP直線提取—參數講解
- 五、運行效果圖
- 六、參考
一、前文
Hough變換
經典的直線檢測算法
二、算法流程
三、界面布局
- 一個Label
- N個Button
- 三個Picture
四、功能實現
4.1 打開圖片
private void openFileBtn_Click(object sender, EventArgs e){OpenFileDialog openfiledialog = new OpenFileDialog();openfiledialog.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";openfiledialog.RestoreDirectory = true;if (openfiledialog.ShowDialog() == DialogResult.OK){Console.WriteLine(openfiledialog.FileName);fileName = openfiledialog.FileName;//Mat src = new Mat("foo.png", LoadMode.Color);Mat src = new Mat(fileName);//Mat src = new Mat(fileName, ImreadModes.Color);var frameBitmap = BitmapConverter.ToBitmap(src);pictureBox1.Image?.Dispose();pictureBox1.Image = frameBitmap;}}4.2 HoughLinesP直線提取—源碼
private void houghLinesBtn_Click(object sender, EventArgs e) {cannyBtn_Click(sender, e);mOutput = new Mat(mInput.Rows, mInput.Cols, MatType.CV_8UC4);mInput.CopyTo(mOutput);double rho = 1;double theta = 1;int threshold = 10;double minLineLength = 0;double maxLineGap = 0;var res = Cv2.HoughLinesP(edges,rho: rho,theta: theta / 100.0,threshold: threshold,minLineLength: minLineLength,maxLineGap: maxLineGap);for (int i = 0; i < res.Length; i++){Scalar color = Scalar.RandomColor();Cv2.Line(mOutput, res[i].P1, res[i].P2,color: color,thickness: 2,lineType: LineTypes.Link8);}srcPictureBox.Image = BitmapConverter.ToBitmap(mInput);grayPictureBox.Image = BitmapConverter.ToBitmap(edges);dstPictureBox.Image = BitmapConverter.ToBitmap(mOutput); }4.3 HoughLinesP直線提取—參數講解
// // 摘要: // Finds lines segments in a binary image using probabilistic Hough transform. // // 參數: // image: // // rho: // Distance resolution of the accumulator in pixels // // theta: // Angle resolution of the accumulator in radians // // threshold: // The accumulator threshold parameter. Only those lines are returned that get enough // votes ( > threshold ) // // minLineLength: // The minimum line length. Line segments shorter than that will be rejected. [By // default this is 0] // // maxLineGap: // The maximum allowed gap between points on the same line to link them. [By default // this is 0] // // 返回結果: // The output lines. Each line is represented by a 4-element vector (x1, y1, x2, // y2) 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—直線檢測
覺得好,就一鍵三連唄(點贊+收藏+關注)
總結
以上是生活随笔為你收集整理的OpenCVSharp入门教程 特征提取③——HoughLinesP直线寻找,直线提取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 马士兵python_马士兵老师的pyth
- 下一篇: dilink虚拟服务器怎么设置,最简单的