@Carlos,我真的不霍夫圓的大風扇,一個像你」的情況已經描述過。說實話,我覺得這個算法非常不直觀。我會建議你的情況是使用findContour()
功能,然後計算質量中心。這樣說,我調整了霍夫的參數有點合理的結果。在Canny之前,我還使用了一種不同的預處理方法,因爲我沒有看到在任何其他情況下該閾值將如何工作。
霍夫方法:
發現質量中心:
,代碼:
from matplotlib.pyplot import imshow, scatter, show, savefig
import cv2
image = cv2.imread('circles.png', 0)
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.GaussianBlur(image.copy(), (27, 27), 0)
image = cv2.Canny(image, 0, 130)
cv2.imshow("canny", image)
cv2.waitKey(0)
imshow(image, cmap='gray')
circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50)
x = circles[0, :, 0]
y = circles[0, :, 1]
scatter(x, y)
show()
savefig('result1.png')
cv2.waitKey(0)
_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"]/M["m00"])
cY = int(M["m01"]/M["m00"])
#draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (125, 125, 125), 2)
cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1)
cv2.imshow("Image", image)
cv2.imwrite("result2.png", image)
cv2.waitKey(0)
兩種方法都需要一些微調,但我希望給你更多的東西來與...合作。
來源:this answer當然也是唯一的pyimagesearch。
考慮接受答案,如果你認爲它有幫助。 – m3h0w