我正在尝试计算使用opencv的goodFeaturesToTrack提取的某些点的光流。只要将直接从goodFeaturesToTrack提取的点或由numpy函数修改的点发送给calcOpticalFlowPyrLK函数,它就可以很好地工作。 np.array_like和np.squeeze都兼容。但是,如果我从float32 numpy数组的一个切片发送这些点,它将挂起,并且出现以下错误:
error: OpenCV(4.1.0) D:\Build\OpenCV\opencv-4.1.0\modules\video\src\lkpyramid.cpp:1229: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'cv::`anonymous-namespace'::SparsePyrLKOpticalFlowImpl::calc'
下面是一些示例代码:
cap = cv2.VideoCapture(fname)
ret,frame = cap.read()
gr_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
candidate_feats = cv2.goodFeaturesToTrack( image = gr_img,
maxCorners = 5,
qualityLevel = 0.3,
minDistance = 7,
mask = None,
blockSize = 7)
pts_candidate = np.squeeze(candidate_feats, 1) #this one works fine
pts = np.zeros_like(pts_candidate) #this works fine too
pts = np.zeros(pts_candidate.shape, dtype=np.float32) #this one works
#the feature array from which I want to take a slice
feats = np.zeros([len(pts_candidate),13], dtype=np.float32)
feats[:,6:8] = pts_candidate
pts = feats[:,6:8] #this one hangs
pts_candidate, st, err = cv2.calcOpticalFlowPyrLK(prevImg = gr_img,
nextImg = gr_img,
prevPts = pts,
nextPts = None,
winSize = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS |
cv2.TERM_CRITERIA_COUNT,
10, 0.03))
有什么建议吗?