JavaCV 中的密集光流 (DualTVL1)



我之前在 c++ 中实现了密集光流来计算每个像素的速度。这个例子对我帮助很大(https://github.com/Itseez/opencv/blob/master/samples/cpp/tvl1_optical_flow.cpp)。现在我想将我的代码转换为 java,但我找不到 createOptFlow_DualTVL1() 的等效 JavaCV 函数。有人可以帮我吗?如果有榜样就好了。

我使用 OpenCV 的 C++ 代码是这样的:

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();                 //This two lines is the want i want to change to JavaCV
tvl1->calc(New_Previous_Gray, New_Current_Gray, Optical_Flow);     //This two lines is the want i want to change to JavaCV
double Pxl_Distance[Optical_Flow.rows][Optical_Flow.rows]; //will hold the velocity for every pixel 
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)))));
Pxl_Distance[x][y] = Pxl_Distance;
}
}

提前致谢

到目前为止,我一直在研究这个问题,下面是我想出的计算光流的代码,任何人都可以告诉我我做对了

IplImage pFrameGray = cvCreateImage(cvGetSize(pFront),IPL_DEPTH_8U,1);
IplImage cFrameGray = cvCreateImage(cvGetSize(pFront),IPL_DEPTH_8U,1);
cvConvertImage(pFront, pFrameGray, CV_CVTIMG_FLIP);
cvConvertImage(cFront, cFrameGray, CV_CVTIMG_FLIP);
IplImage Optical_Flow  = cvCreateImage( cvSize(pFrameGray.width(),pFrameGray.height()), IPL_DEPTH_32F, 2 );
DenseOpticalFlow tvl1 = createOptFlow_DualTVL1();
tvl1.calc(pFrameGray, cFrameGray, Optical_Flow);

如果这是正确的,我如何从Optical_Flow(IplImage)获取光学值。

谢谢

public void TestOF()
{
   IplImage pFrame = cvLoadImage("/sdcard//DCIM/Camera/Frame-1.jpg");
   IplImage cFrame = cvLoadImage("/sdcard//DCIM/Camera/Frame-2.jpg");
   IplImage pGray = cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
   IplImage cGray = cvCreateImage(cvGetSize(cFrame),IPL_DEPTH_8U,1);
   cvConvertImage(pFrame, pGray, IPL_DEPTH_32F);
   cvConvertImage(cFrame, cGray, IPL_DEPTH_32F);
   IplImage Optical_Flow  = cvCreateImage( cvGetSize(pGray), IPL_DEPTH_32F, 2 );
   DenseOpticalFlow tvl1 = createOptFlow_DualTVL1();
   tvl1.calc(pGray, cGray, Optical_Flow);
   FloatBuffer buffer = Optical_Flow.getFloatBuffer(); 
   CvMat OF  = cvCreateMat( pGray.height(),pGray.width(), CV_32FC1);
   int pixelVelocity =0;
   int xVelocity = 0;
   int yVelocity = 0;
   int bufferIndex = 0;
   for(int y =0; y< pGray.height(); y++)
   {
      for(int x = 0; x < pGray.width() ;x++)
      {
         xVelocity = (int)buffer.get(bufferIndex);
         yVelocity = (int)buffer.get(bufferIndex+1);
         pixelVelocity = (int)Math.sqrt((double)(xVelocity*xVelocity +             yVelocity*yVelocity));
         OF.put(y, x, pixelVelocity);
     }
   }
   IplImage temp = OF.asIplImage();
   cvSaveImage("/sdcard//DCIM/Camera/OF.jpg",temp);
}

此代码将使用两个图像(Frame-1和Frame-2)之间的DenseOpticalFlow计算每个像素的光流,并将每个像素的速度放在其坐标中的另一个图像(OF)。干杯。

相关内容

  • 没有找到相关文章