[Opencv2] Line-based hough Transformation and opencv2hough Transformation
In opencv2, the line-based Hough transformation has the following two forms:
Standard Hov Line Transformation (HoughLines)
The principle has already been discussed in the blog here. This function provides us with a set of parameter pairs (θ, r θ) to represent the detected straight line.
Statistical Probability Hof Line Transformation (HoughLinesP)
This is the HOF Line Transformation with higher execution efficiency. It outputs the endpoints of the detected straight line (x0, y0, xn, xn)
Let's look at the program first. Next we will talk about the usage of each function:
# Include "opencv2/highgui. hpp "# include" opencv2/imgproc. hpp "# include <iostream> using namespace cv; using namespace std; int main (int argc, char ** argv) {string imageName =" 1.jpg"; Mat src, dst, color_dst; src = imread ("1.jpg", 0); // read the image, and use the gray-scale mode of" Canny (src, dst, 50,200, 3 ); // cvtColor (dst, color_dst, CV_GRAY2BGR) for Edge Detection by using the canny operator; // converts the grayscale image of a single channel to a 3-to-3 image, it is used to draw a line with a color paint brush # if 0 // The standard Hough transform vector <Vec2f> lines; HoughLines (dst, lines, 1, CV_PI/180,100); for (size_t I = 0; I <lines. size (); I ++) {float ROV = lines [I] [0]; float theta = lines [I] [1]; double a = cos (theta ), B = sin (theta); double x0 = a * rock, y0 = B * rock; Point pt1 (cvRound (x0 + 1000 * (-B )), cvRound (y0 + 1000 * (a); Point pt2 (cvRound (x0-1000 * (-B), cvRound (y0-1000 * ())); line (color_dst, pt1, pt2, Scalar (255,), 3, 8) ;}# else // calculate the probability of the Hough Transformation vector <Vec4i> lines; HoughLinesP (dst, lines, 1, CV_PI/180,200, 30, 10); for (size_t I = 0; I <lines. size (); I ++) {line (color_dst, Point (lines [I] [0], lines [I] [1]), point (lines [I] [2], lines [I] [3]), Scalar (0, 0,255), 1, 8) ;}# endifnamedWindow ("Source ", 1); imshow ("Source", src); namedWindow ("Detected Lines", 1); imshow ("Detected Lines", color_dst); waitKey (0 ); return 0 ;}
This is the parameter explanation provided in the opencv manual. Some special symbols cannot be typed. For details, see here:
Standard hough functions:
Void HoughLines (InputArray image, OutputArray lines, double rock, double theta, int threshold, double srn = 0, double stn = 0)
· Image-8-bit, single-channel binary source image. The image may be modified by the function.
· Lines-Output vector of lines. Each line is represented by a two-element vector (p, θ)
P is the distance from the coordinate origin (topleft corner of the image). θ is the line rotation angle in radians (0, pi ).
· Rho-distance resolution of the accumulator in pixels.
· Theta-Angle resolution of the accumulator in radians.
· Threshold-Accumulator threshold parameter. Only those lines are returned that get enough votes.
· Srn-For the multi-scale Hough transform, it is a divisor for the distance resolution ROV. the coarse accumulator distance resolution is Company and the accurate accumulator resolution is company and srn. if both srn = 0 and stn = 0, the classical Hough transform is used. otherwise, both these parameters shoshould be positive.
· Stn-For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
My translation and understanding are as follows:
Dst: output image of edge detection. It should be a grayscale image (but actually a binarization image)
Lines: the container that stores the parameter pairs of the detected straight lines (p, θ)
The maximum diameter of the parameter is the resolution measured in pixels. The program uses 1 pixel.
Theta: the resolution of the polar angle θ in radians. We use 1 degree (CV_PI/180)
Threshold: confirm the minimum cumulative value of the accumulators in a straight line. That is, the line corresponding to the parameter is a straight line after the accumulated number of reshold is exceeded.
Srn and stn are a set of additional parameters used for multi-scale hough transformation. When both of these parameters are 0, the standard hough algorithm is executed.
Calculate the probability:
Void HoughLinesP (InputArray image, OutputArray lines, double rock, double theta, int threshold, double minLineLength = 0, double maxLineGap = 0)
The preceding parameters are the same as those of the standard. Only the last two parameters are different.
The following is an explanation in the opencv manual:
MinLineLength-Minimum line length. Line segments shorter than that is rejected.
MaxLineGap-Maximum allowed gap between points on the same line to link them.
My translation and understanding are as follows:
MinLinLength: minimum length of a straight line. That is, when the length of the detected line is greater than minLinLength, this is considered a straight line.
MaxLineGap: Maximum number of gaps (Interruptions) in a straight line. That is, if a straight line is disconnected from somewhere in the middle, the maximum length of the gap is allowed.
Adjusting the above two parameters can reduce the interference line in the image.
The source image is given below:
Detected:
The specific effect depends on the selection of parameters.