OpenCV:用轮廓上的大多数点拟合椭圆(而不是最小二乘法)

OpenCV: Fit ellipse with most points on contour (instead of least squares)

我有一个二值化图像,我已经对其使用了 open/close 形态学操作(这是我能得到的最干净的图像,请相信我)看起来像这样:

如你所见,有一个明显的椭圆,顶部有些扭曲。 注意:我没有关于圆圈大小的先验信息,这必须 运行 非常快(我发现 HoughCircles 太慢了)。 我我试图弄清楚如何将椭圆拟合到它,以便它最大化拟合椭圆上对应于形状边缘的点数。也就是说,我想要这样的结果:

但是,我似乎无法在 OpenCV 中找到执行此操作的方法。使用 fitEllipse(蓝线)和 minAreaRect(绿线)的常用工具,我得到了这些结果:

这显然不代表我要检测的实际椭圆。关于如何实现这一目标的任何想法?很高兴看到 Python 或 C++ 中的示例。

Hough-Circle 非常适合这个。如果您知道直径,您可以获得更好的解决方案。如果您只知道一个范围,这可能最适合:

编辑:这比拟合椭圆效果更好的原因是:如果您正在寻找一个圆,您应该使用一个圆作为模型。 wiki article解释了这个美丽的想法。

顺便说一句,您也可以通过打开和关闭来完成此操作。 (给定你现在的圈子到底有多大)

import skimage
import matplotlib.pyplot as plt
import numpy as np
from skimage import data, color
from skimage.feature import canny
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte
from skimage.transform import hough_circle, hough_circle_peaks


image = skimage.io.imread("hvFJF.jpg")

# Load picture and detect edges

edges = canny(image, sigma=3, low_threshold=10, high_threshold=50)


# Detect two radii
hough_radii = np.arange(250, 300, 10)
hough_res = hough_circle(edges, hough_radii)

# Select the most prominent 5 circles
accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii,
                                           total_num_peaks=3)

# Draw them
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10, 4))
image = color.gray2rgb(image)
for center_y, center_x, radius in zip(cy, cx, radii):
    circy, circx = circle_perimeter(center_y, center_x, radius)
    image[circy, circx] = (220, 20, 20)

ax.imshow(image, cmap=plt.cm.gray)
plt.show()

鉴于显示的示例图像,我对以下陈述非常怀疑:

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[...]

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

希望对您有所帮助!