另一种解决方案是使用cv2.HoughCircles 来检测图像中的圆形。
我使用来自pyimagesearch 的示例作为我的代码的基础,并在HoughCircles 中将检测到的圆圈的输出添加到圆圈的下部。结果如下图:
由于此函数返回[x, y, r](圆的中心点和半径),您可以轻松找到圆的最低部分:
low_point = [x, y + r]
请记住,您可以玩弄来自cv2.HoughCircles() 函数的参数。
你可以看到我在this Github page. 中使用的 Jupyter Notebook。
我使用的代码:
# import the necessary packages
import numpy as np
import cv2
import matplotlib.pyplot as plt
# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread('img_circle.png')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#cv2.imshow("test", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(image)
plt.title('my picture')
plt.show()
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100,
param1=100, param2=40,
minRadius = 130, maxRadius = 0)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
# Draw low point of the circle
cv2.rectangle(output, (x - 10, y - 10 + r), (x + 10, y + 10 + r), (0, 0, 255), -1)
# show the output image
plt.imshow(np.hstack([image, output]))
plt.title('my picture')
plt.show()
cv2.imwrite( "lowPoint.jpg", np.hstack([image, output]));