Commit 12e1e11d authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #68 from tpietruszka/deepflow

Deepflow implementation (and optical flow testing again)
parents 0f9cb5f8 d07bf35b
......@@ -48,6 +48,69 @@ See [Tao2012]_. And site of project - http://graphics.berkeley.edu/papers/Tao-SA
.. note::
* An example using the simpleFlow algorithm can be found at samples/simpleflow_demo.cpp
optflow::OpticalFlowDeepFlow
-----------------------------
DeepFlow optical flow algorithm implementation.
.. ocv:class:: optflow::OpticalFlowDeepFlow: public DenseOpticalFlow
The class implements the DeepFlow optical flow algorithm described in [Weinzaepfel2013]_ . See also http://lear.inrialpes.fr/src/deepmatching/ .
Parameters - class fields - that may be modified after creating a class instance:
.. ocv:member:: float alpha
Smoothness assumption weight
.. ocv:member:: float delta
Color constancy assumption weight
.. ocv:member:: float gamma
Gradient constancy weight
.. ocv:member:: float sigma
Gaussian smoothing parameter
.. ocv:member:: int minSize
Minimal dimension of an image in the pyramid (next, smaller images in the pyramid are
generated until one of the dimensions reaches this size)
.. ocv:member:: float downscaleFactor
Scaling factor in the image pyramid (must be < 1)
.. ocv:member:: int fixedPointIterations
How many iterations on each level of the pyramid
.. ocv:member:: int sorIterations
Iterations of Succesive Over-Relaxation (solver)
.. ocv:member:: float omega
Relaxation factor in SOR
optflow::createOptFlow_DeepFlow
---------------------------------
Create an OpticalFlowDeepFlow instance
.. ocv:function:: Ptr<DenseOpticalFlow> optflow::createOptFlow_DeepFlow()
Returns a pointer to a ``DenseOpticalFlow`` instance - see ``DenseOpticalFlow::calc()``
.. [Tao2012] Michael Tao, Jiamin Bai, Pushmeet Kohli and Sylvain Paris. SimpleFlow: A Non-iterative, Sublinear Optical Flow Algorithm. Computer Graphics Forum (Eurographics 2012)
.. [Weinzaepfel2013] P. Weinzaepfel, J. Revaud, Z. Harchaoui, and C. Schmid, “DeepFlow: Large Displacement Optical Flow with Deep Matching,” 2013 IEEE Int. Conf. Comput. Vis., pp. 1385–1392, Dec. 2013.
......@@ -11,3 +11,4 @@ The opencv_optflow module includes different algorithms for tracking points
dense_optflow
motion_templates
optflow_io
Optical flow input / output
============================
Functions reading and writing .flo files in "Middlebury" format, see: [MiddleburyFlo]_
readOpticalFlow
-----------------
Read a .flo file
.. ocv:function:: Mat readOpticalFlow( const String& path )
:param path: Path to the file to be loaded
The function readOpticalFlow loads a flow field from a file and returns it as a single matrix.
Resulting ``Mat`` has a type ``CV_32FC2`` - floating-point, 2-channel.
First channel corresponds to the flow in the horizontal direction (u), second - vertical (v).
writeOpticalFlow
-----------------
Write a .flo to disk
.. ocv:function:: bool writeOpticalFlow( const String& path, InputArray flow )
:param path: Path to the file to be written
:param flow: Flow field to be stored
The function stores a flow field in a file, returns ``true`` on success, ``false`` otherwise.
The flow field must be a 2-channel, floating-point matrix (``CV_32FC2``).
First channel corresponds to the flow in the horizontal direction (u), second - vertical (v).
.. [MiddleburyFlo] http://vision.middlebury.edu/flow/code/flow-code/README.txt
\ No newline at end of file
......@@ -41,6 +41,7 @@ the use of this software, even if advised of the possibility of such damage.
#define __OPENCV_OPTFLOW_HPP__
#include "opencv2/core.hpp"
#include "opencv2/video.hpp"
namespace cv
{
......@@ -57,8 +58,26 @@ CV_EXPORTS_W void calcOpticalFlowSF( InputArray from, InputArray to, OutputArray
double sigma_dist_fix, double sigma_color_fix, double occ_thr,
int upscale_averaging_radius, double upscale_sigma_dist,
double upscale_sigma_color, double speed_up_thr );
}
//! reads optical flow from a file, Middlebury format:
// http://vision.middlebury.edu/flow/code/flow-code/README.txt
CV_EXPORTS_W Mat readOpticalFlow( const String& path );
//! writes optical flow to a file, Middlebury format
CV_EXPORTS_W bool writeOpticalFlow( const String& path, InputArray flow );
// DeepFlow implementation, based on:
// P. Weinzaepfel, J. Revaud, Z. Harchaoui, and C. Schmid, “DeepFlow: Large Displacement Optical Flow with Deep Matching,”
CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_DeepFlow();
// Additional interface to the SimpleFlow algorithm - calcOpticalFlowSF()
CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_SimpleFlow();
// Additional interface to the Farneback's algorithm - calcOpticalFlowFarneback()
CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_Farneback();
} //optflow
}
#include "opencv2/optflow/motempl.hpp"
......
This diff is collapsed.
This diff is collapsed.
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/video.hpp"
#include "opencv2/optflow.hpp"
namespace cv
{
namespace optflow
{
class OpticalFlowSimpleFlow : public DenseOpticalFlow
{
public:
OpticalFlowSimpleFlow();
void calc(InputArray I0, InputArray I1, InputOutputArray flow);
void collectGarbage();
// AlgorithmInfo* info() const;
protected:
int layers;
int averaging_radius;
int max_flow;
double sigma_dist;
double sigma_color;
int postprocess_window;
double sigma_dist_fix;
double sigma_color_fix;
double occ_thr;
int upscale_averaging_radius;
double upscale_sigma_dist;
double upscale_sigma_color;
double speed_up_thr;
};
OpticalFlowSimpleFlow::OpticalFlowSimpleFlow()
{
// values from the example app
layers = 3;
averaging_radius = 2;
max_flow = 4;
// values from the default function parameters
sigma_dist = 4.1;
sigma_color = 25.5;
postprocess_window = 18;
sigma_dist_fix = 55.0;
sigma_color_fix = 25.5;
occ_thr = 0.35;
upscale_averaging_radius = 18;
upscale_sigma_dist = 55.0;
upscale_sigma_color = 25.5;
speed_up_thr = 10;
}
void OpticalFlowSimpleFlow::calc(InputArray I0, InputArray I1, InputOutputArray flow)
{
optflow::calcOpticalFlowSF(I0, I1, flow, layers, averaging_radius, max_flow, sigma_dist, sigma_color,
postprocess_window, sigma_dist_fix, sigma_color_fix, occ_thr,
upscale_averaging_radius, upscale_sigma_dist, upscale_sigma_color, speed_up_thr);
}
void OpticalFlowSimpleFlow::collectGarbage()
{
}
//CV_INIT_ALGORITHM(OpticalFlowSimpleFlow, "DenseOpticalFlow.SimpleFlow"),
// obj.info()->addParam(obj, "layers", obj.layers);
// obj.info()->addParam(obj, "averaging_radius", obj.averaging_radius);
// obj.info()->addParam(obj, "max_flow", obj.max_flow);
// obj.info()->addParam(obj, "sigma_dist", obj.sigma_dist);
// obj.info()->addParam(obj, "sigma_color", obj.sigma_color);
// obj.info()->addParam(obj, "postprocess_window", obj.postprocess_window);
// obj.info()->addParam(obj, "sigma_dist_fix", obj.sigma_dist_fix);
// obj.info()->addParam(obj, "sigma_color_fix", obj.sigma_color_fix);
// obj.info()->addParam(obj, "occ_thr", obj.occ_thr);
// obj.info()->addParam(obj, "upscale_averaging_radius", obj.upscale_averaging_radius);
// obj.info()->addParam(obj, "upscale_sigma_dist", obj.upscale_sigma_dist);
// obj.info()->addParam(obj, "upscale_sigma_color", obj.upscale_sigma_color);
// obj.info()->addParam(obj, "speed_up_thr", obj.speed_up_thr))
Ptr<DenseOpticalFlow> createOptFlow_SimpleFlow()
{
return makePtr<OpticalFlowSimpleFlow>();
}
class OpticalFlowFarneback : public DenseOpticalFlow
{
public:
OpticalFlowFarneback();
void calc(InputArray I0, InputArray I1, InputOutputArray flow);
void collectGarbage();
// AlgorithmInfo* info() const;
protected:
int numLevels;
double pyrScale;
bool fastPyramids;
int winSize;
int numIters;
int polyN;
double polySigma;
int flags;
};
OpticalFlowFarneback::OpticalFlowFarneback()
{
// values copied from the FarnebackOpticalFlow class
numLevels = 5;
pyrScale = 0.5;
fastPyramids = false;
winSize = 13;
numIters = 10;
polyN = 5;
polySigma = 1.1;
flags = 0;
}
void OpticalFlowFarneback::calc(InputArray I0, InputArray I1, InputOutputArray flow)
{
calcOpticalFlowFarneback(I0, I1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
}
void OpticalFlowFarneback::collectGarbage()
{
}
//CV_INIT_ALGORITHM(OpticalFlowFarneback, "DenseOpticalFlow.Farneback",
// obj.info()->addParam(obj, "numLevels", obj.numLevels);
// obj.info()->addParam(obj, "pyrScale", obj.pyrScale);
// obj.info()->addParam(obj, "fastPyramids", obj.fastPyramids);
// obj.info()->addParam(obj, "winSize", obj.winSize);
// obj.info()->addParam(obj, "numIters", obj.numIters);
// obj.info()->addParam(obj, "polyN", obj.polyN);
// obj.info()->addParam(obj, "polySigma", obj.polySigma);
// obj.info()->addParam(obj, "flags", obj.flags))
Ptr<DenseOpticalFlow> createOptFlow_Farneback()
{
return makePtr<OpticalFlowFarneback>();
}
}
}
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include<iostream>
#include<fstream>
namespace cv {
namespace optflow {
const float FLOW_TAG_FLOAT = 202021.25f;
const char *FLOW_TAG_STRING = "PIEH";
CV_EXPORTS_W Mat readOpticalFlow( const String& path )
{
// CV_Assert(sizeof(float) == 4);
//FIXME: ensure right sizes of int and float - here and in writeOpticalFlow()
Mat_<Point2f> flow;
std::ifstream file(path.c_str(), std::ios_base::binary);
if ( !file.good() )
return flow; // no file - return empty matrix
float tag;
file.read((char*) &tag, sizeof(float));
if ( tag != FLOW_TAG_FLOAT )
return flow;
int width, height;
file.read((char*) &width, 4);
file.read((char*) &height, 4);
flow.create(height, width);
for ( int i = 0; i < flow.rows; ++i )
{
for ( int j = 0; j < flow.cols; ++j )
{
Point2f u;
file.read((char*) &u.x, sizeof(float));
file.read((char*) &u.y, sizeof(float));
if ( !file.good() )
{
flow.release();
return flow;
}
flow(i, j) = u;
}
}
file.close();
return flow;
}
CV_EXPORTS_W bool writeOpticalFlow( const String& path, InputArray flow )
{
// CV_Assert(sizeof(float) == 4);
const int nChannels = 2;
Mat input = flow.getMat();
if ( input.channels() != nChannels || input.depth() != CV_32F || path.length() == 0 )
return false;
std::ofstream file(path.c_str(), std::ofstream::binary);
if ( !file.good() )
return false;
int nRows, nCols;
nRows = (int) input.size().height;
nCols = (int) input.size().width;
const int headerSize = 12;
char header[headerSize];
memcpy(header, FLOW_TAG_STRING, 4);
// size of ints is known - has been asserted in the current function
memcpy(header + 4, reinterpret_cast<const char*>(&nCols), sizeof(nCols));
memcpy(header + 8, reinterpret_cast<const char*>(&nRows), sizeof(nRows));
file.write(header, headerSize);
if ( !file.good() )
return false;
// if ( input.isContinuous() ) //matrix is continous - treat it as a single row
// {
// nCols *= nRows;
// nRows = 1;
// }
int row;
char* p;
for ( row = 0; row < nRows; row++ )
{
p = input.ptr<char>(row);
file.write(p, nCols * nChannels * sizeof(float));
if ( !file.good() )
return false;
}
file.close();
return true;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment