9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<SuperpixelLSC> > 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>=3 && (nrhs%2)==1 && nlhs<=1);
44 for (
int i=3; i<nrhs; i+=2) {
45 string key(rhs[i].toString());
46 if (key ==
"RegionSize")
47 region_size = rhs[i+1].toInt();
48 else if (key ==
"Ratio")
49 ratio = rhs[i+1].toFloat();
51 mexErrMsgIdAndTxt(
"mexopencv:error",
52 "Unrecognized option %s", key.c_str());
54 Mat image(rhs[2].toMat());
55 obj_[++last_id] = createSuperpixelLSC(image, region_size, ratio);
61 Ptr<SuperpixelLSC> obj = obj_[id];
62 if (method ==
"delete") {
66 else if (method ==
"clear") {
70 else if (method ==
"load") {
71 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
73 bool loadFromString =
false;
74 for (
int i=3; i<nrhs; i+=2) {
75 string key(rhs[i].toString());
77 objname = rhs[i+1].toString();
78 else if (key ==
"FromString")
79 loadFromString = rhs[i+1].toBool();
81 mexErrMsgIdAndTxt(
"mexopencv:error",
82 "Unrecognized option %s", key.c_str());
91 FileStorage fs(rhs[2].toString(), FileStorage::READ +
92 (loadFromString ? FileStorage::MEMORY : 0));
93 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
95 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
98 else if (method ==
"save") {
100 obj->save(rhs[2].toString());
102 else if (method ==
"empty") {
104 plhs[0] =
MxArray(obj->empty());
106 else if (method ==
"getDefaultName") {
108 plhs[0] =
MxArray(obj->getDefaultName());
110 else if (method ==
"iterate") {
111 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
112 int num_iterations = 10;
113 for (
int i=2; i<nrhs; i+=2) {
114 string key(rhs[i].toString());
115 if (key ==
"NumIterations")
116 num_iterations = rhs[i+1].toInt();
118 mexErrMsgIdAndTxt(
"mexopencv:error",
119 "Unrecognized option %s", key.c_str());
121 obj->iterate(num_iterations);
123 else if (method ==
"getNumberOfSuperpixels") {
125 plhs[0] =
MxArray(obj->getNumberOfSuperpixels());
127 else if (method ==
"getLabels") {
130 obj->getLabels(labels_out);
133 else if (method ==
"getLabelContourMask") {
134 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
135 bool thick_line =
true;
136 for (
int i=2; i<nrhs; i+=2) {
137 string key(rhs[i].toString());
138 if (key ==
"ThickLine")
139 thick_line = rhs[i+1].toBool();
141 mexErrMsgIdAndTxt(
"mexopencv:error",
142 "Unrecognized option %s", key.c_str());
145 obj->getLabelContourMask(image, thick_line);
148 else if (method ==
"enforceLabelConnectivity") {
149 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
150 int min_element_size = 20;
151 for (
int i=2; i<nrhs; i+=2) {
152 string key(rhs[i].toString());
153 if (key ==
"MinElementSize")
154 min_element_size = rhs[i+1].toInt();
156 mexErrMsgIdAndTxt(
"mexopencv:error",
157 "Unrecognized option %s", key.c_str());
159 obj->enforceLabelConnectivity(min_element_size);
162 mexErrMsgIdAndTxt(
"mexopencv:error",
163 "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.