c++椭圆最小二乘法原理_OpenCV:用轮廓上的大多数点拟合椭圆(而不是最小二乘法)...

鉴于所示的示例图像,我对以下说法表示怀疑:which I've already used open/close morphology operations on (this is as clean as I can get it, trust me on this)

看了你的评论后For precision, I need it to be fit within about 2 pixels accuracy

我很确定,使用形态学运算可能有很好的近似值。在

请看以下代码: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)

中间图像如下所示:

最终结果如下:

既然你的形象很大,我想,缩小规模不会有什么坏处。下面的形态学操作(严重)加速,这可能对您的设置感兴趣。在

根据你的陈述:NOTE: I do not have prior info as to the size of the circle[...]

您可以从您的输入中找到上述内核大小的适当近似值。由于只给出了一个示例图像,我们无法知道该问题的可变性。在

希望有帮助!在


版权声明:本文为weixin_42250191原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。