Opencv learning notes (iv) hohoff Transformation

Source: Internet
Author: User

The simplest Hof transformation is to recognize straight lines in an image. In the Cartesian coordinate system (x-y) of a plane, a straight line can be expressed in the following formula.

Y = kx + B

For a definite point (x_0, y_0) on a straight line, there are: y_0 = kx_0 + B

This indicates a straight line in the parameter plane (k-B. Therefore, a point in the image corresponds to a straight line in the parameter plane, and a straight line in the image corresponds to a point in the parameter plane. Apply the HOF transformation to all vertices in the image, and the final line to be detected must correspond to the point with the most straight lines in the parameter plane. In this way, the straight line is detected in the image. In practical application, linear parameters are usually used.

Opencv has the following function to detect a straight line (the most basic Hof transformation ):

Void houghlines (inputarray image, outputarray lines, double rock, double Theta, int threshold, double SRN = 0, double STN = 0)

You can see the code for specific usage:

# Include "opencv2/opencv. HPP "# define PI 3.1415926int main (INT argc, char * argv []) {CV: mat image = CV: imread (" road.jpg "); CV: mat result; CV: cvtcolor (image, result, cv_bgra2gray); CV: mat contours; // Edge Detection CV: canny (result, contours, 125,350); STD :: vector <CV: vec2f> lines; // Hof transform, get a set of polar coordinate parameters (rock, theta), each of which corresponds to a straight line, save to lines // The minimum unit of the x-axis and Y-axis in the (rock, theta) coordinate system, that is, the step CV: houghlines (contours, lines, 1, PI/180, 80); STD: vector <CV: vec2f >:: const_iterator it = lines. begin (); STD: cout <lines. size () <STD: Endl; while (it! = Lines. end () {float ROV = (* It) [0]; float Theta = (* It) [1]; If (theta <PI/4. | Theta> 3. * PI/4) {// draw the straight line CV on the upper and lower sides of the intersection point: Point pt1 (records/cos (theta), 0); CV :: point pt2 (rho-result.rows * sin (theta)/cos (theta), result. rows); CV: Line (image, pt1, pt2, CV: Scalar (255), 1);} else {// draw a straight line CV at the intersection of the left and right sides :: point pt1 (0, ROV/sin (theta); CV: Point pt2 (result. cols, (rho-result.cols * Cos (theta)/sin (theta); CV: Line (image, pt1, pt2, CV: Scalar (255), 1 );} + + it;} CV: namedwindow ("Hough"); CV: imshow ("Hough", image); CV: waitkey (0 );}

Sample detection result:

In addition, we can see that the above line detection has the following problems:

1) You can only check the straight line of a line segment without knowing the specific position or length of the line segment;

2) Multiple straight lines may be detected in the same line;

3) a straight line may also be misjudged occasionally.

To address these problems, opencv has a function:

Void houghlinesp (inputarray image, outputarray lines, double rock, double Theta, int threshold, dou-
Ble minlinelength = 0, double maxlinegap = 0)
This method is implemented through probabilistic Hoff Transformation:

1) randomly obtain the foreground points on the edge image and map them to the level coordinate system to draw a curve;

2) When the intersection points in the polar coordinate system reach the minimum number of votes, find the line L corresponding to the x-y coordinate system;

3) Search for the top scenic spots of edge images, connect the vertices on the line L (and the distance between the vertices is smaller than that of maxlinegap) to a line segment, and delete all the vertices, the parameter for recording the line segment is the starting point and ending point ~~~~~~~~~~~~~~~~~~~ (Of course, the length of a line segment must satisfy the minimum length. Otherwise, no record is required)

4) Repeat 1), 2), 3)

For the usage method, see the code:

# Include "opencv2/opencv. HPP "# define PI 3.1415926 class linefinder {PRIVATE: // point parameter vector corresponding to the straight line Std: vector <CV: vec4i> lines; // Step Double deltarho; double deltatheta; // determines the minimum number of votes in a straight line, int minvote; // determines the minimum length of a straight line, double minlength; // The distance between points in a straight line, double maxgap; public: // initialize linefinder (): deltarho (1), deltatheta (PI/180), minvote (10), minlength (0 .), maxgap (0 .) {}// set the step void setaccresolution (double drho, d Ouble dtheta) {deltarho = drho; deltatheta = dtheta;} // set the minimum number of votes void setminvote (int minv) {minvote = MINV ;} // set the minimum length of a line segment and the spacing tolerance of a line segment void setlinelengthandgap (double length, double gap) {minlength = length; maxgap = gap;} // find a line segment STD: vector <CV:: vec4i> findlines (CV: mat & Binary) {lines. clear (); CV: houghlinesp (binary, lines, deltarho, deltatheta, minvote, minlength, maxgap); // return lines;} // draw a line segment voi D drawdetectedlines (CV: mat & image, CV: Scalar color = CV: Scalar (255,255,255) {STD: vector <CV: vec4i> :: const_iterator it2 = lines. begin (); While (it2! = Lines. end () {CV: Point pt1 (* it2) [0], (* it2) [1]); CV: Point pt2 (* it2) [2], (* it2) [3]); CV: Line (image, pt1, pt2, color); ++ it2 ;}}}; int main (INT argc, char * argv []) {CV: mat image = CV: imread ("road.jpg"); CV: mat result; CV :: cvtcolor (image, result, cv_bgra2gray); CV: mat contours; // Edge Detection CV: canny (result, contours, 125,350); linefinder; finder. setminvote (80); finder. setlinelengthandgap (100,20); finder. findlines (contours); finder. drawdetectedlines (image);/* STD: vector <CV: vec2f> lines; // get a set of polar coordinate parameters (rock, theta ), each pair corresponds to a straight line, and is saved to lines // the smallest unit of the 3 or 4 parameters in the (rock, theta) coordinate system, that is, the step CV: houghlines (contours, lines, 1, PI/180,80); STD: vector <CV: vec2f >:: const_iterator it = lines. begin (); STD: cout <lines. size () <STD: Endl; while (it! = Lines. end () {float ROV = (* It) [0]; float Theta = (* It) [1]; If (theta <PI/4. | Theta> 3. * PI/4) {// draw the straight line CV on the upper and lower sides of the intersection point: Point pt1 (records/cos (theta), 0); CV :: point pt2 (rho-result.rows * sin (theta)/cos (theta), result. rows); CV: Line (image, pt1, pt2, CV: Scalar (255), 1);} else {// draw a straight line CV at the intersection of the left and right sides :: point pt1 (0, ROV/sin (theta); CV: Point pt2 (result. cols, (rho-result.cols * Cos (theta)/sin (theta); CV: Line (image, pt1, pt2, CV: Scalar (255), 1 );} + + it;} */CV: namedwindow ("Hough"); CV: imshow ("Hough", image); CV: waitkey (0 );}

The test results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.