Dense Optical Flow in JavaCV (DualTVL1)

asked 2014-04-04 08:41:18 -0600

Dawit gravatar image

updated 2014-04-04 08:49:25 -0600

berak gravatar image

I have implemented dense optical flow to compute the velocity of every pixel in c++ using OpenCV 2.4.8 previously. This example helps me a lot (https://github.com/Itseez/opencv/blob/master/samples/cpp/tvl1_optical_flow.cpp). Now I want to convert my code to javaCV but i could’t. my code in c++ using OpenCV is like this:

Mat New_Previous_Gray (holds the previous image)
Mat New_Current_Gray (holds the current image)
Mat_<Point2f> Optical_Flow;
Ptr<DenseOpticalFlow> tvl1 = createOptFlow_DualTVL1();  
tvl1->calc(New_Previous_Gray, New_Current_Gray, Optical_Flow); 
for(int y = 0; y < Optical_Flow.cols; y++)
{
   for(int x = 0; x < Optical_Flow.rows; x++)
   {
      const Point2f& flow_xy = Optical_Flow.at<Point2f>(x, y);
      int Vel_x = flow_xy.x;
      int Vel_y = flow_xy.y;
      Pxl_Distance = sqrt(double(((abs(Vel_x) * abs(Vel_x)) + (abs(Vel_y) * abs(Vel_y)))));
   }
}

so i could get the velocity for every pixel in "Pxl_Distance".

Now I want to do the same thing in JavaCv but so far what what i could do is:

CvMat pFrame = pFrameGray.asCvMat();
CvMat cFrame = pFrameGray.asCvMat();
CvMat flow = Flowipl.asCvMat();
cvConvertImage(pFrame, pFrame, CV_CVTIMG_FLIP);
cvConvertImage(cFrame, cFrame, CV_CVTIMG_FLIP);
cvConvertImage(flow, flow, CV_CVTIMG_FLIP); 
DenseOpticalFlow tvl1 = createOptFlow_DualTVL1();
tvl1.calc(pFrame, cFrame, flow);

But when i view the pixel values of "flow"(CvMat) all are 0. i don't know what i have done wrong or if am doing it right when i compute the DenseOpticalFlow in the first place. i need help. i already spend too much time on this. Thanks in advance.

edit retag flag offensive close merge delete