opencv-Hough transform-straight line detection

Hough Transform

definition

Hough transform is a type of feature extraction, which is widely used in image analysis
, computer vision, and digital image processing.

The Hough transform is used to identify features in objects, and the process is roughly as follows:

Given an object, the type of shape to be distinguished, the algorithm will perform voting in the parameter space to determine the shape of the object, and this is determined by the local maximum in the accumulator space Decided.

Commonly used Hough transform

  • 1. Hough line transformation: find a straight line in the image

  • 2. Hough circle transformation: find a circle in the image.

Hough linear transformation

  • Hough Line Transform is used for straight line detection
  • Prerequisite-
    Canny edge detection has been completed , input 8-bit image, output single-channel 8-bit grayscale image
  • Plane space to polar coordinate space conversion

How do I know that the detected line is a straight line?
Insert picture description here
The image is first converted to polar coordinates
polar coordinates midpoint is characterized by r and θ
i.e.
Insert picture description here
according to the above formula, we can derive the following equation:
Insert picture description here
derivation process may be as follows:
Insert picture description here

What does the formula do?
For example,
Insert picture description here
for the above straight line, the following five points are on the straight line:
Insert picture description here
so we can get five straight lines:
Insert picture description here

If we take θ as the abscissa and r as the ordinate, when we draw these five straight lines in the same coordinate system, we can get the following picture:

Insert picture description here
We found that these five straight lines intersect at the same point. This is the characteristic of straight lines. We use this feature to accumulate all straight lines. Because the intersection point is the sum of all straight lines, the pixel value must be the highest. We know , The higher the pixel, the brighter the image, when it reaches 255, it is the brightest, that is, white. So we can detect straight lines in this way.

Insert picture description here

Visual explanation

Insert picture description here
There is such an image, and the corresponding polar coordinate image is as follows. The
Insert picture description here
white point is where the straight lines intersect.
The position of the straight line can be inferred by using polar coordinates.
Insert picture description here

HoughLines

The final output of this API is the coordinates in polar coordinates, that is, the
final result of (θ, r) is difficult to calculate back, so there is HoughLinesP in opencv. HoughLines
is introduced here.

HoughLines( 
    InputArray image, 
    OutputArray lines,
    double rho, 
    double theta, 
    int threshold,                              
    double srn = 0, 
    double stn = 0,                              
    double min_theta = 0, 
    double max_theta = CV_PI 
);

(1) InputArray type src, 8-bit, single-channel binary source image (grayscale image).

(2) Lines of the OutputArray type, the output vector of the straight line. Each straight line is represented by a two-element vector. rho is the distance (0,0) from the origin of the coordinates (upper left corner of the image). Theta is the angle of rotation of the line in radians.

(3) Double type rho, the distance resolution (pixels) of the accumulator, generally 1

(4) Double type theta, the angular resolution of the accumulator (in radians), the general value is CV_PI/180, one degree each time .

(5) Threshold of int type, accumulator threshold parameter. Only return lines that have enough accumulators. Generally, 10 is used . 10 pixels are counted as a line. If the line is long enough, it can be changed to 100.

(6) Double type srn, for multi-scale Hough transform, it is the divisor of the distance resolution rho, the coarse accumulator distance resolution is rho, and the precise accumulator resolution is rho/srn. If srn=0 and stn=0, the classical Hough transform is used. Otherwise, both of these parameters should be positive. Set to 0 if multi-scale Hough transform is not used

(7) Double type sth, for multi-scale Hough transform, it is the divisor of distance resolution θ. Set to 0 if multi-scale Hough transform is not used

(8) The min_theta of double type is used for standard and multi-scale Hough transform to check the minimum angle of a straight line. Must be between 0 and maxθ.

(9) Double type max_theta, used for standard and multi-scale Hough transform, used to check the maximum angle of the line. Must be between the minimum θ and CVπ.

HoughLinesP

This function returns the coordinates directly. It is
recommended to use this API

HoughLines( 
    InputArray image, 
    OutputArray lines,
    double rho, 
    double theta, 
    int threshold,                              
    double minLineLength = 0, 
    double maxLineGap = 0                             
);

(1) InputArray type src, 8-bit, single-channel binary source image (grayscale image).

(2) Lines of the OutputArray type, the output vector of the straight line. Each straight line is represented by a two-element vector. rho is the distance (0,0) from the origin of the coordinates (upper left corner of the image). Theta is the angle of rotation of the line in radians.

(3) For rho of double type, the distance resolution (pixel) of the accumulator is generally 1

(4) For double type theta, the angular resolution (radians) of the accumulator is generally CV_PI/180 , one degree each time.

(5) Threshold of int type, accumulator threshold parameter. Only return lines that have enough accumulators. Generally take 10 , 10 pixels are counted as a straight line, if the straight line is long enough, it can be changed to 100.

(6) minLineLength of double type, the minimum line length. Line segments smaller than this value will be rejected.

(7) Double type maxLineGap, the maximum distance allowed by linking points on the same line.

Code

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    
    
	Mat src, src_gray, dst;
	src = imread("C:/Users/86176/Pictures/pics/lines.jpg");
	if (!src.data)
	{
    
    
		cout << "could not load image !";
		return -1;
	}
	imshow("【输入图像】", src);

	GaussianBlur(src, src, Size(3, 3), 0, 0);
	Canny(src, src_gray, 0, 255);
	cvtColor(src_gray, dst, CV_GRAY2BGR);

	imshow("【获取边缘】", src_gray);

	vector<Vec4f> plines;
	HoughLinesP(src_gray, plines, 1, CV_PI / 180.0, 100, 0, 10);

	Scalar color = Scalar(0, 0, 255);
	for (size_t i = 0; i < plines.size(); i++) {
    
    
		Vec4f hline = plines[i];
		line(dst, Point(hline[0], hline[1]), Point(hline[2], hline[3]), color, 3, LINE_AA);
	}

	imshow("【输出图像】", dst);

	waitKey(0);
	return 0;
}

effect

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_28258885/article/details/112940316