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