【问题标题】:Detect ellipse parameters from a given elliptical mask从给定的椭圆掩码中检测椭圆参数
【发布时间】:2020-08-11 12:50:24
【问题描述】:

我正在关注tutorial from the skimage 网站以检测蒙版的边缘,然后获取椭圆周长。

但是,当我运行这段代码时,我得到了这个:

我已将 hough_ellipse 函数中的阈值降低到 100,因为这是工作的最高值(有时不适用于其他掩码),min_size 降低到 10(出于同样的原因)。我没有图像处理或计算机视觉方面的经验,也没有找到任何其他方法来从大量面具中获取周长。

【问题讨论】:

  • 您有一个带有单个椭圆的图像。为什么要使用诸如霍夫变换之类的复杂方法来找到它?它已经被检测到了。除了椭圆之外什么都没有!
  • 获取轮廓并使用 cv2.fitEllipe。这将返回椭圆中心、大直径和小直径以及椭圆的旋转角度。
  • @CrisLuengo 我想要的是图像中椭圆的周长。
  • 例如在 OpenCV 中使用 cv.findContours()。 Skimage 似乎没有类似的功能,但它可以让您获取椭圆的参数,然后您可以绘制它。使用skimage.measure.regionprops。它给出了长轴和短轴的长度、方向和质心,以及椭圆的所有参数。
  • @fmw42 谢谢你完美的工作。你能把它放在这里作为你的答案吗?

标签: python image-processing computer-vision scikit-image


【解决方案1】:

这是在 Python/OpenCV 中拟合椭圆的方法。

  • 读取输入
  • 转换为灰色
  • 阈值
  • 使椭圆适合形状
  • 在输入图像上绘制椭圆
  • 保存结果

输入:

import cv2
import numpy as np

# read image
img = cv2.imread('ellipse_shape.png')
hh, ww = img.shape[:2]

# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# threshold to binary and invert
thresh = cv2.threshold(gray, 252, 255, cv2.THRESH_BINARY)[1]

# fit ellipse
# note: transpose needed to convert y,x points from numpy to x,y for opencv
points = np.column_stack(np.where(thresh.transpose() > 0))
hull = cv2.convexHull(points)
((centx,centy), (width,height), angle) = cv2.fitEllipse(hull)
print("center x,y:",centx,centy)
print("diameters:",width,height)
print("orientation angle:",angle)

# draw ellipse on input image
result = img.copy()
cv2.ellipse(result, (int(centx),int(centy)), (int(width/2),int(height/2)), angle, 0, 360, (0,0,255), 2)

# show results
cv2.imshow('image', img)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('ellipse_shape_fitted.png', result)

生成的椭圆:

椭圆数据:

center x,y: 291.0881042480469 337.10638427734375
diameters: 176.3456573486328 207.72769165039062
orientation angle: 125.05526733398438


【讨论】:

    【解决方案2】:

    从本教程和这张图片我得到了这个参数:

    accuracy=50
    
    threshold=50
    
    min_size=100
    
    max_size=100
    

    Hough ellipse example

    【讨论】:

    • 如果您打算将其作为本页顶部问题的答案,那么您必须使回答部分更加明显......您可以edit 这样做。即使“本教程”是一个链接,也不会被视为答案。
    猜你喜欢
    • 2020-09-08
    • 2016-12-24
    • 2014-12-23
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多