13 int check_arguments(
int dims,
bool uniform,
14 const vector<vector<float> > &ranges,
const vector<int> &channels,
15 const vector<int> &histSize)
19 if (!histSize.empty() && !ranges.empty()) {
20 if (histSize.size() != ranges.size())
21 mexErrMsgIdAndTxt(
"mexopencv:error",
22 "HistSize must match histogram dimensionality");
24 for (mwIndex i=0; i<ranges.size(); ++i) {
25 if (histSize[i] != (ranges[i].size() - 1))
26 mexErrMsgIdAndTxt(
"mexopencv:error",
27 "HistSize must match non-uniform ranges");
31 else if (!histSize.empty() && ranges.empty()) {
33 dims = histSize.size();
35 if (!channels.empty() && channels.size() < dims)
36 mexErrMsgIdAndTxt(
"mexopencv:error",
37 "Channels must match histogram dimensionality");
49 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
52 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
55 vector<MxArray> rhs(prhs, prhs+nrhs);
60 vector<MxArray> arrays_(rhs[0].toVector<MxArray>());
61 arrays.reserve(arrays_.size());
62 for (vector<MxArray>::const_iterator it = arrays_.begin(); it != arrays_.end(); ++it)
63 arrays.push_back(it->toMat(it->isUint8() ? CV_8U :
64 (it->isUint16() ? CV_16U : CV_32F)));
68 int total_channels = 0;
69 for (vector<Mat>::const_iterator it = arrays.begin(); it != arrays.end(); ++it)
70 total_channels += it->channels();
71 vector<int> channels(total_channels);
72 for (
int i=0; i<total_channels; ++i)
76 vector<vector<float> > ranges(MxArrayToVectorVectorPrimitive<float>(rhs[1]));
77 int dims =
static_cast<int>(ranges.size());
78 vector<const float*> ranges_ptr(dims);
79 for (mwIndex i=0; i<dims; ++i)
80 ranges_ptr[i] = (!ranges[i].empty() ? &ranges[i][0] : NULL);
83 vector<int> histSize(dims);
84 for (mwIndex i=0; i<dims; ++i)
85 histSize[i] = (!ranges[i].empty() ? ranges[i].size() - 1 : 0);
90 MxArray hist0(static_cast<mxArray*>(NULL));
91 bool accumulate =
false;
93 for (
int i=2; i<nrhs; i+=2) {
94 string key(rhs[i].toString());
95 if (key ==
"Channels")
96 channels = rhs[i+1].toVector<
int>();
97 else if (key ==
"Mask")
98 mask = rhs[i+1].toMat(CV_8U);
99 else if (key ==
"HistSize")
100 histSize = rhs[i+1].toVector<
int>();
101 else if (key ==
"Uniform")
102 uniform = rhs[i+1].toBool();
103 else if (key ==
"Hist") {
107 else if (key ==
"Sparse")
108 sparse = rhs[i+1].
toBool();
110 mexErrMsgIdAndTxt(
"mexopencv:error",
111 "Unrecognized option %s", key.c_str());
115 dims = check_arguments(dims, uniform, ranges, channels, histSize);
119 calcHist((!arrays.empty() ? &arrays[0] : NULL), arrays.size(),
120 (!channels.empty() ? &channels[0] : NULL), mask, hist, dims,
121 (!histSize.empty() ? &histSize[0] : NULL),
122 (!ranges_ptr.empty() ? &ranges_ptr[0] : NULL),
123 uniform, accumulate);
128 if (accumulate) hist = hist0.
toMatND(CV_32F);
129 calcHist((!arrays.empty() ? &arrays[0] : NULL), arrays.size(),
130 (!channels.empty() ? &channels[0] : NULL), mask, hist, dims,
131 (!histSize.empty() ? &histSize[0] : NULL),
132 (!ranges_ptr.empty() ? &ranges_ptr[0] : NULL),
133 uniform, accumulate);
cv::MatND toMatND(int depth=CV_USRTYPE1, bool transpose=true) const
Convert MxArray to a single-channel cv::Mat.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
bool toBool() const
Convert MxArray to bool.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
cv::SparseMat toSparseMat(int depth=CV_USRTYPE1) const
Convert double sparse MxArray to 2D single-channel cv::SparseMat.