13 void check_arguments(
int hist_dims,
bool uniform,
14 const vector<vector<float> > &ranges,
const vector<int> &channels)
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");
24 for (vector<vector<float> >::const_iterator it = ranges.begin(); it != ranges.end(); ++it)
26 mexErrMsgIdAndTxt(
"mexopencv:error",
27 "Ranges cannot be empty for non-uniform histogram");
32 int histogram_dims(
const SparseMat &hist)
34 return ((hist.dims() > 2) ? hist.dims() :
35 ((hist.size(0) == 1 || hist.size(1) == 1) ? 1 : 2));
39 int histogram_dims(
const MatND &hist)
41 return ((hist.dims > 2) ? hist.dims :
42 ((hist.rows == 1 || hist.cols == 1) ? 1 : 2));
53 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
56 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
59 vector<MxArray> rhs(prhs, prhs+nrhs);
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)));
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)
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);
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();
98 mexErrMsgIdAndTxt(
"mexopencv:error",
99 "Unrecognized option %s", key.c_str());
104 if (rhs[1].isSparse()) {
105 SparseMat hist(rhs[1].toSparseMat(CV_32F));
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);
112 MatND hist(rhs[1].toMatND(CV_32F));
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);
118 plhs[0] =
MxArray(backProject);
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.