ZheyuanXie / KLT-Feature-Tracking

A Python implementation of the Kanade–Lucas–Tomasi (KLT) feature tracker
90 stars 23 forks source link

Is this different to cv2.goodFeaturesToTrack + cv2.calcOpticalFlowPyrLK ? #7

Open bgriffen opened 1 year ago

bgriffen commented 1 year ago

I'm just trying to understand if it is different to this opencv function (i.e. perform cv2.goodFeaturesToTrack + cv2.calcOpticalFlowPyrLK between frames). Thank you.

e.g. one can track the average motion of points within a bounding box as follows

def get_optical_flow_per_frame(video_file, bbox):
    tif = tifffile.TiffFile(video_file)
    total_frames = len(tif.pages)

    # Read the first frame
    prev_gray = tif.pages[0].asarray()

    # Convert bounding box coordinates to integers
    bbox1 = tuple(map(int, bbox))

    # Detect good features to track in each bounding box
    features_bbox1 = cv2.goodFeaturesToTrack(prev_gray[bbox1[2]:bbox1[3], bbox1[0]:bbox1[1]], maxCorners=100, qualityLevel=0.01, minDistance=5)

    features_bbox1 = (features_bbox1 + np.array([bbox1[0], bbox1[2]])).astype(np.float32)

    # Initialize variables for the flow calculation per frame
    flow_data_bbox1 = []

    for frame_idx in range(1, total_frames):
        frame_gray = tif.pages[frame_idx].asarray()
        # Calculate optical flow using Lucas-Kanade method
        features_bbox1_new, status1, _ = cv2.calcOpticalFlowPyrLK(prev_gray, frame_gray, features_bbox1, None)

        # Calculate the flow for each bounding box
        valid_flow1 = features_bbox1_new[status1.ravel() == 1] - features_bbox1[status1.ravel() == 1]
        if len(valid_flow1) > 0:
            flow_data_bbox1.append(( np.mean(valid_flow1, axis=0).reshape(2)))
        prev_gray = frame_gray

        # Update feature points
        features_bbox1 = features_bbox1_new[status1.ravel() == 1]
    return flow_data_bbox1

Above, it is using two methods:

  1. Good Features to Track (Tomasi-Kanade): The cv2.goodFeaturesToTrack() function is used to detect feature points (corners) in the first frame of the video.
  2. Lucas-Kanade method: The cv2.calcOpticalFlowPyrLK() function is used to calculate the optical flow for the detected feature points between consecutive frames. This is based on the Lucas-Kanade method, used for estimating optical flow.

I'm just trying to understand the differences between the above and your KLT-Feature-Tracking package. I want to ensure I am using the most accurate methods. Nice work however.