Python Digital Image processing (15): Hough Line transformation

Source: Internet
Author: User
Tags cos sin

In the image processing, the Hough transform is mainly used to detect the geometric shapes in the image, including lines, circles, ellipses and so on.

In Skimage, the Hough transform is placed in the Tranform module, this chapter mainly explains the Hough line transformation.

For a line in a plane, in a Cartesian coordinate system, Y=MX+B is used, where m is the slope and B is the Intercept. But if the line is a vertical line, M is infinity, and all of us usually represent a line in another coordinate system, that is, R=xcos (theta) +ysin (theta) in polar coordinates. You can use (R,theta) to represent a straight line. where R is the distance from the line to the origin, Theta is the angle between the perpendicular of the line and the x-axis. As shown in.

For a given point (x0,y0), we draw all the straight lines (R,theta) through it in polar coordinates and we get a sine curve. If you draw all the sinusoidal curves that are not 0 points in the picture, there are some intersections. All sine curves passing through this intersection have the same meaning (R,theta), which means that the points are in a straight line.

As shown, three points (three sine curves in the corresponding graph) are in a straight line because the three curves are at a point and have the same (R, Theta). The Hough Line transformation is to use this method to find the straight line in the graph.

Function:skimage.transform. Hough_line (img)

Returns a value of three:

H: Hough Transform cumulants

Theta: A set of angles between points and X-axes, typically 0-179 degrees

Distance: The distance from the point to the origin, that is, the above mentioned R.

Cases:

ImportSkimage.transform as StImportNumPy as NPImportMatplotlib.pyplot as Plt#build a test pictureImage = Np.zeros ((100, 100))#Background MapIDX = Np.arange (25, 75)#25-74 SequenceIMAGE[IDX[::-1], idx] = 255#lines \IMAGE[IDX, idx] = 255#Lines/#Hough Line TransformationH, theta, d =st.hough_line (image)#generates a two-row window (two pictures can be displayed).Fig, (ax0, ax1) = Plt.subplots (1, 2, figsize= (8, 6) ) plt.tight_layout ()#Show original pictureax0.imshow (image, Plt.cm.gray) Ax0.set_title ('Input Image') Ax0.set_axis_off ()#Show Hough Transform DataAx1.imshow (Np.log (1 +h)) Ax1.set_title ('Hough Transform') Ax1.set_xlabel ('Angles (degrees)') Ax1.set_ylabel ('Distance (pixels)') Ax1.axis ('Image')

As you can see from the graph on the right, there are two intersections, indicating that there are two straight lines in the original image.

If we want to draw two lines in the diagram, we need to use another function:

Skimage.transform. Hough_line_peaks (hspace, angles, dists)

Use this function to remove the peak point, the intersection, or the line in the original.

The parameter returned is the same as the input parameter. We modify the above program to draw two lines in the original image.

ImportSkimage.transform as StImportNumPy as NPImportMatplotlib.pyplot as Plt#build a test pictureImage = Np.zeros ((100, 100))#Background MapIDX = Np.arange (25, 75)#25-74 SequenceIMAGE[IDX[::-1], idx] = 255#lines \IMAGE[IDX, idx] = 255#Lines/#Hough Line TransformationH, theta, d =st.hough_line (image)#generates a three-row window (showing three pictures).Fig, (ax0, ax1,ax2) = Plt.subplots (1, 3, figsize= (8, 6) ) plt.tight_layout ()#Show original pictureax0.imshow (image, Plt.cm.gray) Ax0.set_title ('Input Image') Ax0.set_axis_off ()#Show Hough Transform DataAx1.imshow (Np.log (1 +h)) Ax1.set_title ('Hough Transform') Ax1.set_xlabel ('Angles (degrees)') Ax1.set_ylabel ('Distance (pixels)') Ax1.axis ('Image')#show the detected linesax2.imshow (image, Plt.cm.gray) Row1, col1=Image.shape for_, Angle, distinchZip (*st.hough_line_peaks (H, Theta, D): Y0= (dist-0 * Np.cos (angle))/np.sin (angle) y1= (Dist-col1 * Np.cos (angle))/np.sin (angle) Ax2.plot ((0, col1), (y0, y1),'- R') Ax2.axis ((0, col1, row1, 0)) Ax2.set_title ('detected lines') Ax2.set_axis_off ()

Note that when you draw a line, you convert from polar coordinates to Cartesian coordinates, and the formula is:

Skimage also provides another Hough transform function for detecting lines, the probability Hough line transformation:

Skimage.transform. probabilistic_hough_line (IMG, threshold=10, line_length=5,line_gap=3)

Parameters:

IMG: The image to be detected.

Threshold: Threshold value, first entry, default = 10

Line_length: The shortest line length detected, default is 50

Line_gap: The maximum gap between lines. Increase this value to merge broken lines. Default is 10

Return:

Lines: A list of lines, formatted as ((x0, y0), (x1, y0)), indicating the start and end points .

Below, we use the canny operator to extract the edges and then detect which edges are straight lines?

ImportSkimage.transform as StImportMatplotlib.pyplot as Plt fromSkimageImportdata,feature#use probabilistic Hough Transform.Image =Data.camera () edges= Feature.canny (image, sigma=2, low_threshold=1, high_threshold=25) Lines= St.probabilistic_hough_line (edges, threshold=10, line_length=5,line_gap=3)#creates a display window.Fig, (ax0, ax1, ax2) = Plt.subplots (1, 3, figsize= (16, 6) ) plt.tight_layout ()#Show Original Imageax0.imshow (image, Plt.cm.gray) Ax0.set_title ('Input Image') Ax0.set_axis_off ()#Show Canny edgesax1.imshow (edges, Plt.cm.gray) ax1.set_title ('Canny Edges') Ax1.set_axis_off ()#Plot all the lines with plotAx2.imshow (Edges *0) forLineinchLines:p0, p1=Line Ax2.plot ((p0[0], p1[0]), (p0[1], p1[1])) Row2, col2=Image.shapeax2.axis ((0, col2, row2, 0)) Ax2.set_title ('Probabilistic Hough') Ax2.set_axis_off () plt.show ()

Python Digital Image processing (15): Hough Line transformation

Related Article

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.