Detailed explanation of codes for Python Implementation of HOV and elliptic transformation, and python HOV

Source: Internet
Author: User

Detailed explanation of codes for Python Implementation of HOV and elliptic transformation, and python HOV

In polar coordinates, the circle is expressed:

X = x0 + rcos θ

Y = y0 + rsin θ

The center of the circle is (x0, y0), r is the radius, θ is the degree of rotation, and the value range is 0-359.

If the center and radius are given, whether the other points are in the circle can be detected. In the image, we take each non-zero pixel as the center point and check it with a certain radius. If there is a point on the circle, we will accumulate the center once. If a circle is detected, the center of the circle is accumulated to the maximum and becomes the peak value. Therefore, in the detection result, a peak point corresponds to a center point.

Functions of Hov Circle Detection:

skimage.transform.hough_circle(image, radius)

Radius is an array that represents a set of radius, such as [3, 4, 5, 6].

Returns a three-dimensional array (radius index, M, N). The first dimension represents the radius index, and the last two dimensions represent the image size.

Example 1: draw two circles, and use the hohoff circle transformation to detect them.

Import numpy as npimport matplotlib. pyplot as pltfrom skimage import draw, transform, featureimg = np. zeros (250,250, 3), dtype = np. uint8) rr, cc = draw. circle_perimeter (60, 60, 50) # draw a circle rr1, PC3 = draw with a radius of 50. circle_perimeter (150,150, 60) # draw an img [cc, rr,:] = 255img [, rr1,:] = 255fig, (ax0, ax1) with a radius of 60) = plt. subplots (, figsize = (8, 5) ax0.imshow (img) # display the original image ax0.set _ title ('origin image') hough_radii = np. arange (50, 80, 5) # Radius Range: hough_res = transform. hough_circle (img [:,:, 0], hough_radii) # Circle conversion centers = [] # Save the coordinates of all center points accums = [] # cumulative value radii = [] # radius for radius, h in zip (hough_radii, hough_res ): # obtain the num_peaks = 2 peaks = feature for each radius value. peak_local_max (h, num_peaks = num_peaks) # retrieve the peak value of centers. extend (peaks) accums. extend (h [peaks [:, 0], peaks [:, 1]) radii. extend ([radius] * num_peaks) # Draw the closest circle image = np. copy (img) for idx in np. argsort (accums) [:-1] [: 2]: center_x, center_y = centers [idx] radius = radii [idx] cx, cy = draw. circle_perimeter (center_y, center_x, radius) image [cy, cx] = (255, 0, 0) ax1.imshow (image) ax1.set _ title ('detected image ')

The result is as follows: the circle in the source image is drawn in white, and the detected circle is drawn in red.

Example 2: detected coins.

Import numpy as npimport matplotlib. pyplot as pltfrom skimage import data, color, draw, transform, feature, utilimage = util. img_as_ubyte (data. coins () [0: 95, 70: 370]) # crop the original image edges = feature. canny (image, sigma = 3, low_threshold = 10, high_threshold = 50) # detect the secondary edge fig, (ax0, ax1) = plt. subplots (1, 2, figsize = (8, 5) ax0.imshow (edges, cmap = plt. cm. gray) # display the 'original iamge' hough_radii = np. arange (15, 30, 2) # Radius Range: hough_res = transform. hough_circle (edges, hough_radii) # Circle transformation centers = [] # Save the central point coordinate accums = [] # cumulative value radii = [] # radius for radius, h in zip (hough_radii, hough_res): # For each radius value, obtain the num_peaks = 2 peaks = feature. peak_local_max (h, num_peaks = num_peaks) # retrieve the peak value of centers. extend (peaks) accums. extend (h [peaks [:, 0], peaks [:, 1]) radii. extend ([radius] * num_peaks) # Draw the nearest five circles image = color. gray2rgb (image) for idx in np. argsort (accums) [:-1] [: 5]: center_x, center_y = centers [idx] radius = radii [idx] cx, cy = draw. circle_perimeter (center_y, center_x, radius) image [cy, cx] = (255, 0, 0) ax1.imshow (image) ax1.set _ title ('detected image ')

The elliptic transformation is similar. The following functions are used:

skimage.transform.hough_ellipse(img,accuracy, threshold, min_size, max_size)

Input parameters:

Img: image to be detected.

Accuracy: the binary size of the short axis used on the accumulators. It is a double value. The default value is 1.

Thresh: Threshold Value of the accumulators. The default value is 4.

Min_size: minimum length of the long axis. The default value is 4.

Max_size: Maximum length of the short axis. The default value is None, indicating half of the shorter side of the image.

Returns an array of [(accumulator, y0, x0, a, B, orientation)]. accumulator indicates the accumulators, (y0, x0) indicates the center of the elliptic, (a, B) represents the long and short axes, and orientation represents the elliptical direction.

For example, an elliptical cup is detected in the coffee image.

Import matplotlib. pyplot as pltfrom skimage import data, draw, color, transform, feature # load the image, convert it to a grayscale image, and detect the edge image_rgb = data. coffee () [, 160: 420] # crop the original image. Otherwise, the image is very slow. image_gray = color. rgb2gray (image_rgb) edges = feature. canny (image_gray, sigma = 2.0, low_threshold = 0.55, high_threshold = 0.8) # execute the elliptic Transformation result = transform. hough_ellipse (edges, accuracy = 20, threshold = 250, min_size = 100, max_size = 120) result. sort (order = 'accessulator') # sort by accumulators # estimate the elliptic parameter best = list (result [-1]) # obtain the last yc, xc,, B = [int (round (x) for x in best [] orientation = best [5] # draw an elliptical cy, cx = draw on the source image. ellipse_perimeter (yc, xc, a, B, orientation) image_rgb [cy, cx] = (0, 0,255) # Use blue to represent the detected ellipse in the source image # use white to represent the canny edge, and use red to represent the detected ellipse and compare edges = color. gray2rgb (edges) edges [cy, cx] = (250, 0, 0) fig2, (ax1, ax2) = plt. subplots (ncols = 2, nrows = 1, figsize = (8, 4) ax1.set _ title ('original picture ') ax1.imshow (image_rgb) ax2.set _ title ('edge (white) and result (red) ') ax2.imshow (edges) plt. show ()

Hov elliptical transformation is very slow, so we should avoid the image being too large.

Summary

The above is all the details about how to implement the codes of HOV and elliptic transformation in Python. 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!

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.