【问题标题】:Python Fit ellipse to an imagePython 将椭圆拟合到图像
【发布时间】:2016-07-22 16:02:04
【问题描述】:

我有一个使用 OpenCV 的网络摄像头供稿,我正在尝试实时拟合椭圆。

我目前使用的代码有效,但很多时候它无法将椭圆拟合到图像中。我还可以追求哪些其他椭圆拟合图像的方法?

当前代码:

def find_ellipses(img): #img is grayscale image of what I want to fit
        ret,thresh = cv2.threshold(img,127,255,0)
        _,contours,hierarchy = cv2.findContours(thresh, 1, 2)

        if len(contours) != 0:
            for cont in contours:
                if len(cont) < 5:
                    break
                elps = cv2.fitEllipse(cont)
                return elps  #only returns one ellipse for now
        return None

elps 的格式为 (x_centre,y_centre),(minor_axis,major_axis),angle

这是我想要成功拟合椭圆的示例。当我不想要这张图片时,我当前的代码失败了。

【问题讨论】:

    标签: python opencv ellipse


    【解决方案1】:

    原来我错了只是从函数中获取第一个椭圆。虽然我认为第一个计算出的椭圆是最正确的,但实际上我要做的是遍历所有椭圆 - 并选择最合适的椭圆来界定图像中的对象。

    【讨论】:

      【解决方案2】:

      我会在函数之外定义我的轮廓,因为您不需要在此图像中不断重新定义它们。

      def create_ellipse(thresh,cnt):
          ellipse = cv2.fitEllipse(cnt)
          thresh = cv2.ellipse(thresh,ellipse,(0,255,0),2)
          return thresh
      

      这段代码正在做的是我获取我的 thresh 图像流并在其顶部添加一个椭圆。稍后在我的代码中,当我想调用它时,我使用该行

      thresh = create_ellipse(thresh,cnt)
      

      【讨论】:

      • 图像不断更新(它是实时网络摄像头)。我遇到的问题是它找不到适合此图像的轮廓。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2019-03-19
      • 2011-06-14
      • 2015-04-01
      • 2023-03-19
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多