mexopencv  3.4.1
MEX interface for OpenCV library
calcBackProject.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 {
14 void check_arguments(int hist_dims, bool uniform,
15  const vector<vector<float> > &ranges, const vector<int> &channels)
16 {
17  // some sanity checks not covered in cv::calcBackProject
18  if (!channels.empty() && channels.size() < hist_dims)
19  mexErrMsgIdAndTxt("mexopencv:error",
20  "Channels must match histogram dimensionality");
21  if ((!ranges.empty() || !uniform) && ranges.size() != hist_dims)
22  mexErrMsgIdAndTxt("mexopencv:error",
23  "Ranges must match histogram dimensionality");
24  if (!uniform) {
25  for (vector<vector<float> >::const_iterator it = ranges.begin(); it != ranges.end(); ++it)
26  if (it->empty())
27  mexErrMsgIdAndTxt("mexopencv:error",
28  "Ranges cannot be empty for non-uniform histogram");
29  }
30 }
31 
33 int histogram_dims(const SparseMat &hist)
34 {
35  return ((hist.dims() > 2) ? hist.dims() :
36  ((hist.size(0) == 1 || hist.size(1) == 1) ? 1 : 2));
37 }
38 
40 int histogram_dims(const MatND &hist)
41 {
42  return ((hist.dims > 2) ? hist.dims :
43  ((hist.rows == 1 || hist.cols == 1) ? 1 : 2));
44 }
45 }
46 
54 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
55 {
56  // Check the number of arguments
57  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
58 
59  // Argument vector
60  vector<MxArray> rhs(prhs, prhs+nrhs);
61 
62  // source arrays (cell array of images)
63  vector<Mat> arrays;
64  {
65  vector<MxArray> arrays_(rhs[0].toVector<MxArray>());
66  arrays.reserve(arrays_.size());
67  for (vector<MxArray>::const_iterator it = arrays_.begin(); it != arrays_.end(); ++it)
68  arrays.push_back(it->toMat(it->isUint8() ? CV_8U :
69  (it->isUint16() ? CV_16U : CV_32F)));
70  }
71 
72  // channels: default to use all channels from all images
73  int total_channels = 0;
74  for (vector<Mat>::const_iterator it = arrays.begin(); it != arrays.end(); ++it)
75  total_channels += it->channels();
76  vector<int> channels(total_channels);
77  for (int i=0; i<total_channels; ++i)
78  channels[i] = i;
79 
80  // ranges (cell array of vectors): bin boundaries in each hist dimension
81  vector<vector<float> > ranges(MxArrayToVectorVectorPrimitive<float>(rhs[2]));
82  mwSize dims = ranges.size();
83  vector<const float*> ranges_ptr(dims);
84  for (mwIndex i=0; i<dims; ++i)
85  ranges_ptr[i] = (!ranges[i].empty() ? &ranges[i][0] : NULL);
86 
87  // Option processing
88  double scale = 1.0;
89  bool uniform = false;
90  for (int i=3; i<nrhs; i+=2) {
91  string key(rhs[i].toString());
92  if (key == "Channels")
93  channels = rhs[i+1].toVector<int>();
94  else if (key == "Scale")
95  scale = rhs[i+1].toDouble();
96  else if (key == "Uniform")
97  uniform = rhs[i+1].toBool();
98  else
99  mexErrMsgIdAndTxt("mexopencv:error",
100  "Unrecognized option %s", key.c_str());
101  }
102 
103  // Process
104  Mat backProject;
105  if (rhs[1].isSparse()) {
106  SparseMat hist(rhs[1].toSparseMat(CV_32F)); // 2D sparse matrix
107  check_arguments(histogram_dims(hist), uniform, ranges, channels);
108  calcBackProject((arrays.empty() ? NULL : &arrays[0]), arrays.size(),
109  (channels.empty() ? NULL : &channels[0]), hist, backProject,
110  (ranges_ptr.empty() ? NULL : &ranges_ptr[0]), scale, uniform);
111  }
112  else {
113  MatND hist(rhs[1].toMatND(CV_32F)); // multi-dim dense array
114  check_arguments(histogram_dims(hist), uniform, ranges, channels);
115  calcBackProject((arrays.empty() ? NULL : &arrays[0]), arrays.size(),
116  (channels.empty() ? NULL : &channels[0]), hist, backProject,
117  (ranges_ptr.empty() ? NULL : &ranges_ptr[0]), scale, uniform);
118  }
119  plhs[0] = MxArray(backProject);
120 }
void calcBackProject(const Mat *images, int nimages, const int *channels, InputArray hist, OutputArray backProject, const float **ranges, double scale=1, bool uniform=true)
T empty(T... args)
#define CV_8U
STL namespace.
int dims() const
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T push_back(T... args)
bool empty() const
void check_arguments(int hist_dims, bool uniform, const vector< vector< float > > &ranges, const vector< int > &channels)
#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
#define CV_16U
T size(T... args)
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T begin(T... args)
T c_str(T... args)
int channels() const
int histogram_dims(const MatND &hist)
determine histogram dimensionality: 1-D, 2-D, or N-D
int channels() const
const int * size() const
T reserve(T... args)