9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<SuperpixelSEEDS> > obj_;
29 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
35 vector<MxArray> rhs(prhs, prhs+nrhs);
36 int id = rhs[0].toInt();
37 string method(rhs[1].toString());
40 if (method ==
"new") {
41 nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=1);
43 int histogram_bins = 5;
44 bool double_step =
false;
45 for (
int i=5; i<nrhs; i+=2) {
46 string key(rhs[i].toString());
48 prior = rhs[i+1].toInt();
49 else if (key ==
"HistogramBins")
50 histogram_bins = rhs[i+1].toInt();
51 else if (key ==
"DoubleStep")
52 double_step = rhs[i+1].toBool();
54 mexErrMsgIdAndTxt(
"mexopencv:error",
55 "Unrecognized option %s", key.c_str());
57 vector<int> sz(rhs[2].toVector<int>());
58 if (sz.size() != 2 && sz.size() != 3)
59 mexErrMsgIdAndTxt(
"mexopencv:error",
"Incorrect size");
60 int image_width = sz[1],
62 image_channels = (sz.size() == 3 ? sz[2] : 1),
63 num_superpixels = rhs[3].toInt(),
64 num_levels = rhs[4].toInt();
65 obj_[++last_id] = createSuperpixelSEEDS(
66 image_width, image_height, image_channels,
67 num_superpixels, num_levels, prior, histogram_bins, double_step);
73 Ptr<SuperpixelSEEDS> obj = obj_[id];
74 if (method ==
"delete") {
78 else if (method ==
"clear") {
82 else if (method ==
"load") {
83 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
85 bool loadFromString =
false;
86 for (
int i=3; i<nrhs; i+=2) {
87 string key(rhs[i].toString());
89 objname = rhs[i+1].toString();
90 else if (key ==
"FromString")
91 loadFromString = rhs[i+1].toBool();
93 mexErrMsgIdAndTxt(
"mexopencv:error",
94 "Unrecognized option %s", key.c_str());
103 FileStorage fs(rhs[2].toString(), FileStorage::READ +
104 (loadFromString ? FileStorage::MEMORY : 0));
105 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
107 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
110 else if (method ==
"save") {
112 obj->save(rhs[2].toString());
114 else if (method ==
"empty") {
116 plhs[0] =
MxArray(obj->empty());
118 else if (method ==
"getDefaultName") {
120 plhs[0] =
MxArray(obj->getDefaultName());
122 else if (method ==
"iterate") {
123 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
124 int num_iterations = 4;
125 for (
int i=3; i<nrhs; i+=2) {
126 string key(rhs[i].toString());
127 if (key ==
"NumIterations")
128 num_iterations = rhs[i+1].toInt();
130 mexErrMsgIdAndTxt(
"mexopencv:error",
131 "Unrecognized option %s", key.c_str());
133 Mat img(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
134 (rhs[2].isUint16() ? CV_16U : CV_32F)));
135 obj->iterate(img, num_iterations);
137 else if (method ==
"getNumberOfSuperpixels") {
139 plhs[0] =
MxArray(obj->getNumberOfSuperpixels());
141 else if (method ==
"getLabels") {
144 obj->getLabels(labels_out);
147 else if (method ==
"getLabelContourMask") {
148 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
149 bool thick_line =
false;
150 for (
int i=2; i<nrhs; i+=2) {
151 string key(rhs[i].toString());
152 if (key ==
"ThickLine")
153 thick_line = rhs[i+1].toBool();
155 mexErrMsgIdAndTxt(
"mexopencv:error",
156 "Unrecognized option %s", key.c_str());
159 obj->getLabelContourMask(image, thick_line);
163 mexErrMsgIdAndTxt(
"mexopencv:error",
164 "Unrecognized operation %s", method.c_str());
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.