有没有办法在每10帧后更新检测到的角并计算光流量?

时间:2018-02-07 16:08:47

标签: python opencv opticalflow

我现在使用Lucas canade光流法。为此,我使用shi-thomasi角点检测检测到物体的角落。我希望连续每10帧后检测一组角点,然后将这些新点跟踪到接下来10帧的光流。之后我又想要检测那个框架中的新角落。同样,我希望每10帧后更新一组点并跟踪光流。我想知道这个领域的新人有没有办法做到这一点?谢谢。

这是我当前的代码,遵循opencv文档。

import numpy as np
import cv2

cap = cv2.VideoCapture('video3.mov')

# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 50,
                       qualityLevel = 0.01,
                       minDistance = 10,
                       blockSize = 3 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize  = (200,200),
                  maxLevel = 50,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 100, 0.03))

# Create some random colors
color = np.random.randint(0,255,(100,3))

ret, old_frame = cap.read()

#select an area
rows, cols = old_frame.shape[:2]

bottom_left = [cols * 0, rows * 0.9]
top_left = [cols * 0.25, rows * 0.6]
bottom_right = [cols * 1, rows * 0.9]
top_right = [cols * 0.6, rows * 0.6]

vertices = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype=np.int32)

maskzero2 = np.zeros_like(old_frame)
cv2.fillPoly(maskzero2, vertices, (255,) * maskzero2.shape[2])
maskedimg2 = cv2.bitwise_and(old_frame, maskzero2)

old_gray = cv2.cvtColor(maskedimg2, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)

print("p0",p0)

# gray = np.float32(old_gray)
# p0 = cv2.cornerHarris(gray, 2, 3, 0.04)

# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)

while(1):
    ret,frame = cap.read()
    rows, cols = frame.shape[:2]

    bottom_left = [cols * 0, rows * 0.9]
    top_left = [cols * 0.25, rows * 0.5]
    bottom_right = [cols * 1, rows * 0.9]
    top_right = [cols * 0.6, rows * 0.5]

    vertices = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype=np.int32)

    maskzero1 = np.zeros_like(frame)
    cv2.fillPoly(maskzero1, vertices, (255,) * maskzero1.shape[2])
    maskedimg1 = cv2.bitwise_and(frame, maskzero1)

    frame_gray = cv2.cvtColor(maskedimg1, cv2.COLOR_BGR2GRAY)

    # calculate optical flow
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
    print("P1",p1)
    print("st",st)
    print("error",err)

    if p1 is not None:
        good_new = p1[st==1]
        good_old = p0[st==1]

    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)
        frame1 = cv2.circle(maskedimg1,(a,b),5,color[i].tolist(),-1)
        # frame2 = cv2.circle(maskedimg1, (c, d), 5, color[i].tolist(), -1)

    img = cv2.add(frame1,mask)

    cv2.imshow("mask",img)


    if cv2.waitKey(10) & 0xFF == ord('q'):
            cv2.waitKey(0)



cv2.destroyAllWindows()
cap.release()

0 个答案:

没有答案