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