First time here? Check out the FAQ!

Ask Your Question
2

video stabilization

asked Jan 25 '14

Jenny gravatar image

updated Jan 25 '14

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?

Preview: (hide)

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 (Jan 25 '14)edit

3 answers

Sort by » oldest newest most voted
4

answered Jan 25 '14

berak gravatar image

updated Jan 25 '14

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;
}
Preview: (hide)
2

answered Feb 19 '14

victusfate gravatar image

updated Feb 19 '14

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.

Preview: (hide)
1

answered Jan 28 '14

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;

}

Preview: (hide)

Comments

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

lilouch gravatar imagelilouch (Dec 2 '14)edit

Question Tools

1 follower

Stats

Asked: Jan 25 '14

Seen: 7,191 times

Last updated: Feb 19 '14