cgddrd/CS39440-major-project

View on GitHub
final/technical_work/additional_work/python/feature_tracking_motion_displacement/optical_flow.py

Summary

Maintainability
C
1 day
Test Coverage
"""
Note: This code is heavily based upon the C# implementation developed by Dr Rainer Hessmer (http://www.hessmer.org/blog/2010/08/17/monocular-visual-odometry/comment-page-1/)

"""

__author__ = 'connorgoddard'

import cv2

from optical_flow_settings import *

class OpticalFlow:

    def __init__(self, feature_detector_config, subpix_feature_detector_config, feature_tracker_config):
        self._feature_detector_config = feature_detector_config
        self._subpix_feature_detector_config = subpix_feature_detector_config
        self._feature_tracker_config = feature_tracker_config

    @classmethod
    def load_defaults(cls):
        return cls(feature_detector_defaults, subpix_feature_detector_defaults, feature_tracker_defaults)

    def find_features_to_track(self, input_image_gray):

        points = cv2.goodFeaturesToTrack(input_image_gray, **self._feature_detector_config)

        cv2.cornerSubPix(input_image_gray, points, **self._subpix_feature_detector_config)

        return points

    def calc_optical_flow(self, prev_gray_image, current_gray_image, prev_feature_points):

        return cv2.calcOpticalFlowPyrLK(prev_gray_image, current_gray_image, prev_feature_points, None, **self._feature_tracker_config)