Python Digital Image Processing-detailed explanation of the implementation of Hov Line Transformation, python Digital Image Processing

Source: Internet
Author: User

Python Digital Image Processing-detailed explanation of the implementation of Hov Line Transformation, python Digital Image Processing

In image processing, the HOF transform is mainly used to detect the geometric shapes in the image, including straight lines, circles, and edges.

In skimage, the HOF transformation is placed in the tranform module. This article mainly describes the HOF line transformation.

For a straight line in a plane, y = mx + B can be used in the Cartesian coordinate system. m is the slope, and B is the intercept. However, if a straight line is a vertical line, m is infinite. Generally, we represent a straight line in another coordinate system, that is, r = xcos (theta) + ysin (theta) in the polar coordinate system ). You can use (r, theta) to represent a straight line. Here, r is the distance from the straight line to the origin, and theta is the angle between the vertical line of the straight line and the X axis. As shown in.

For a given vertex (x0, y0), we plot all the straight lines (r, theta) that pass through it in the polar coordinate to produce a sine curve. If all the non-zero sine curves in the image are drawn, there will be some intersections. All the sine curves that pass through this intersection point indicate that they all have the same (r, theta), meaning that these points are on a straight line.

As shown in the following figure, the three points (corresponding to the three sine curves in the figure) are in a straight line, because these three curves are handed over to one point and have the same (r, theta ). This method is used to find the straight line in the graph.

Function: skimage. transform. hough_line (img)

Three values are returned:

H: Hov transform Accumulators

Theta: a set of angle between a point and the X axis, generally 0-degrees.

Distance: the distance from the point to the origin, that is, the r.

Example:

Import skimage. transform as stimport numpy as npimport matplotlib. pyplot as plt # Build Test image = np. zeros (100,100) # background image idx = np. arange (25, 75) #25-74 sequence image [idx [:-1], idx] = 255 # Line \ image [idx, idx] = 255 # Line/# Line Transformation h, theta, d = st. hough_line (image) # generate a window with one row and two columns (two images can be displayed ). fig, (ax0, ax1) = plt. subplots (1, 2, figsize = (8, 6) plt. tight_layout () # display the original image ax0.imshow (image, plt. cm. gray) ax0.set _ title ('input image') ax0.set _ axis_off () # display ax1.imshow (np. log (1 + h) ax1.set _ title ('hough transform') ax1.set _ xlabel ('angles (degrees) ') ax1.set _ ylabel ('distance (pixels )') ax1.axis ('image ')

The picture on the right shows that there are two intersections, indicating that there are two straight lines in the original image.

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

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

This function can be used to retrieve the peak point, that is, the intersection point, that is, the straight line in the source image.

The returned parameters are the same as the input parameters. Let's modify the above program and draw two straight lines in the source image.

Import skimage. transform as stimport numpy as npimport matplotlib. pyplot as plt # Build Test image = np. zeros (100,100) # background image idx = np. arange (25, 75) #25-74 sequence image [idx [:-1], idx] = 255 # Line \ image [idx, idx] = 255 # Line/# Line Transformation h, theta, d = st. hough_line (image) # generate a window with one row and three columns (three images can be displayed ). fig, (ax0, ax1, ax2) = plt. subplots (1, 3, figsize = (8, 6) plt. tight_layout () # display the original image ax0.imshow (image, plt. cm. gray) ax0.set _ title ('input image') ax0.set _ axis_off () # display ax1.imshow (np. log (1 + h) ax1.set _ title ('hough transform') ax1.set _ xlabel ('angles (degrees) ') ax1.set _ ylabel ('distance (pixels )') ax1.axis ('image') # displays the detected lines ax2.imshow (image, plt. cm. gray) row1, col1 = image. shapefor _, angle, dist in zip (* 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: to draw a line, convert it from polar coordinate to Cartesian coordinate. The formula is as follows:

Skimage also provides another Hof transform function for detecting a straight line. The probability Hof line transformation is as follows:

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

Parameters:

Img: The image to be detected.

Threshold: threshold value, which can be set first. The default value is 10.

Line_length: Minimum Line Length detected. The default value is 50.

Line_gap: The maximum gap between lines. Increasing this value can combine broken lines. The default value is 10.

Return Value:

Lines: Line list, in the format of (x0, y0), (x1, y0), indicating the start point and end point.

Next, we will extract the edge by using the 'EC' operator and then detect which edge is a straight line?

Import skimage. transform as stimport matplotlib. pyplot as pltfrom skimage import data, 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) # create a display window. fig, (ax0, ax1, ax2) = plt. subplots (1, 3, figsize = (16, 6) plt. tight_layout () # display the original image ax0.imshow (image, plt. cm. gray) ax0.set _ title ('input image') ax0.set _ axis_off () # display the ctan edge ax1.imshow (edges, plt. cm. gray) ax1.set _ title ('canny edges ') ax1.set _ axis_off () # Use plot to draw all straight lines ax2.imshow (edges * 0) for line in lines: 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 ()

Summary

The above is all the details about how to implement the Line Transformation of Python digital image processing, and I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.