OpenCV notes (17)--Detecting lines using Hough transform

Source: Internet
Author: User

In general, when we detect the contour of the object, there will be a need to detect the line, then you can use the OPENCV in the Hough transform.

A brief description of the principle of Hough transform see: Http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html#hough-lines

To put it simply, for a single pixel, it could be a lot of straight lines, and we can construct countless straight lines with one point.

For a pixel position (x, y), from Cartesian coordinates and polar coordinates two angles, we can have

And then get

The upper-order (x, y) is an argument, R and Theta are dependent variables.

So for one (x, y), we have countless r and theta corresponding to it, see

For multiple (x, y), we draw their R and Theta:

Because their R curves have intersections, they can be theta by the same line at an angle, or understood to intersect their r-theta curves.

Let's add a limit to these points: they are adjacent points.

On the image, they are a straight line consisting of several adjacent points.

In OpenCV, the Hough transform is implemented in two ways: Houghlines () and Houghlinesp (). The latter is a more effective implementation.

void houghlines(inputarray image, Outputarray lines, double rho, double Theta, int threshold, double srn=0, double stn=0 )

void houghlinesp(inputarray image, Outputarray lines, double Rho , double theta, int threshold, double minlinelength=0, double maxlinegap =0 )

Images is a 8-bit single-channel graph, typically the output of the canny method (this is a contour map)

Lines is the line set of the output, typically the vector<vec2f> type.

Rho is generally 1. Not clear.

Theta is generally cv_pi/180. Not clear.

The threshold is the minimum number of intersecting bars of the R-theta curve we have described above, and it can also be understood as the least pixels that make up a line.

Paste a section of the OPENCV code using the Hough transform, you can see the use of houghlinesp more convenient:

1 voidStandard_hough (int,void* )2 {3Vector<vec2f>S_lines;4 Cvtcolor (edges, Standard_hough, CV_GRAY2BGR);5 6   ///1. Use standard Hough Transform7Houghlines (edges, S_lines,1, cv_pi/ the, Min_threshold + S_trackbar,0,0 );8 9   ///Show The resultTen    for(size_t i =0; I < s_lines.size (); i++ ) One      { A       floatr = s_lines[i][0], T = s_lines[i][1]; -       Doublecos_t = cos (t), sin_t =sin (t); -       Doublex0 = r*cos_t, y0 = r*sin_t; the       DoubleAlpha = +; -  -Point Pt1 (Cvround (x0 + alpha* (-sin_t)), Cvround (y0 + alpha*cos_t)); -Point Pt2 (Cvround (x0-alpha* (-sin_t)), Cvround (y0-alpha*cos_t)); +Line (Standard_hough, pt1, Pt2, Scalar (255,0,0),3, CV_AA); -      } +  A imshow (Standard_name, standard_hough); at } -  - /** - * @function Probabilistic_hough -  */ - voidProbabilistic_hough (int,void* ) in { -Vector<vec4i>P_lines; to Cvtcolor (edges, Probabilistic_hough, CV_GRAY2BGR); +  -   ///2. Use probabilistic Hough Transform theHoughlinesp (edges, P_lines,1, cv_pi/ the, Min_threshold + P_trackbar, -,Ten ); *  $   ///Show The resultPanax Notoginseng    for(size_t i =0; I < p_lines.size (); i++ ) -      { theVec4i L =P_lines[i]; +Line (Probabilistic_hough, point (l[0], l[1]), point (l[2], l[3]), Scalar (255,0,0),3, CV_AA); A      } the  + imshow (Probabilistic_name, probabilistic_hough); -}
View Code

-

OpenCV notes (17)--Detecting lines using Hough transform

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.