Dense optical flow test in 2 continuous images(opencv 2.4.9)

Dear Rahma
I hope to help this code to you.
Thank you.




...
#include < stdio.h>    
  
#include < opencv2\opencv.hpp>    
#include < opencv2/core/core.hpp>    
#include < opencv2/highgui/highgui.hpp>    
#include < opencv2\nonfree\features2d.hpp >        
  
  
  
#ifdef _DEBUG            
#pragma comment(lib, "opencv_core249d.lib")    
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing    
#pragma comment(lib, "opencv_objdetect249d.lib") //HOGDescriptor    
#pragma comment(lib, "opencv_features2d249d.lib")    
#pragma comment(lib, "opencv_highgui249d.lib")    
#pragma comment(lib, "opencv_video249d.lib")    
#else    
#pragma comment(lib, "opencv_core249.lib")    
#pragma comment(lib, "opencv_imgproc249.lib")    
#pragma comment(lib, "opencv_objdetect249.lib")    
#pragma comment(lib, "opencv_features2d249.lib")    
#pragma comment(lib, "opencv_highgui249.lib")   
#pragma comment(lib, "opencv_video249.lib")    
#endif     
  
using namespace std;    
using namespace cv; 

void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color);

void main()
{
 Mat prev_im,nex_im,flow;

 prev_im=imread("1.jpg",0);
 imshow("previm",prev_im);


 nex_im=imread("2.jpg",0);
 imshow("nextim",nex_im);


 calcOpticalFlowFarneback(prev_im,nex_im,flow,0.5, 1, 12, 2, 7, 1.5, 0); 

 Mat cflow;
 cvtColor(prev_im, cflow, CV_GRAY2BGR);
 drawOptFlowMap(flow, cflow, 10, CV_RGB(0, 255, 0));  


 imshow("OpticalFlowFarneback", cflow);
 waitKey(0);

}


void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color) {  
 for(int y = 0; y < cflowmap.rows; y += step)  
  for(int x = 0; x < cflowmap.cols; x += step)  
  {  
   const Point2f& fxy = flow.at< Point2f>(y, x);  
   line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),  
    color);  
   circle(cflowmap, Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), 1, color, -1);  
  }  
}  

...

Comments

  1. Hi Kim,

    I tried to run this program in Qt Env. I am getting a run time exception when I invoke the function
    calcOpticalFlowFarneback.
    Could you help me in isolating the issue

    /home/santhosh/opencv-qt/OpticalFlowDense2/OpticalFlowDense2... OpenCV Error: Assertion failed (prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyr_scale < 1) in calcOpticalFlowFarneback, file /home/santhosh/opencv/opencv-3.1.0-source/modules/video/src/optflowgf.cpp, line 1102 terminate called after throwing an instance of 'cv::Exception' what(): /home/santhosh/opencv/opencv-3.1.0-source/modules/video/src/optflowgf.cpp:1102: error: (-215) prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyr_scale < 1 in function calcOpticalFlowFarneback

    ReplyDelete
    Replies
    1. Such as error message,
      Check your 2 image that prev and next is same size, 1 ch or not.
      Thank you.

      Delete

Post a Comment

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

Optical Flow sample source code using OpenCV

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

SICK LMS511 sensor data acquisition interface (source code, C++/MFC)

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)