使用OpenCV進行蟒蛇橢圓我需要適應的橢圓(使用cv2.fitEllipse)由cv.FindCornerSubPix返回點的陣列(這裏命名爲「功能」)。我在網上看到過很多這樣的例子,但我無法弄清楚。 我想通cv.FindCornerSubPix返回元組的數組,我的代碼引發的錯誤問我要numpy的數組作爲論據cv2.fitEllipse,所以我試圖「功能」轉換爲numpy的陣列,現在的錯誤是:適合通過點
'error:...... \ src \ opencv \ modules \ imgproc \ src \ contours.cpp:2019:error:(-215)points.checkVector(2)> = 0 & &(points.depth ()== CV_32F || points.depth()== CV_32S)'
on line 196('cv2.fitEllipse(ellipse)'at the end of my code),所以我想我不是喂右邊數組格式爲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橢圓 feature_matrix = cv.CreateMat(1,LEN(特徵),cv2.CV_32SC2) I = 0 爲the_point在特徵: cv.Set2D( feature_matrix,0,i,(int(the_point [0]),int(the_point [1]))) if = len + = cv2.fitEllipse(feature_matrix) image = np.asarray(image [:,:]) cv2.ellipse(i法師,橢圓形,(0,255,0),2) 圖像= cv.fromarray(圖像[:,:]) – Raoul
請張貼作爲回答您的評論,並接受它,這樣任何人都可以看到它已經解決了。 ;) – jnovacho