mexopencv  3.4.1
MEX interface for OpenCV library
calcOpticalFlowFarneback.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/video.hpp"
10 using namespace std;
11 using namespace cv;
12 
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
21 {
22  // Check the number of arguments
23  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Option processing
29  Mat flow;
30  double pyrScale = 0.5;
31  int levels = 5;
32  int winsize = 13;
33  int iterations = 10;
34  int polyN = 5;
35  double polySigma = 1.1;
36  int flags = 0;
37  for (int i=2; i<nrhs; i+=2) {
38  string key(rhs[i].toString());
39  if (key == "InitialFlow") {
40  flow = rhs[i+1].toMat(CV_32F);
42  }
43  else if (key == "PyrScale")
44  pyrScale = rhs[i+1].toDouble();
45  else if (key == "Levels")
46  levels = rhs[i+1].toInt();
47  else if (key == "WinSize")
48  winsize = rhs[i+1].toInt();
49  else if (key == "Iterations")
50  iterations = rhs[i+1].toInt();
51  else if (key == "PolyN")
52  polyN = rhs[i+1].toInt();
53  else if (key == "PolySigma")
54  polySigma = rhs[i+1].toDouble();
55  else if (key == "Gaussian")
56  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::OPTFLOW_FARNEBACK_GAUSSIAN);
57  else
58  mexErrMsgIdAndTxt("mexopencv:error",
59  "Unrecognized option %s", key.c_str());
60  }
61 
62  // Process
63  Mat prevImg(rhs[0].toMat(CV_8U)),
64  nextImg(rhs[1].toMat(CV_8U));
65  calcOpticalFlowFarneback(prevImg, nextImg, flow, pyrScale, levels,
66  winsize, iterations, polyN, polySigma, flags);
67  plhs[0] = MxArray(flow);
68 }
#define CV_8U
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void calcOpticalFlowFarneback(InputArray prev, InputArray next, InputOutputArray flow, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
#define CV_32F
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:174
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
STL class.
Global constant definitions.
T c_str(T... args)
OPTFLOW_FARNEBACK_GAUSSIAN
cv::Mat toMat() const
OPTFLOW_USE_INITIAL_FLOW