【问题标题】:fit an ellipse through points通过点拟合椭圆
【发布时间】:2013-12-25 06:54:55
【问题描述】:

在 python 中使用 opencv 我需要将一个椭圆(使用 cv2.fitEllipse)拟合到 cv.FindCornerSubPix(这里命名为“特征”)返回的点数组中。我在互联网上看到了很多这样的例子,但我无法弄清楚。 我认为 cv.FindCornerSubPix 返回一个元组数组,我的代码触发了一个错误,要求我输入一个 numpy 数组作为 cv2.fitEllipse 的参数,所以我尝试将“功能”转换为一个 numpy 数组,现在错误是:

'error: ......\src\opencv\modules\imgproc\src\contours.cpp:2019: 错误: (-215) points.checkVector(2) >= 0 && (points.depth( ) == CV_32F || points.depth() == CV_32S)'

在第 196 行(代码末尾的“cv2.fitEllipse(ellipse)”),所以我想我没有将正确的数组格式提供给 cv2.fitEllipse。你能帮帮我吗?下面的代码只是 opencv 示例 lkdemo.py 的修改版本。

            # search the good points
        features = cv.GoodFeaturesToTrack (
            grey, eig, temp,
            MAX_COUNT,
            quality, min_distance, mask, 10, 0, 0.04)

        # refine the corner locations
        features = cv.FindCornerSubPix (
            grey,
            features,
            (win_size, win_size),  (-1, -1),
            (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03))

    elif features != []:
        # we have points, so display them

        # calculate the optical flow
        features, status, track_error = cv.CalcOpticalFlowPyrLK (
            prev_grey, grey, prev_pyramid, pyramid,
            features,
            (win_size, win_size), 3,
            (cv.CV_TERMCRIT_ITER|cv.CV_TERMCRIT_EPS, 20, 0.03),
            flags)

        # set back the points we keep
        features = [ p for (st,p) in zip(status, features) if st]

        if add_remove_pt:
            # we have a point to add, so see if it is close to
            # another one. If yes, don't use it
            def ptptdist(p0, p1):
                dx = p0[0] - p1[0]
                dy = p0[1] - p1[1]
                return dx**2 + dy**2
            if min([ ptptdist(pt, p) for p in features ]) < 25:
                # too close
                add_remove_pt = 0

        # draw the points as green circles
        for the_point in features:
            cv.Circle (image, (int(the_point[0]), int(the_point[1])), 3, (0, 255, 0, 0), -1, 8, 0)

        #Fit an ellipse
        array = np.array([tuple(i) for i in features])
        ellipse = np.asarray(array)
        cv2.fitEllipse(ellipse)

【问题讨论】:

  • 已解决:#Fit an ellipse feature_matrix = cv.CreateMat(1, len(features), cv2.CV_32SC2) i = 0 for the_point in features: cv.Set2D(feature_matrix, 0, i, (int(the_point[0]), int(the_point[1]))) i = i + 1 if len(features) > 6: feature_matrix = np.asarray(feature_matrix) ellipse = cv2.fitEllipse(feature_matrix) image = np .asarray(image[:,:]) cv2.ellipse(image,ellipse,(0,255,0),2) image = cv.fromarray(image[:,:])
  • 请发表您的评论作为答案并接受它,这样任何人都可以看到它已经解决了。 ;)

标签: python opencv numpy


【解决方案1】:

这个问题解决了。请查看 cmets 部分。顺便说一句,Stackoverflow 要求延迟几个小时让新人回答他自己的问题,这就是为什么我将答案放在 cmets 中。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2018-06-01
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多