# video stabilization

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:

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:

edit retag close merge delete

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 ;)

( 2014-01-25 13:28:39 -0500 )edit

Sort by ยป oldest newest most voted

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

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;
}

more

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;
}
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(;;)
{
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));

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;


}

more

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

( 2014-12-02 09:32:46 -0500 )edit

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.

more

Official site

GitHub

Wiki

Documentation