mexopencv  3.4.1
MEX interface for OpenCV library
compareHist.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  ("Correlation", cv::HISTCMP_CORREL)
17  ("ChiSquare", cv::HISTCMP_CHISQR)
18  ("Intersection", cv::HISTCMP_INTERSECT)
19  ("Bhattacharyya", cv::HISTCMP_BHATTACHARYYA)
20  ("Hellinger", cv::HISTCMP_HELLINGER)
21  ("AltChiSquare", cv::HISTCMP_CHISQR_ALT)
22  ("KullbackLeibler", cv::HISTCMP_KL_DIV);
23 }
24 
32 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
33 {
34  // Check the number of arguments
35  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
36 
37  // Argument vector
38  vector<MxArray> rhs(prhs, prhs+nrhs);
39 
40  // Option processing
41  int method = cv::HISTCMP_CORREL;
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  HistComp[rhs[i+1].toString()] : rhs[i+1].toInt();
47  else
48  mexErrMsgIdAndTxt("mexopencv:error",
49  "Unrecognized option %s", key.c_str());
50  }
51 
52  // Process
53  double d = 0;
54  if (rhs[0].isSparse() && rhs[1].isSparse()) {
55  SparseMat H1(rhs[0].toSparseMat(CV_32F)),
56  H2(rhs[1].toSparseMat(CV_32F));
57  d = compareHist(H1, H2, method);
58  }
59  else {
60  MatND H1(rhs[0].toMatND(CV_32F)),
61  H2(rhs[1].toMatND(CV_32F));
62  d = compareHist(H1, H2, method);
63  }
64  plhs[0] = MxArray(d);
65 }
double compareHist(InputArray H1, InputArray H2, int method)
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
#define CV_32F
HISTCMP_CORREL
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
HISTCMP_CHISQR
STL class.
HISTCMP_CHISQR_ALT
Global constant definitions.
const ConstMap< string, int > HistComp
Histogram comparison methods.
Definition: compareHist.cpp:15
T c_str(T... args)
HISTCMP_INTERSECT
HISTCMP_KL_DIV
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
HISTCMP_HELLINGER
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: compareHist.cpp:32
HISTCMP_BHATTACHARYYA