OpenCV image recognition from zero to proficient (-----) Hough Transform to detect lines and circles

Source: Internet
Author: User
Tags scalar

Today is to see the Hough transformation, commonly used to detect lines and circles, here is the common Cartesian coordinate system into polar coordinates, the maximum value of cumulative peak, determined. Houghlines,Houghlinesp,houghcircles, three functions, first look at the principle, the final will use a beautiful MATLAB diagram, to return to, Hough linear transformation.

Hough Line Transformation:

  1. As we all know, a straight line in the image of the two-dimensional space can be represented by two variables. For example:

      1. In Cartesian coordinate system: can be represented by parameters: slope and intercept.
      2. In polar coordinates: can be represented by parameters: polar diameter and polar angle

    For the Hough transform, we will use polar coordinates to represent the line. Therefore, the expression for the line can be:

    To simplify:

  2. In general, for points, we can uniformly define a family of lines through this point as:

    This means that each pair represents a straight line through the point.

  3. If for a given point we draw all straight lines through it in polar coordinates to the polar angle plane, we will get a sine curve. For example, for a given point and we can draw (in plane-):

    Only points and that meet the following conditions are plotted.

  4. We can do this for all the points in the image. If two different points do this, the resulting curves in the plane-intersect, which means they pass the same line. For example, following the example above we continue to point to:, and point, Draw, get:

    The three curves intersect at the point in the-plane, and the coordinates represent the straight line in the plane of the parameter pair () or the point, point, and Point.

The OpenCV implements the following three Hough line transformations:
    1. Standard Hough Line transformation, multi-scale Hough transform
  • The principle is explained in the above section. It can give us a set of parameter pairs to represent the detected line.
  • In OpenCV, the function Houghlines is adopted to realize
    1. Statistical Probabilistic Hough Line transformation
  • This is a more efficient Hough line transformation. It outputs the endpoint of the detected line
  • In OpenCV, it is implemented by the function Houghlinesp.
<span style= "FONT-SIZE:18PX;" >c++: void Houghlines (inputarray image, Outputarray lines, double rho, double theta, int threshold, double srn=0, Doubl e stn=0)  </span>

  • The first parameter, the Inputarray type of image, the input images, that is, the source image, a 8-bit single-channel binary image, you can load any of the source map into the function after the change to this format, and then fill in here.
  • The second parameter, the Inputarray type of lines, stores the output vectors of the line transformation detected by the Hough line transform after calling the Houghlines function. Each line is represented by a vector of two elements, which is the distance from the origin of the coordinates ((0,0) (that is, the upper-left corner of the image). is the Radian line rotation angle (0~ vertical, π/2~ horizontal).
  • The third argument, the double type of rho, is the distance precision in pixels. Another way to describe this is the unit radius of the progressive dimension when searching straight lines. Ps:latex in the/rho is said.
  • The fourth argument, the theta of the double type, the angular precision in radians. Another way to describe this is the unit angle of the progressive dimension when searching straight lines.
  • The fifth parameter, the threshold of the int type, is the threshold parameter of the cumulative plane, which is the value that must be reached in the accumulation plane when a part is divided into a straight line in the graph. Segments greater than the threshold threshold can be detected and returned to the results.
  • The sixth argument, a double of type SRN, has a default value of 0. For multi-scale Hough transforms, this is the third parameter of progressive size Rho's divisor distance. The rough accumulator progression dimension is directly the third parameter rho, while the precise accumulator progressive size is RHO/SRN.
  • The seventh parameter, STN of type double, has a default value of 0, and for Multiscale Hough Transforms, SRN represents the divisor distance of the unit angle theta for the fourth parameter progression dimension. And if the SRN and STN are 0 simultaneously, the classical Hough transform is used. Otherwise, both of these parameters should be positive.
<span style= "FONT-SIZE:18PX;" >c++: void Houghlinesp (inputarray image, Outputarray lines, double rho, double theta, int threshold, double Minlineleng Th=0, double maxlinegap=0)  </span>

  • The first parameter, the Inputarray type of image, the input images, that is, the source image, a 8-bit single-channel binary image, you can load any of the source map into the function after the change to this format, and then fill in here.
  • The second argument, the Inputarray type of lines, after calling the HOUGHLINESP function, stores the output vectors of the detected lines, each of which is represented by a vector of four elements (X_1,y_1, x_2, y_2) and (X_1, y_1) and (x_2 , y_2) is the end point of each detected segment.
  • The third argument, the double type of rho, is the distance precision in pixels. Another way to describe this is the unit radius of the progressive dimension when searching straight lines.
  • The fourth argument, the theta of the double type, the angular precision in radians. Another way to describe this is the unit angle of the progressive dimension when searching straight lines.
  • The fifth parameter, the threshold of the int type, is the threshold parameter of the cumulative plane, which is the value that must be reached in the accumulation plane when a part is divided into a straight line in the graph. Segments greater than the threshold threshold can be detected and returned to the results.
  • The sixth parameter, the double type of minlinelength, has a default value of 0, which indicates the length of the lowest segment, which is less visible than the shorter segment of the set parameter.
  • The seventh parameter, double of type Maxlinegap, has a default value of 0, which allows the maximum distance to be connected between the same line of points and points.
combine the above 2 functions to see how to detect a straight line

<span style= "FONT-SIZE:18PX;" > #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using  namespace CV;    using namespace Std;      int main (int argc, char** argv) {const char* filename = argc >= 2? Argv[1]: "Lena.jpg";      Mat src = imread (filename, 0);          if (Src.empty ()) {Help ();          cout << "Can not open" << filename << Endl;      return-1;      } Mat DST, CDST;      Canny (SRC, DST, 50, 200, 3);  Cvtcolor (DST, CDST, CV_GRAY2BGR);      #if 0 vector<vec2f> Lines;      Houghlines (DST, lines, 1, cv_pi/180, 100, 0, 0);          for (size_t i = 0; i < lines.size (); i++) {Float rho = lines[i][0], theta = lines[i][1];          Point Pt1, Pt2;          Double A = cos (theta), B = sin (theta);          Double x0 = a*rho, y0 = B*rho;          pt1.x = Cvround (x0 + 1000* (-B));          Pt1.y = Cvround (y0 + 1000* (a)); pt2.x = Cvround (x0-1000* (-B));          Pt2.y = Cvround (y0-1000* (a));      Line (CDST, pt1, Pt2, Scalar (0,0,255), 3, CV_AA);      } #else vector<vec4i> lines;      Houghlinesp (DST, lines, 1, cv_pi/180, 50, 50, 10);          for (size_t i = 0; i < lines.size (); i++) {vec4i L = lines[i];      Line (CDST, point (L[0], l[1]), point (L[2], l[3]), Scalar (0,0,255), 3, CV_AA);      } #endif imshow ("source", SRC);        Imshow ("Detected lines", CDST);        Waitkey ();  return 0; } </span>


<span style= "FONT-SIZE:18PX;" > #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/  Imgproc.hpp> using namespace std;  using namespace CV;  Mat g_srcimage, g_dstimage,g_midimage;vector<vec4i> g_lines;int g_nthreshold=100;      static void On_houghlines (int, void*); int main () {Mat g_srcimage = Imread ("lena.jpg");        Imshow ("The original Figure" ", g_srcimage);      Namedwindow ("" ", 1);      Createtrackbar ("Value", "" "", "&g_nthreshold,200,on_houghlines");    Canny (G_srcimage, G_midimage, 50, 200, 3);    Cvtcolor (G_midimage,g_dstimage, CV_GRAY2BGR);      On_houghlines (g_nthreshold,0);      Houghlinesp (G_midimage, G_lines, 1, cv_pi/180, 80, 50, 10);        Imshow ("" "", g_dstimage);        Waitkey (0);      return 0;       } static void On_houghlines (int, void*) {Mat dstimage=g_dstimage.clone ();       Mat Midimage=g_midimage.clone ();      Vector<vec4i> Mylines; Houghlinesp (Midimage, Mylines, 1, CV_PI/180, G_nthreshold+1, 50, 10);          for (size_t i = 0; i < mylines.size (); i++) {vec4i L = mylines[i];      Line (Dstimage, point (L[0], l[1]), point (L[2], l[3]), Scalar (23,180,55), 1, CV_AA);  } imshow ("" "", dstimage); } </span>

The principle of the Hough Circle transformation and the above-mentioned Hough line changes are largely similar, but the point corresponding to the two-dimensional polar angle space is three-dimensional center point x, Y and radius r space Replace

<span style= "FONT-SIZE:18PX;" >c++: void Houghcircles (Inputarray image,outputarray circles, int method, double DP, double mindist, double param1=100, Double param2=100, int minradius=0, int maxradius=0)  </span>

  • The first parameter, the Inputarray type of image, is the 8-bit grayscale single-channel image, which is the source image.
  • The second argument, the Inputarray type of circles, after calling the Houghcircles function, stores the output vectors of the detected circles, each of which is represented by a floating-point vector (x, y, radius) that contains 3 elements.
  • The third parameter, the type of int method, that is, the use of detection methods, the current OPENCV in the Hough gradient method can be used, its identifier is cv_hough_gradient, this parameter is filled with this identifier.
  • The fourth parameter, a double of type DP, is used to detect the inverse of the ratio of the image of the accumulator in the center of the input image, and this parameter allows the creation of an accumulator with a lower resolution than the input image. If the above words are difficult to understand, let's look at examples. For example, if dp= 1 o'clock, the accumulator and the input image have the same resolution. If dp=2, the accumulator has a width and height that is half as large as the input image.
  • The fifth parameter, the mindist of the double type, is the minimum distance between the center of the circle that the Hough transform detects, that is, the minimum distance between the two different circles that allow our algorithm to differentiate clearly. If this parameter is too small, multiple adjacent circles may be incorrectly detected as a coincident circle. Conversely, if this parameter is set too large, some circles cannot be detected.
  • The sixth argument, a double of type param1, has a default value of 100. It is the corresponding parameter of the detection method set by the third parameter method. The current only method cv_hough_gradient the Hough gradient method, which represents the high threshold value passed to the canny edge detection operator, while the low threshold is half the high threshold value.
  • The seventh argument, a double of type param2, also has a default value of 100. It is the corresponding parameter of the detection method set by the third parameter method. The current only method cv_hough_gradient the Hough gradient method, which represents the accumulator threshold at the center of the detection phase. The smaller it is, the more it can detect a circle that doesn't exist, and the bigger it is, the more rounded it is to be able to pass through the circle.
  • The eighth parameter, Minradius of type int, has a default value of 0, which represents the minimum value of the circle radius.
  • The Nineth parameter, Maxradius of type int, also has a default value of 0, which represents the maximum value of the circle radius.

All circles of the over point (X1,Y1) can be expressed as (A1 (i), B1 (i), R1 (i)), all circles over points (x2,y2) can be expressed as (A2 (i), B2 (i), R2 (i)), and all circles over points (x3,y3) can be expressed as (A3 (i), B3 (i), R3 ( i)), if these three points are on the same circle, then there is a value (A0,B0,R0), which makes a0 = A1 (k) =a2 (k) =a3 (k) and B0 = B1 (k) =b2 (k) =b3 (k) and r0 = R1 (k) =r2 (k) =r3 (k), That is, these three points are at the same time on a circle (A0,B0,R0).
Can be seen from the image:

first, all the circles (x1,y1) are analyzed (A1 (i), B1 (i), R1 (i)), and when the R1 (i) is determined, the trajectory (A1 (i), B1 (i)) is a circle (X1,Y1,R1 (i)) with a center radius of R1 (i). Then, all the circles (A1 (i), B1 (i), R1 (i)) consist of a conical face with a vertex (x1,y1,0) and a cone angle of 90 degrees.
Intersection A of three conical faces is a circle that crosses these three points simultaneously.
<span Style= "FONT-SIZE:18PX;"  > #include <opencv2/opencv.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace CV;      int main () {Mat srcimage = Imread ("1.png");     Mat Midimage,dstimage;        Imshow ("The original Figure" ", srcimage);    Cvtcolor (Srcimage,midimage, Cv_bgr2gray);      Gaussianblur (Midimage, Midimage, Size (9, 9), 2, 2);      Vector<vec3f> circles;       Houghcircles (Midimage, circles, cv_hough_gradient,1.5, 10, 200, 100, 0, 0); for (size_t i = 0; i < circles.size (); i++) {Point Center (cvround (circles[i][0)), Cvround (Circles[i][1]          ));           int radius = Cvround (circles[i][2]);          Circle (srcimage, center, 3, Scalar (0,255,0),-1, 8, 0);      Circle (srcimage, center, RADIUS, Scalar (155,50,255), 3, 8, 0);        } imshow ("" "", srcimage);        Waitkey (0);    return 0; } </span> 



Matlab

<span style= "FONT-SIZE:18PX;" >i = Imread (' circuit.tif '); RotI = Imrotate (i,33, ' crop '); Figureimshow (RotI, []) BW = Edge (RotI, ' canny ');            [H,t,r] = Hough (BW, ' rhoresolution ', 0.5, ' thetaresolution ', 0.5); Figureimshow (h,[], ' XData ', T, ' Ydata ', R,... ' Initialmagnification ', ' fit '), Xlabel (' Theta '), Ylabel (' Rho '), axis on, axis normal, hold On;colormap (hot) P = Houghpeaks (h,5, ' threshold ', ceil (0.3*max (H (:)))); x = T (P (:, 2)); y = R (P (:, 1));p lot (x, y, ' s ', ' color ', ' white ');% Find lines and plot Themlines = Houghlines (bw,t,r,p, ' Fillgap ', 5, ' MinLength ', 7); figure, Imshow (RotI), hold Onmax_len = 0;for k = 1:length (lin   ES) xy = [lines (k). Point1; lines (k). Point2];    Plot (XY (:, 1), XY (:, 2), ' LineWidth ', 2, ' Color ', ' green ');   % plot beginnings and ends of lines plot (XY (n), XY (+), ' x ', ' LineWidth ', 2, ' Color ', ' yellow ');    Plot (XY (2,1), XY (2,2), ' x ', ' LineWidth ', 2, ' Color ', ' red ');   % determine the endpoints of the longest line segment len = Norm (lines (k). Point1-lines (k). Point2); if (Len > Max_len)      Max_len = Len;   Xy_long = XY; EndEnd% highlight the longest line Segmentplot (Xy_long (:, 1), Xy_long (:, 2), ' LineWidth ', 2, ' Color ', ' Blue '); </span >
The picture on the left is the Hough transform and the right is the marker line result

OpenCV image recognition from zero to proficient (-----) Hough Transform to detect lines and circles

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.