21. Hough transform-straight line detection

1. Main content

  • Introduction to Hough Line Transformation
  • Related API learning

2. Introduction to Hough Line Transformation

  • Hough Line Transform is used for linear transformation

  • Prerequisites-edge detection and completion

  • Conversion from plane space to polar coordinate space

  • Through the Hough transform, we can detect a regular geometric shape. The principle is to convert each point of rectangular coordinates to polar coordinate space through the change of polar coordinates to form a curve. The position of the intersection of the curves in the image, we think Are the points on the same straight line, and then we are calculating the polar coordinates of these points to the rectangular coordinate space through the formula.
    Insert picture description here

  • r is the distance. With x0 and y0 unchanged, we get an r value for each given theta. At this time, we get the curve of the relationship between each angle and r 'in Figure 1.
    Insert picture description here

  • At this time, we move x0, y0 one forward, and then find the polar coordinates of the function, theta and r, and repeat the operation continuously to obtain the result shown in Figure 2. These curves intersect at one point, indicating that our pixels all belong to the same straight line.
    Insert picture description here

  • According to our figure two, the angle and polar diameter of the intersection point of the curve are taken into the following formula to calculate back to the plane space, input different x, and get different y.
    The obtained coordinate points are retained on the image, and deleted if they are not on the image.
    Insert picture description here

  • Question: How do you know that this is the point of intersection in the image?
    Answer: Add 1 to the data where each curve passes, because the data at the intersection is the most, then the point is the brightest.
    Insert picture description here

  • For all points on any straight line, transform to polar coordinates, from [0 · 360] space, you can get the size of r. Points belonging to the same line in the polar coordinate space [r, theta] must have the strongest signal (the intersection) at a point, and the pixel coordinates of each point on the line can be obtained by inversely calculating the plane coordinates. Thereby getting a straight line.

  • Transformation from plane coordinates to Hough space (polar coordinates)
    Insert picture description here

3. Related API learning

  • The standard Hough transform cv :: HoughLines is converted from plane coordinates to Hough space, and the final output is (threta, r) ​​representing a polar coordinate space.
  • Hough transform straight line probability cv ;; HoughLinesP final output is two points of the straight line (x0, y0, x1, y1)

4. Relevant API learning

  • cv ;; HonghLines
cv::HoughLine(
    InputArray src,   //输入图像,必须8—bit的灰度图像
    OutputArray lines,//输出的极坐标来表示直线
    double rho, //生成极坐标的时候像素扫描步长(极坐标空间中r的最大值),一般取值1
    double theta,//生成极坐标的时候的角度扫描步长,一般取值CV_PI/180,每次移动一度
    int threshold,//阈值,只有获得足够交点的极坐标点才被看成是直线
    double srn = 0,//是否应用多尺度的霍夫变换,如果不是设置0表示经典霍夫变换(此处的多尺度参考图像金字塔的相关知识)
    double stn = 0.//是否应用多尺度的霍夫变换,如果不是设置0表示经典霍夫变换
    double min_theta = 0,//表示角度扫描范围0-180度之间,默认即可
    double max_theta = CV_PI
)//一般是有经验的开发者使用,需要自己反变换到平面空间
  • cv :: HonghLinesP
cv::HoughLineP(
    InputArray src,   //输入图像,必须8—bit的灰度图像
    OutputArray lines,//输出的极坐标来表示直线
    double rho, //生成极坐标的时候像素扫描步长
    double theta,//生成极坐标的时候的角度扫描步长,一般取值CV_PI/180
    int threshold,//阈值,只有获得足够交点的极坐标点才被看成是直线
    double minlineLength = 0,//最小直线长度
    double maxLineGap = 0,//最大间隔,比较重要的一个参数
    )//自动反算到平面空间

HonghLinesP detection effect
Insert picture description here

  • Demo code
//先进行边缘检测
imshow("原图像",src);
Canny(src,src_gray,150,200,3);
cvtColor(src_gray,dst,CV_GRAY2BGR);
//霍夫直线检测
vector<Vec4f>plines;//将我们霍夫变换得到的结果存放到这个数据中
HonghLinesP(src_gray,plines,1,CV_PI/180,10,0,10);
for(size_t i = 0;i < plines.size();i++){
    Vec4f  h1 = plines[i];
    line(dst,Point(h1[0],h1[1]),Point(h1[2],h1[3]),Scalar(0,0,255),3,LINE_AA);
}  
imshow("输出图像",dst);

5.line function

void  line(
    Mat& img, //要绘制线段的图像
    Point pt1,//线段的起点
    Point pt2,//线段的终点
    const Scalar& color,//线段的颜色,通过一个Scalar对象定义
    int thickness = 1,//线条的宽度
    int linetType = 8,
    //线条的类型。可以取值8,4,和CV_AA,
    //分别代表8邻接连接线、4邻接连接线和反锯齿连接线
   // 默认值为8邻接连接线,为了获得更好的效果可以选用CV_AA(采用了高斯滤波)
    int shift = 0 //坐标点小数点位数
)

Examples:

line(frame,beginPoint,endPoint,Scalar(0,0,255),2)
//画一条直线,起点为beginPoint,终点为endPoint,颜色是红色,线宽为2,shift是默认值

6. Extracurricular expansion


The difference between size_t data type OpenCV CV_RGB2GRAY and CV_BGR2GRAY
OpenCV Image Watch plug-in installation and use

Published 66 original articles · won praise 53 · views 6812

Guess you like

Origin blog.csdn.net/qq_43367829/article/details/105423785