videostab::estimateGlobalMotionLeastSquares() doesn't work properly
Hi there,
Could any one give me an example of using videostab APIs in OpenCV-3.3.0 to estimate global motion between two adjacent frames? I couldn't find any of them, so I have figured out my way (as explained step by step bellowed). I am trying with estimateGlobalMotionLeastSquares(), but things seem doesn't work well.
Here are 2 original gray frames (left: previous, right: current) between those I want to find the global motion matrix.
After the extraction of keypoints (using cv::goodFeatureTotrack).
Getting transformation matrix with estimateGlobalMotionLeastSquares(), then have it transform the previous frame into coordinate space of the current one (left) and compute difference image (right).
You can see that the result are terrible. In bellow I attach my piece of code, I would highly appreciate a help to figure out possible mistakes in it, or favorably a well working sample of code.
//------------- READ 2 CONSECUTIVE FRAMES
videoSrc.read(prev_frame);
videoSrc.read(cur_frame);
//------------- CONVERT TO GRAY SCALE & EXTRACT KEY POINTS
cvtColor(prev_frame, prev_gray, COLOR_RGB2GRAY);
cvtColor(cur_frame, cur_gray, COLOR_RGB2GRAY);
goodFeaturesToTrack(prev_gray, prev_keypoints, 500, 0.04f, 10, cv::Mat(), 5, 0, 0.04f);
goodFeaturesToTrack(cur_gray, cur_keypoints, 500, 0.04f, 10, cv::Mat(), 5, 0, 0.04f);
//------------- ESTIMATE GLOBAL MOTION BASED ON THE ABOVE SETS OF KEY POINTS
Mat m_transform = videostab::estimateGlobalMotionLeastSquares(prev_keypoints, cur_keypoints, videostab::MM_AFFINE, NULL);
//------------- COMPUTE DIFFERENCE IMAGES
Mat prev_gray_trans, diffImg, normImg;
warpPerspective(prev_gray, prev_gray_trans, m_transform, Size(prev_gray.cols, prev_gray.rows));
diffImg = cur_gray - prev_gray_trans;
normalize (diffImg, normImg, 0, 255, NORM_MINMAX, CV_8UC1, Mat());