通过点拟合椭圆

时间:2013-05-25 11:30:43

标签: python opencv numpy

使用opencv for python我需要将一个椭圆(使用cv2.fitEllipse)拟合到cv.FindCornerSubPix(此处命名为'features')返回的点数组。我在互联网上看到了很多这样的例子,但我无法弄清楚。 我想cv.FindCornerSubPix返回一个元组数组,我的代码触发了一个错误,要求我将一个numpy数组作为cv2.fitEllipse的参数,所以我试图将'features'转换为numpy数组,现在错误是:

'错误:...... \ 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)

1 个答案:

答案 0 :(得分:0)

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

干杯