鉴于显示的示例图像,我对以下陈述非常怀疑:
我已经使用了开/关形态操作(这是我能得到的最干净的,相信我)
而且,在阅读了您的评论后,
为了精度,我需要它在大约 2 个像素的精度范围内
我很确定,使用形态学运算可能会有很好的近似值。
请看下面的代码:
import cv2
# Load image (as BGR for later drawing the circle)
image = cv2.imread('images/hvFJF.jpg', cv2.IMREAD_COLOR)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Get rid of possible JPG artifacts (when do people learn to use PNG?...)
_, gray = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
# Downsize image (by factor 4) to speed up morphological operations
gray = cv2.resize(gray, dsize=(0, 0), fx=0.25, fy=0.25)
# Morphological Closing: Get rid of the hole
gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))
# Morphological opening: Get rid of the stuff at the top of the circle
gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (121, 121)))
# Resize image to original size
gray = cv2.resize(gray, dsize=(image.shape[1], image.shape[0]))
# Find contours (only most external)
cnts, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Draw found contour(s) in input image
image = cv2.drawContours(image, cnts, -1, (0, 0, 255), 2)
cv2.imwrite('images/intermediate.png', gray)
cv2.imwrite('images/result.png', image)
中间图像如下所示:
而且,最终的结果是这样的:
由于您的图像非常大,我认为,缩小它不会造成任何伤害。以下形态学操作(大幅)加速,这可能对您的设置感兴趣。
根据你的说法:
注意:我没有关于圆圈大小的先前信息[...]
您可以从您的输入中找到上述内核大小的适当近似值。由于只给出了一个示例图像,我们无法知道该问题的可变性。
希望有帮助!