隐藏

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

发布:2024/2/4 15:59:58作者:管理员 来源:本站 浏览次数:194

文章目录


   一、前文

   二、算法流程

   三、界面布局

   四、功能实现

       4.1 打开图片

       4.2 HoughLinesP直线提取—源码

       4.3 HoughLinesP直线提取—参数讲解

   五、运行效果图

   六、参考


一、前文


   Hough变换

   经典的直线检测算法


二、算法流程


   高斯模糊

   Canny边缘检测

   HoughLinesP直线寻找,直线提取


三、界面布局


   一个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—直线检测