0.12-OpenCvSharp4霍夫变换检测直线

0.12 OpenCvSharp4霍夫变换检测直线

//
// 摘要:
//     Finds lines in a binary image using standard Hough transform.
//
// 参数:
//   image: 输入八位,单通道,二值图像
//     The 8-bit, single-channel, binary source image. The image may be modified by
//     the function
//
//   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 )
//
//   srn:
//     For the multi-scale Hough transform it is the divisor for the distance resolution
//     rho. [By default this is 0]
//
//   stn:
//     For the multi-scale Hough transform it is the divisor for the distance resolution
//     theta. [By default this is 0]
//
// 返回结果: 直线的输出向量。每条线都由一个二元向量表示。是到坐标原点(0,0)(图像的左上角)的距离,是以弧度表示的直线旋转角度
//     The output vector of lines. Each line is represented by a two-element vector
//     (rho, theta) . rho is the distance from the coordinate origin (0,0) (top-left
//     corner of the image) and theta is the line rotation angle in radians
public static LineSegmentPolar[] HoughLines(InputArray image, double rho, double theta, int threshold, double srn = 0, double stn = 0);

//
// 摘要:
//     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]
//
// 返回结果:输出线。每条线由一个4元向量(x1, y1, x2, y2)表示。
//     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);

检测步骤:
1)读入图像,灰度变换。
2)阈值二值化。
3)Canny 边缘提取。
4)霍夫变换检测直线。
实现:

Mat pLine = Cv2.ImRead("D:\\OpenCV\\Topencv\\CsharpOpenCv\\CsharpOpenCv\\3.png");
Cv2.ImShow("Line",pLine);
Mat pGray = new Mat(pLine.Size(),pLine.Type());

// 霍夫直线   canny边缘提取 --> cvtcolor转灰度 --> HoughLinesP霍夫边缘检测
// 灰度变换
Cv2.CvtColor(pLine, pGray, ColorConversionCodes.RGB2GRAY);
// 阈值二值化
Mat pThresholdImage = new Mat(pLine.Size(), pLine.Type());
Cv2.Threshold(pGray, pThresholdImage, 100, 255, ThresholdTypes.Binary);
// Canny 边缘提取
Mat pCannyImage = new Mat(pLine.Size(), pLine.Type());
Cv2.Canny(pThresholdImage, pCannyImage, 20, 50, 3);
// 霍夫变换检测直线
LineSegmentPoint[] lineSegmentPoint; //检测直线的端点的集合。LineSegmentPoint
lineSegmentPoint = Cv2.HoughLinesP(pCannyImage, 1.0, Cv2.PI / 180, 200, 50, 20);
// 转换为BGR图,为后面输出彩线
Cv2.CvtColor(pCannyImage, pCannyImage, ColorConversionCodes.GRAY2BGR); 
Scalar pLcolor = new Scalar(0, 255, 0); //绿色 BGR
for (int i = 0; i < lineSegmentPoint.Length; i++)
{
	Cv2.Line(pCannyImage, lineSegmentPoint[i].P1, lineSegmentPoint[i].P2, pLcolor, 1);   //依据点集画线
}

Cv2.ImShow("检测直线",pCannyImage);

0.12-OpenCvSharp4霍夫变换检测直线_第1张图片

参考:
OpenCv–霍夫直线变换(检测直线)

0.12-OpenCvSharp4霍夫变换检测直线_第2张图片

你可能感兴趣的:(OpenCvSharp)