“ good_new = p1 [st == 1]”在OpenCV中的Lucas-Kanade光流代码中是什么意思

问题描述 投票:0回答:1

[我在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()
python opencv computer-vision motion-detection opticalflow
1个回答
0
投票

数组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]])

© www.soinside.com 2019 - 2024. All rights reserved.