函數定義如下:
HoughCircles(image, method, dp, minDist, circles=None, param1=None, param2=None, minRadius=None, maxRadius=None)
# coding:utf8
import cv2
import numpy as np
def row_method(src):
image = np.array(src)
cimage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 灰度圖
circles = cv2.HoughCircles(cimage, cv2.HOUGH_GRADIENT, 1, 40, param1=250, param2=58, minRadius=0)
circles = np.uint16(np.around(circles)) # 取整
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # 在原圖上畫圓,圓心,半徑,顏色,線框
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # 畫圓心
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) # 灰度圖
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)) # 取整
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # 在原圖上畫圓,圓心,半徑,顏色,線框
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # 畫圓心
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) # 灰度圖
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)) # 取整
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # 在原圖上畫圓,圓心,半徑,顏色,線框
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # 畫圓心
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) # 均值偏移濾波
cimage = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) # 灰度圖
circles = cv2.HoughCircles(cimage, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=20, minRadius=0)
circles = np.uint16(np.around(circles)) # 取整
for i in circles[0, :]:
cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2) # 在原圖上畫圓,圓心,半徑,顏色,線框
cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2) # 畫圓心
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") # 讀取圖片位置
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()