OpenCV Python中的椭圆检测

9

我的图片在这里:

我的照片在这里。

我正在寻找一种更好的解决方案或算法来检测这张照片中椭圆形部分(盘子),并在OpenCV中将其遮罩到另一张照片上。请问您能给我一些建议或解决方案吗?下面是我的代码:

 circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.2, 1, param1=128, minRadius=200, maxRadius=600)
    # draw detected circles on image
    circles = circles.tolist()
    for cir in circles:
        for x, y, r in cir:
            x, y, r = int(x), int(y), int(r)
            cv2.circle(img, (x, y), r, (0, 255, 0), 4)

    # show the output image
    cv2.imshow("output", cv2.resize(img, (500, 500)))

1
这里不需要椭圆检测(顺便说一下,在OpenCV中也不可用)。你可能只需要对亮度值进行简单的阈值处理,并保留最大的连通组件。另外,请展示一下你尝试过的内容。 - Miki
一个简单的颜色分割也可能有效。 - ZdaR
我正在添加与我的问题相关的特殊代码部分,请您看一下。 - Mohammad Mostafavi
2个回答

10
在skimage中有一个替代方法,由Xie, Yonghong和Qiang Ji制作并发表于“一种新的高效椭圆检测方法”。Pattern Recognition, 2002. Proceedings. 16th International Conference on. Vol. 2. IEEE, 2002.他们的椭圆检测代码相对较慢,示例需要大约70秒;与网站声称的“28秒”相比。如果您有conda或pip:“name”安装scikit-image并尝试一下...您可以在此处找到他们的代码here或作为复制/粘贴下方:
import matplotlib.pyplot as plt

from skimage import data, color, img_as_ubyte
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

# Load picture, convert to grayscale and detect edges
image_rgb = data.coffee()[0:220, 160:420]
image_gray = color.rgb2gray(image_rgb)
edges = canny(image_gray, sigma=2.0,
              low_threshold=0.55, high_threshold=0.8)

# Perform a Hough Transform
# The accuracy corresponds to the bin size of a major axis.
# The value is chosen in order to get a single high accumulator.
# The threshold eliminates low accumulators
result = hough_ellipse(edges, accuracy=20, threshold=250,
                       min_size=100, max_size=120)
result.sort(order='accumulator')

# Estimated parameters for the ellipse
best = list(result[-1])
yc, xc, a, b = [int(round(x)) for x in best[1:5]]
orientation = best[5]

# Draw the ellipse on the original image
cy, cx = ellipse_perimeter(yc, xc, a, b, orientation)
image_rgb[cy, cx] = (0, 0, 255)
# Draw the edge (white) and the resulting ellipse (red)
edges = color.gray2rgb(img_as_ubyte(edges))
edges[cy, cx] = (250, 0, 0)

fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4), sharex=True,
                                sharey=True,
                                subplot_kw={'adjustable':'box'})

ax1.set_title('Original picture')
ax1.imshow(image_rgb)

ax2.set_title('Edge (white) and result (red)')
ax2.imshow(edges)

plt.show()

如果您使用的是Matplotlib版本<2.2.2,则请使用:subplot_kw = {'adjustable':'box-forced'}。 - ZF007

7

方法一:

根据Miki的建议,我使用轮廓属性(这里使用了面积属性)成功地检测出给定图像中的椭圆。

代码:

#--- First obtain the threshold using the greyscale image ---
ret,th = cv2.threshold(gray,127,255, 0)

#--- Find all the contours in the binary image ---
_, contours,hierarchy = cv2.findContours(th,2,1)
cnt = contours
big_contour = []
max = 0
for i in cnt:
   area = cv2.contourArea(i) #--- find the contour having biggest area ---
    if(area > max):
        max = area
        big_contour = i 

final = cv2.drawContours(img, big_contour, -1, (0,255,0), 3)
cv2.imshow('final', final)

这是我得到的:

enter image description here

方法二:

在这种情况下,您也可以使用您建议的方法。进行椭圆/圆形的霍夫检测。

您需要对图像进行预处理。我执行了自适应阈值并得到了以下结果:

enter image description here

现在,您可以对此图像执行霍夫圆检测。希望这不是一个难以理解的词汇! :D

谢谢你的帮助,但我的问题是我无法使用轮廓面积方法。我正在尝试使用HoughCircles变换来解决它,但我的问题是我无法在盘子周围画椭圆。你能帮我用这个算法怎么做吗?提前感谢你。 - Mohammad Mostafavi
你可以尝试在我发布的第二张图片上使用霍夫变换来检测圆形。试一试吧。 - Jeru Luke
4
小提示:HoughCircles只能检测圆形,而不能检测椭圆形 ;) @Moh - Miki
2
不,如果点不在圆周上,你不能很好地填充霍夫累加器...只有两个轴非常相似的椭圆。而用圆来近似椭圆大多数情况下都是一个糟糕的近似。 - Miki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接