c++opencvsurfimage-stabilization

video stabilization using opencv


I am trying to do video stabilization with opencv(without the opencv video stabilization class).

the steps for my algo is as follows->

  1. Surf points extraction,

  2. Matching,

  3. Homography matrix,

  4. warpPerspective

And the output video is not stabilized at all :(. it just looks like the original video. I could not find and reference code for video stabilization. I followed the procedure described here . Can anybody help me out by telling me where I am going wrong or provide me some source code link to improve my algo.

Please help. Thank you


Solution

  • You can use my code snippet as a start point (not very stable but seems it works):

    #include "opencv2/opencv.hpp"
    #include <iostream>
    #include <vector>
    #include <stdio.h>
    
    using namespace cv;
    using namespace std;
    
    int main(int ac, char** av)
    {
        VideoCapture capture(0);
        namedWindow("Cam");
        namedWindow("Camw");
        Mat frame;
        Mat frame_edg;
        Mat prev_frame;
        int k=0;
        Mat Transform;
        Mat Transform_avg=Mat::eye(2,3,CV_64FC1);
        Mat warped;
        while(k!=27)
        {
            capture >> frame;
            cv::cvtColor(frame,frame,cv::COLOR_BGR2GRAY);
            cv::equalizeHist(frame,frame);
            cv::Canny(frame,frame_edg,64,64);
            //frame=frame_edg.clone();
            imshow("Cam_e",frame_edg);
            imshow("Cam",frame);
    
            if(!prev_frame.empty())
            {
                Transform=estimateRigidTransform(frame,prev_frame,0);
                Transform(Range(0,2),Range(0,2))=Mat::eye(2,2,CV_64FC1);
                Transform_avg+=(Transform-Transform_avg)/2.0;
                warpAffine(frame,warped,Transform_avg,Size( frame.cols, frame.rows));
    
                imshow("Camw",warped);
            }
    
            if(prev_frame.empty())
            {
                prev_frame=frame.clone();
            }
    
            k=waitKey(20);      
        }
        cv::destroyAllWindows();
        return 0;
    }
    

    You can also look for paper: Chen_Halawa_Pang_FastVideoStabilization.pdf as I remeber there was MATLAB source code supplied.