鉴于所显示的示例图像,我对以下陈述非常怀疑:
我已经使用过开/关形态学操作(这是我能得到的最干净的,相信我)
并且,在阅读了您的评论后,
为了精度,我需要它的精度在大约 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)
中间图像如下所示:
并且,最终结果如下所示:
由于您的图像相当大,我认为缩小它不会有什么坏处。以下形态学操作(大大)加速,这可能对您的设置感兴趣。
根据你的说法:
注意:我没有关于圆圈大小的事先信息[...]
您通常可以从输入中找到上述内核大小的适当近似值。由于只给出了一张示例图像,我们无法知道该问题的可变性。
希望有帮助!