| #include "GlobalMotionCompensation.h"
|
|
|
| GlobalMotionCompensation::~GlobalMotionCompensation() {}
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Eigen::MatrixXf GlobalMotionCompensation::apply(const cv::Mat &frame_raw) {
|
|
|
| int height = frame_raw.rows;
|
| int width = frame_raw.cols;
|
| Eigen::MatrixXf H = Eigen::MatrixXf::Zero(2, 3);
|
| H.setIdentity();
|
| cv::Mat frame;
|
| cv::cvtColor(frame_raw, frame, cv::COLOR_BGR2GRAY);
|
|
|
|
|
| if (_downscale > 1.0F) {
|
| width /= _downscale, height /= _downscale;
|
| cv::resize(frame, frame, cv::Size(width, height));
|
| }
|
|
|
| if (!_first_frame_initialized) {
|
| |
| |
| |
|
|
| _first_frame_initialized = true;
|
| _prev_frame = frame.clone();
|
| return H;
|
| }
|
|
|
| std::vector <cv::Point2f> prev_pts;
|
| cv::goodFeaturesToTrack(_prev_frame, prev_pts, 200, 0.01, 30, cv::noArray(), 3);
|
|
|
|
|
| std::vector <cv::Point2f> matched_keypoints;
|
| std::vector <uchar> status;
|
| std::vector<float> err;
|
| try {
|
| cv::calcOpticalFlowPyrLK(_prev_frame, frame, prev_pts, matched_keypoints, status, err);
|
| }
|
| catch (const cv::Exception &e) {
|
| std::cout << "Warning: Could not find correspondences for GMC" << std::endl;
|
| _prev_frame = frame.clone();
|
| return H;
|
| }
|
|
|
| std::vector <cv::Point2f> prev_points, curr_points;
|
| for (size_t i = 0; i < matched_keypoints.size(); i++) {
|
| if (status[i]) {
|
| prev_points.push_back(prev_pts[i]);
|
| curr_points.push_back(matched_keypoints[i]);
|
| }
|
| }
|
|
|
| if (prev_points.size() > 4) {
|
| cv::Mat homography = cv::estimateAffine2D(prev_points, curr_points);
|
|
|
|
|
| H << homography.at<double>(0, 0), homography.at<double>(0, 1), homography.at<double>(0, 2),
|
| homography.at<double>(1, 0), homography.at<double>(1, 1), homography.at<double>(1, 2);
|
| }
|
| _prev_frame = frame.clone();
|
| return H;
|
| } |