我在OpenCV中使用Lucas-Kanade Optical Flow算法进行了一些实验,但是我不知道good_new = p1[st==1]
这个代码的含义。
官方文件将其解释为“选择要点”,但我不知道此处基于原则要选择什么。这是代码的官方部分:
while(1):
ret,frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# Select good points
good_new = p1[st==1]
good_old = p0[st==1]
# draw the tracks
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1)
img = cv2.add(frame,mask)
cv2.imshow('frame',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
cv2.destroyAllWindows()
cap.release()
最佳答案
数组st
在第一个维度上的长度与p1
相同,因此可以用作如何在p1中选择值的“掩码”
此示例应有助于st==1
的实际工作方式:
>>> st = np.asarray([1,0,0,1,0,1])
>>> p1 = np.reshape(np.arange(len(a)*2), [len(a), 2])
>>> p1
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
>>> st==1
array([ True, False, False, True, False, True])
>>> p1[st==1]
array([[ 0, 1],
[ 6, 7],
[10, 11]])
>>> p1[[True, False, True, False, True, False]]
array([[0, 1],
[4, 5],
[8, 9]])
关于python - “good_new = p1[st==1]”在OpenCV中的Lucas-Kanade Optical Flow代码中意味着什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385926/