【问题标题】:ellipse detection in opencv pythonopencv python中的椭圆检测
【发布时间】:2017-07-01 13:20:22
【问题描述】:

我的图片在这里:

我正在寻找更好的解决方案或算法来检测这张照片中的椭圆部分(盘子)并在 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)))

【问题讨论】:

  • 这里不需要椭圆检测(顺便说一句,在 OpenCV 中不可用)。你可能对亮度值有一个简单的阈值,并保持最大的连接分量。另外,请展示您的尝试
  • 简单的颜色分割也可以。
  • 我正在添加代码的特殊部分,这与我的问题有关,请您看一下。

标签: python python-2.7 opencv scikit-image


【解决方案1】:

Xie, Yonghong, and Qiang Ji 制作的skimage 中有一个替代方案,并发布为...

“一种新的高效椭圆检测方法。”模式识别,2002。 诉讼。第 16 届国际会议。卷。 2. IEEE,2002。

他们的Ellipse检测代码比较慢,示例大约需要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】:

方法 1:

按照 Miki 的建议,我能够使用轮廓属性检测给定图像中的椭圆(在此我使用了 area 属性)。

代码:

#--- 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)

这是我得到的:

方法 2:

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

您必须对图像进行预处理。我执行了自适应阈值并得到了这个:

现在您可以对这张图片进行霍夫圆检测了。

希望不是拗口!! :D

【讨论】:

  • 感谢亲爱的帮助我,但我的问题是,我不能使用轮廓区域方法,我正在尝试使用 HoughCircles 变换,但我的问题是我无法在我的周围绘制椭圆菜你能帮我用这个算法怎么做?提前致谢
  • 您可以尝试在我发布的第二张图片上使用霍夫变换检测圆圈。试一试
  • 一点提示:HoughCircles 没有检测到椭圆......只是圆圈;)@Moh
  • 不,如果点不在圆周上,你就不能很好地填充霍夫累加器......只有两个轴非常相似的椭圆。而用圆来逼近椭圆在大多数情况下是一个糟糕的逼近
猜你喜欢
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 2012-06-14
  • 2020-06-16
  • 1970-01-01
  • 2015-08-06
相关资源
最近更新 更多