Python に opencv を使用する場合、cv.FindCornerSubPix (ここでは「features」という名前) によって返される点の配列に (cv2.fitEllipse を使用して) 楕円を合わせる必要があります。インターネット上でこのような例を数多く見てきましたが、理解できません。私は cv.FindCornerSubPix がタプルの配列を返すと考え、私のコードは cv2.fitEllipse の引数として numpy 配列を要求するエラーを引き起こしたので、'features' を numpy 配列に変換しようとしましたが、エラーは次のようになりました:
'エラー: ......\src\opencv\modules\imgproc\src\contours.cpp:2019: エラー: (-215) points.checkVector(2) >= 0 && (points.depth() == CV_32F || ポイント.深さ() == 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)