程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python cv2. Houghcircles Hough circle detection

編輯:Python

HoughCircles Use and instructions

  • 1. HoughCircles explain
  • 2. Code
  • 3. result

cv2 A method of circle detection is provided :HoughCircles. The return result of this function is closely related to the parameter settings .
Detected image 9 Coins , Thresholds are used respectively ( Otsu method and trigonometry )、 Mean shift filtering and unprocessed images . The result of the experiment is that just adjust param1 and param2 Two parameters , The above methods can accurately identify the circle in the image . The closest thing to the circle is the Otsu threshold . Using this method requires the use of cv2.THRESHOLD_TRUNC.

1. HoughCircles explain

The function is defined as follows :

HoughCircles(image, method, dp, minDist, circles=None, param1=None, param2=None, minRadius=None, maxRadius=None)
Parameters meaning image original image method Currently only supported cv2.HOUGH_GRADIENTdp Inverse scale of image parsing .1 For the original size ,2 Half the original size minDist The minimum distance between the centers of a circle . Too small will increase the misjudgment of the circle , Too much will lose the existing circle param1Canny Detector high threshold param2 The accumulator threshold of the center of the circle in the detection stage . The smaller it is , Will increase the nonexistent circle ; The bigger it is , The detected circle is closer to the perfect circle minRadius Radius of the smallest circle detected maxRadius Radius of the largest circle detected

2. Code

# coding:utf8
import cv2
import numpy as np
def row_method(src):
image = np.array(src)
cimage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # grayscale 
circles = cv2.HoughCircles(cimage, cv2.HOUGH_GRADIENT, 1, 40, param1=250, param2=58, minRadius=0)
circles = np.uint16(np.around(circles)) # integer 
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # Draw a circle on the original drawing , center of a circle , radius , Color , Wireframe 
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # Draw the center of the circle 
cv2.putText(image, "param1=250, param2=58", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.imshow("row_circles", image)
def threshold_OTSU_method(src):
image = np.array(src)
cimage = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # grayscale 
th, dst = cv2.threshold(cimage, 200, 255, cv2.THRESH_BINARY + cv2.THRESH_TRUNC + cv2.THRESH_OTSU)
circles = cv2.HoughCircles(dst, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=47, minRadius=0)
circles = np.uint16(np.around(circles)) # integer 
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # Draw a circle on the original drawing , center of a circle , radius , Color , Wireframe 
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # Draw the center of the circle 
cv2.putText(image, "param1=50, param2=47", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.imshow("otsu_circles", image)
def threshold_triangle_method(src):
image = np.array(src)
cimage = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # grayscale 
th, dst = cv2.threshold(cimage, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
circles = cv2.HoughCircles(dst, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=17, minRadius=0)
circles = np.uint16(np.around(circles)) # integer 
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # Draw a circle on the original drawing , center of a circle , radius , Color , Wireframe 
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # Draw the center of the circle 
cv2.putText(image, "param1=50, param2=17", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.imshow("triangle_circles", image)
def mean_circles(src):
image = np.array(src)
dst = cv2.pyrMeanShiftFiltering(image, 10, 100) # Mean shift filtering 
cimage = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) # grayscale 
circles = cv2.HoughCircles(cimage, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=20, minRadius=0)
circles = np.uint16(np.around(circles)) # integer 
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # Draw a circle on the original drawing , center of a circle , radius , Color , Wireframe 
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # Draw the center of the circle 
cv2.putText(image, "param1=50, param2=20", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.imshow("mean_circles", image)
src = cv2.imread("circle.png") # Read picture position 
cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image", src)
threshold_OTSU_method(src)
threshold_triangle_method(src)
mean_circles(src)
row_method(src)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. result


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved