mexopencv  3.4.1
MEX interface for OpenCV library
matchTemplate.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
16  ("SqDiff", cv::TM_SQDIFF)
17  ("SqDiffNormed", cv::TM_SQDIFF_NORMED)
18  ("CCorr", cv::TM_CCORR)
19  ("CCorrNormed", cv::TM_CCORR_NORMED)
20  ("CCoeff", cv::TM_CCOEFF)
21  ("CCoeffNormed", cv::TM_CCOEFF_NORMED);
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38 
39  // Option processing
40  int method = cv::TM_SQDIFF;
41  Mat mask;
42  for (int i=2; i<nrhs; i+=2) {
43  string key(rhs[i].toString());
44  if (key == "Method")
45  method = (rhs[i+1].isChar() ?
46  MatchMethod[rhs[i+1].toString()] : rhs[i+1].toInt());
47  else if (key == "Mask")
48  mask = rhs[i+1].toMat(CV_8U);
49  else
50  mexErrMsgIdAndTxt("mexopencv:error",
51  "Unrecognized option %s", key.c_str());
52  }
53 
54  // Process
55  Mat image(rhs[0].toMat(rhs[0].isUint8() ? CV_8U : CV_32F)),
56  templ(rhs[1].toMat(rhs[1].isUint8() ? CV_8U : CV_32F)),
57  result;
58  matchTemplate(image, templ, result, method, mask);
59  plhs[0] = MxArray(result);
60 }
TM_CCOEFF
TM_SQDIFF_NORMED
#define CV_8U
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
TM_CCORR_NORMED
const ConstMap< string, int > MatchMethod
type of the template matching operation
#define CV_32F
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
TM_SQDIFF
void matchTemplate(InputArray image, InputArray templ, OutputArray result, int method, InputArray mask=noArray())
TM_CCOEFF_NORMED
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T c_str(T... args)
TM_CCORR
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const