Ask Your Question
2

video stabilization

asked 2014-01-25 12:38:14 -0600

Jenny gravatar image

updated 2014-01-25 13:27:12 -0600

berak gravatar image

I am a newbie about Video Stabilization field. Now I am researching about it. I'm coding a small video stabilization demo. But I am stuck in some problems I use the function "estimateGlobalMotionLeastSquares" in OpenCV to estimate global motion But it doesn't work

Here is my code:

image description

For example: I create 2 variables p0,p1 as parameter for the function " estimateGlobalMotionLeastSquares" and I want to estimate global motion "t". But when I complied, the error is as:

image description

Please help me to fix this!!! Can you give me some examples about that function it?

edit retag flag offensive close merge delete

Comments

just a hint for the next time you ask : better just copy & paste & format your code here instead of screenshots, makes it far easier for ppl to try your code ;)

berak gravatar imageberak ( 2014-01-25 13:28:39 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
4

answered 2014-01-25 13:25:30 -0600

berak gravatar image

updated 2014-01-25 13:26:21 -0600

basically, you forgot to link against opencv_videostab248d.lib (w/o d for release)

( linker settings -> input -> additional dependancies )

also, you don't need to specify a vector allocator, if it's the default one.

following snippet compiled & linked fine for me:

#include "opencv2/videostab/global_motion.hpp"
using namespace cv;
using namespace cv::videostab;
#include <vector>
using namespace std;

int main()
{
    vector<Point2f> ax,by;
    ax.push_back(Point2f(1,1));
    by.push_back(Point2f(3,3));
    Mat t = estimateGlobalMotionLeastSquares(ax,by,AFFINE,0); // MM_AFFINE in 3.0
    return 0;
}
edit flag offensive delete link more
1

answered 2014-01-27 20:31:56 -0600

Jenny gravatar image

Thank you in advance. I got it. But I have a question. Please help me! My purpose is be create stabled video. First, I use the function goodFeaturesToTrack to get feature on the previous frame. Next, I use the function calcOpticalFlowPyrLK to obtain optical flow, and Finally I use function estimateGlobalMotionLeastSquares to get global motion between 2 consecutive frames. After receive global motion, I continue use warpPerspective to warp the original previous frame with estimated global motion to create next stabled frame. But the result seem not good. The video result does not stable. Please help me Here my code.

Mat currImg, prevImg, colorImg, outImg, grayImg, resultImg;
if (!capture.isOpened())
{
    printf("Can not open the video file");
    exit;
}
capture.read(colorImg);
cvtColor(colorImg,grayImg,CV_BGR2GRAY);
currImg = grayImg.clone();
outImg = grayImg.clone();

Size imgSize;
int winSize = 15;
int maxCorners = 400;
double qualityLevel = 0.05;
double minDistance = 5.0;
int blockSize = 3;
double k = 0.04;
IplImage *prevIplImg, *curIplImg;
int fps = 25;
int frameW = 640;
int frameH = 480;
VideoWriter writeOutputVideo ("result.avi", 0,fps,cvSize(colorImg.cols,colorImg.rows),FALSE);
for(;;)
{
    capture.read(colorImg);
    cvtColor(colorImg,grayImg,CV_BGR2GRAY);
    prevImg = currImg.clone();
    currImg = grayImg.clone();

    prevIplImg = cvCloneImage(&(IplImage)prevImg);
    curIplImg = cvCloneImage(&(IplImage)currImg);
    CvSize img_sz = cvGetSize(prevIplImg);

    vector<Point2f>cornerPrev;      
    cornerPrev.reserve(maxCorners);

    vector<Point2f>cornerCur;
    cornerCur.reserve(maxCorners);

    imgSize = prevImg.size();

    goodFeaturesToTrack(prevImg,cornerPrev,maxCorners,qualityLevel,minDistance,Mat(),3,false,0.04);
    //goodFeaturesToTrack(currImg,cornerCur,maxCorners,qualityLevel,minDistance,noArray(),3,false,0.04);

    //cornerSubPix(prevImg,cornerPrev,Size(winSize,winSize),Size(-1,1),TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS,20,0.03));
    //cornerSubPix(currImg,cornerCur,Size(winSize,winSize),Size(-1,1),TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS,20,0.03));

    //Call Lucas Kanade algorithm
    CvSize pyr_sz = Size (imgSize.width+8, imgSize.height/3);

    vector<uchar>featureFound;
    featureFound.reserve(maxCorners);

    vector<float>featureErrors;
    featureErrors.reserve(maxCorners);    

    //Only work on gray-scale image
    calcOpticalFlowPyrLK(prevImg,currImg,cornerPrev,cornerCur,featureFound,featureErrors,Size(winSize,winSize),3,
                        cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.3),0,0.0001);

    Mat transformMatrix = estimateGlobalMotionLeastSquares(cornerPrev,cornerCur,AFFINE,0);



    //Computes an optimal affine transformation between two 2D point sets.
    //Mat trans = estimateRigidTransform(currImg,prevImg,false);
    //warpAffine(prevImg,outImg,transformMatrix,Size(640,480),INTER_NEAREST|WARP_INVERSE_MAP);

    warpPerspective(prevImg,outImg,transformMatrix,Size(640,480),INTER_NEAREST|WARP_INVERSE_MAP,BORDER_CONSTANT ,0);

    imshow(inputVideo,colorImg);

    //cvtColor(outImg,resultImg,CV_BGR2GRAY);
    writeOutputVideo.write(outImg);
    //imshow(stabilizedVideo,outImg);
    if(waitKey(27) >= 0) break;

}

edit flag offensive delete link more

Comments

@Jenny did you solve your problem ? I had the same, maybe you can help me ? Thank

lilouch gravatar imagelilouch ( 2014-12-02 09:32:46 -0600 )edit
2

answered 2014-02-19 10:21:31 -0600

victusfate gravatar image

updated 2014-02-19 10:22:15 -0600

I'd suggest using the example in a recent opencv release (opencv3.0+) https://github.com/Itseez/opencv/blob/master/samples/cpp/videostab.cpp

g++ videostab3.0.cc -o videostab $(pkg-config --libs opencv)

I found better stabilization results with a full homographic model, but it was much slower.

ps After spending some time with the videostab module in opencv, I didn't have much luck with a generalized high quality and fast (real time) video stabilization tool. I opted for using the ffmpeg vid.stab plugin, but would like to revisit opencv video stabilization once the module has matured.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-01-25 12:38:14 -0600

Seen: 6,824 times

Last updated: Feb 19 '14