PYTHON+OPENCV image Processing (14)--line detection

Source: Internet
Author: User
Tags cos function prototype sin

Brief introduction:

1. Hough transform (Hough Transform) Hough Transform is one of the basic methods to recognize geometric shapes from images in image processing, which is widely used and has many improved algorithms. It is mainly used to isolate geometric shapes (such as lines, circles, etc.) that have some of the same characteristics from the image. The most basic Hough transform is to detect straight lines (line segments) from black and white images.

The principle of the 2.Hough transformation is to transform the point on a particular graph into a set of parameter spaces, according to the cumulative results of the parameter space points to find a maximum value corresponding to the solution, then the solution corresponds to the geometric shape of the parameter (such as a straight line, then you will get the slope of the straight line K and Changshu B, Circle will get center and radius, etc.)

3. The Hough Line transformation is a way to find a straight line. Before using Hough Line transformation, the first step is to deal with the image edge detection, that is, the direct input of Hough Line transformation can only be the edge binary image.

4. The specific principles of the Hough Line detection refer to:

52944708

Http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html

http://lib.csdn.net/article/opencv/24201

The code is as follows:

#Line Detection#using Hough linear transformation to do straight line detection, precondition: edge detection has been completedImportCv2 as CVImportNumPy as NP#Standard Hough Line Transformationdefline_detection (image): Gray=Cv.cvtcolor (image, CV. Color_rgb2gray) Edges= CV. Canny (Gray, Max, aperturesize=3)#aperturesize parameter default is actually 3Cv.imshow ("edges", edges) lines= CV. Houghlines (edges, 1, np.pi/180, 80)     forLineinchLines:rho, Theta= Line[0]#Line[0] Stores the polar and polar angles of a point to a line, where the polar angle is in radians. A = Np.cos (theta)#Theta is Radianb =Np.sin (theta) x0= A * Rho#represents x = R * cos (theta)y0 = b * Rho#represents y = R * sin (theta)x1 = Int (x0 + $ * (-B))#Calculate line Start horizontaly1 = Int (y0 + $ * a)#calculate starting start ordinatex2 = Int (x0-1000 * (-B))#Calculate the end of a liney2 = Int (y0-1000 * a)#calculate the line end Ordinate note: Here the value of 1000 gives the length range of the drawn segment, the smaller the number, the shorter the line, the larger the number, the longer the line is drawn.Cv.line (image, (x1, y1), (x2, y2), (0, 0, 255), 2)#the coordinates of a point must be a tuple, not a list. Cv.imshow ("Image-lines", image)#Statistical probabilistic Hough Line TransformationdefLine_detect_possible_demo (image): Gray=Cv.cvtcolor (image, CV. Color_rgb2gray) Edges= CV. Canny (Gray, Max, aperturesize=3)#aperturesize parameter default is actually 3Lines = CV. Houghlinesp (edges, 1, np.pi/180, minlinelength=60, maxlinegap=5)     forLineinchlines:x1, y1, x2, y2=Line[0] Cv.line (image, (x1, y1), (x2, y2), (0, 0,255), 2) Cv.imshow ("Line_detect_possible_demo", image) src= Cv.imread ('e:/imageload/louti.jpg')Print(Src.shape) Cv.namedwindow ('Input_image', CV. Window_autosize) Cv.imshow ('Input_image', SRC) line_detection (src) src= Cv.imread ('e:/imageload/louti.jpg')#after calling the previous function, the incoming SRC array is changed, so the next function is called to re-read the pictureLine_detect_possible_demo (SRC) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

The Houghlines function of 1.OPENCV is a standard Hough line transformation function whose function is to represent the detected line through a set of parameter pairs whose function prototype is: houghlines (image, Rho, Theta, threshold[, lines[ , srn[, stn[, min_theta[, Max_theta]]), lines

The image parameter represents the output image of the edge detection, which is a single-channel, 8-bit binary image.

The Rho parameter represents the resolution of the parameter polar diameter in pixels, which is typically used here in 1 pixels.

The theta parameter represents the resolution of the parameter polar angle in radians, which is used 1 degrees.

The threshold parameter represents the minimum curve intersection that is required to detect a line.

The lines parameter represents a container that stores the parameter pairs of detected lines.

The SRN parameter, the STN parameter, is 0 by default. If SRN = 0 and STN = 0, the classic Hough transform is used.

The Min_theta parameter indicates the minimum angle of the line for the standard and Multiscale Hough transformations.

The Max_theta parameter indicates the maximum angle of the line for the standard and Multiscale Hough transformations.

The HOUGHLINESP function of 2.OPENCV is a statistical probabilistic Hough line transformation function that outputs the endpoint of the detected line, whose function prototype is: HOUGHLINESP (image, Rho, Theta, threshold[, lines[, minlinelength[, Maxlinegap]]), lines

The image parameter represents the output image of the edge detection, which is a single-channel, 8-bit binary image.

The Rho parameter represents the resolution of the parameter polar diameter in pixels, which is typically used here in 1 pixels.

The theta parameter represents the resolution of the parameter polar angle in radians, which is used 1 degrees.

The threshold parameter represents the minimum curve intersection that is required to detect a line.

The lines parameter represents the container that stores the parameter pairs of the detected line, that is, the coordinates of the two endpoints of the segment.

The Minlinelength parameter represents the minimum number of points that can form a straight line, and a line with an insufficient number of points is discarded.

The Maxlinegap parameter represents the maximum distance that can be considered as a highlight on a straight line.

PYTHON+OPENCV image Processing (14)--line detection

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.