Hough gradient ： The detected circle has the same size as the original image

The minimum distance between the centers of adjacent circles detected （ If the parameter is too small , Except for a real circle , Multiple adjacent circles may also be incorrectly detected . If it's too big , Some circles may be missed .）

stay #HOUGH Gradient case , It is higher . Two thresholds are passed to Canny Edge detector （ The lower one is twice as small ）.

stay #HOUGH Gradient case , It is the accumulator threshold of the center of the circle in the detection stage . The smaller it is , The more likely it is to detect false circles ;minRadius： Minimum circle radius maxRadius： Maximum circle radius , If <=0, The maximum image size is used . If <0, Returns the center of the radius not found .

PS： stay opencv Often used in cv2.findContours() Function to find the contour of the detected object

""" -*- coding: utf-8 -*- author： Hao Hu @date 2021/12/3 8:00 AM """ import
math import cv2 import numpy as np import matplotlib.pyplot as plt def
circular_detect(): """ Hough transform circle detection """ import cv2 # Load and display pictures img =
cv2.imread('./circular.png') # Grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Output image size , Easy to adjust according to image size minRadius and maxRadius print(img.shape) ret, thresh1 =
cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) cv2.imshow('thresh1', thresh1)
canny = cv2.Canny(thresh1, 40, 80) cv2.imshow('Canny', canny) canny =
cv2.blur(canny, (3, 3)) cv2.imshow('blur', canny) # Hough transform circle detection circles =
cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 100, param1=50, param2=30,
minRadius=30, maxRadius=150) # Output return value , Easy to view type print(' Defines a three-dimensional array （x,y,r）',circles) #
Outputs the number of circles detected print(len(circles)) # According to the information of the detected circle , Draw each circle for circle in circles:
if (circle >= 100): continue # Basic information of circle print(' Radius ',circle) # Coordinate row and column x =
int(circle) y = int(circle) # radius r = int(circle) # Mark the position of the circle with the specified color in the original drawing img
= cv2.circle(img, (x, y), r, (0, 0, 255), -1) # Show new image
cv2.imshow('circular_detection', img) def Ellipse_feature_extraction2(): img =
cv2.imread("ellipse.png", 3) imgray = cv2.Canny(img, 600, 100, 3) #
Canny edge detection , Parameters can be changed ret, thresh = cv2.threshold(imgray, 127, 255,
cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # contours Is a profile set , You can calculate the length of the profile , Area, etc for cnt in
contours: if len(cnt) > 50: S1 = cv2.contourArea(cnt) ell = cv2.fitEllipse(cnt)
S2 = math.pi * ell * ell if (S1 / S2) > 0.2: # Area ratio , Can change , According to data set ...
img = cv2.ellipse(img, ell, (0, 255, 0), 2) #print(str(S1) + " " + str(S2) + "
" + str(ell) + " " + str(ell))
print(contours,contours[-1][-1][-1]) # This is the farthest point from the ellipse x =
(contours + contours[-1][-1][-1])/2 y = (contours +
contours[-1][-1][-1])/2 print(' Ellipse center ',x,y) r =
math.sqrt((x-contours)*(x-contours)+(y-contours)*(y-contours))
print(' Ellipse radius ',r) cv2.imshow("0", img) if __name__ == "__main__": #
line_detect_possible_demo() # circular_detect() Ellipse_feature_extraction2()
cv2.waitKey(0) cv2.destroyAllWindows()

Technology
Daily Recommendation
views 2