Hough transform principle and achieve OpenCV

Hough transform (Hough Transform) processing an image feature extraction techniques, an object having a specific shape is detected by means of a voting algorithm. The process parameter space a set of one matches the maximum value obtained by calculating the specific shape of a partial integration result as a result of the Hough transform. Hough transform in 1962 by the Paul Hough was first proposed [53], after 1972 by the Richard Duda and Peter Hart promote the use of [54], classical Hough transform for line detection in the image, and later extended to any Hough transform recognize the shape of the object, it is multiple circles and ellipses.

After several days of learning, there are some differences are found understanding of the way before you, Daniel, but the core idea has not changed, so that the recording your own understanding of the Hough transform line detection.

First, the principle introduction:

1, for the Cartesian coordinates of any point A (x0, y0), the straight line passing through the point A satisfies Y0 = k * X0 + b. (K is the slope, b is the intercept)

2, the XY plane through the point A (x0, y0) of the linear tufts can Y0 = k * X0 + b represented, but the slope of the straight line perpendicular to the X axis is infinite is not represented. Thus converting Cartesian coordinate system into a polar coordinate system can solve this particular case.

3, the line represents the equation in polar coordinates ρ = xCosθ ​​+ ySinθ (ρ is the distance from the origin to the line), as shown:

4, above, assumes a straight line in the plane of the pixels in a 8 * 8, and the (1,8) pixel counted from the top left θ is 0 °, respectively, 45 °, 90 °, 135 °, 180 ° when the [rho], can be seen in FIG respectively [rho] 1, (9√2) / 2,8, (7√2) / 2, -1, and to these five values ​​were recorded a ticket, calculated pixel empathy (3,6) point θ of 0 °, 45 °, 90 °, 135 °, ρ at 180 °, to give the calculated values ​​were recorded 5 [rho] a ticket, then you will find ρ = (9√ this value 2) / 2 has been recorded two votes, and so on, traversing the full 8 x 8 pixel space of time ρ = (9√2) / 2 to remember the five votes, the votes of the other values ​​ρ less than 5 tickets, so to obtain the polar equation of the straight line in this 8 * 8 pixel coordinates as in (9√2) / 2 = x * Cos45 ° + y * Sin45 °, to find out that this linear equation . (PS: the actual value of θ is not so big span, usually PI / 180).
 

#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
 
int main()
{
	Mat Image = imread(".//res//street.jpg", 0);
	Mat CannyImg;
	Canny(Image, CannyImg, 140, 250, 3);
	imshow("CannyImg", CannyImg);
 
	Mat DstImg;
	cvtColor(Image, DstImg, CV_GRAY2BGR);
 
	vector<Vec4i> Lines;
	HoughLinesP(CannyImg, Lines, 1, CV_PI / 360, 170,30,15);
	for (size_t i = 0; i < Lines.size(); i++)
	{
		line(DstImg, Point(Lines[i][0], Lines[i][1]), Point(Lines[i][2], Lines[i][3]), Scalar(0, 0, 255), 2, 8);
	}
	imshow("HoughLines_Detect", DstImg);
	imwrite(".//res//HoughLines_Detect.jpg", DstImg);
	waitKey(0);
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41598072/article/details/90704640
Recommended