mexopencv  0.1
mex interface for opencv library
calcOpticalFlowPyrLK.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  vector<Point2f> nextPts;
29  Size winSize(21,21);
30  int maxLevel = 3;
31  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
32  int flags = 0;
33  double minEigThreshold = 1e-4;
34  for (int i=3; i<nrhs; i+=2) {
35  string key(rhs[i].toString());
36  if (key=="InitialFlow") {
37  nextPts = rhs[i+1].toVector<Point2f>();
38  flags |= cv::OPTFLOW_USE_INITIAL_FLOW;
39  }
40  else if (key=="WinSize")
41  winSize = rhs[i+1].toSize();
42  else if (key=="MaxLevel")
43  maxLevel = rhs[i+1].toInt();
44  else if (key=="Criteria")
45  criteria = rhs[i+1].toTermCriteria();
46  else if (key=="GetMinEigenvals")
47  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::OPTFLOW_LK_GET_MIN_EIGENVALS);
48  else if (key=="MinEigThreshold")
49  minEigThreshold = rhs[i+1].toDouble();
50  else
51  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
52  }
53 
54  // Process
55  vector<Point2f> prevPts(rhs[2].toVector<Point2f>());
56  Mat status, err;
57  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
58  // images
59  Mat prevImg(rhs[0].toMat(CV_8U)),
60  nextImg(rhs[1].toMat(CV_8U));
61  calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, nextPts, status,
62  (nlhs>2 ? err : noArray()), winSize, maxLevel, criteria, flags,
63  minEigThreshold);
64  }
65  else if (rhs[0].isCell() && rhs[1].isCell()) {
66  // pyramids
67  vector<Mat> prevImg(rhs[0].toVector<Mat>()),
68  nextImg(rhs[1].toVector<Mat>());
69  calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, nextPts, status,
70  (nlhs>2 ? err : noArray()), winSize, maxLevel, criteria, flags,
71  minEigThreshold);
72  }
73  else
74  mexErrMsgIdAndTxt("mexopencv:error","Invalid argument");
75  plhs[0] = MxArray(nextPts);
76  if (nlhs>1)
77  plhs[1] = MxArray(status);
78  if (nlhs>2)
79  plhs[2] = MxArray(err);
80 }
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:159
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.