考虑下面的图片:
可以看到,很多圆圈没有被检测到。我已经玩过了
param1
和 param2
(cv2.HoughCircles
)
在下面的代码中:
import cv2
import numpy as np
from PIL import Image
# Load the image and downscale it
img = Image.open('/tmp/F01-02.jpg')
img = img.resize((img.size[0] // 2, img.size[1] // 2)) # downscale by a factor of 2
open_cv_image = np.array(img)
# Convert to grayscale
gray = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2GRAY)
# Apply Hough transform
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 40, param1=5, param2=30, minRadius=7, maxRadius=10)
# Ensure at least some circles were found
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(open_cv_image, (x, y), r, (0, 255, 0), 2)
pil_image = Image.fromarray(open_cv_image)
pil_image.save('/tmp/result.jpg')
有什么想法吗?谢谢!