9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<SuperpixelSLIC> > obj_;
23 (
"SLIC", cv::ximgproc::SLIC)
24 (
"SLICO", cv::ximgproc::SLICO);
34 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
40 vector<MxArray> rhs(prhs, prhs+nrhs);
41 int id = rhs[0].toInt();
42 string method(rhs[1].toString());
45 if (method ==
"new") {
46 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
47 int algorithm = cv::ximgproc::SLICO;
50 for (
int i=3; i<nrhs; i+=2) {
51 string key(rhs[i].toString());
52 if (key ==
"Algorithm")
53 algorithm = SLICAlgorithmMap[rhs[i+1].toString()];
54 else if (key ==
"RegionSize")
55 region_size = rhs[i+1].toInt();
56 else if (key ==
"Ruler")
57 ruler = rhs[i+1].toFloat();
59 mexErrMsgIdAndTxt(
"mexopencv:error",
60 "Unrecognized option %s", key.c_str());
62 Mat image(rhs[2].toMat());
63 obj_[++last_id] = createSuperpixelSLIC(
64 image, algorithm, region_size, ruler);
70 Ptr<SuperpixelSLIC> obj = obj_[id];
71 if (method ==
"delete") {
75 else if (method ==
"clear") {
79 else if (method ==
"load") {
80 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
82 bool loadFromString =
false;
83 for (
int i=3; i<nrhs; i+=2) {
84 string key(rhs[i].toString());
86 objname = rhs[i+1].toString();
87 else if (key ==
"FromString")
88 loadFromString = rhs[i+1].toBool();
90 mexErrMsgIdAndTxt(
"mexopencv:error",
91 "Unrecognized option %s", key.c_str());
100 FileStorage fs(rhs[2].toString(), FileStorage::READ +
101 (loadFromString ? FileStorage::MEMORY : 0));
102 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
104 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
107 else if (method ==
"save") {
109 obj->save(rhs[2].toString());
111 else if (method ==
"empty") {
113 plhs[0] =
MxArray(obj->empty());
115 else if (method ==
"getDefaultName") {
117 plhs[0] =
MxArray(obj->getDefaultName());
119 else if (method ==
"iterate") {
120 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
121 int num_iterations = 10;
122 for (
int i=2; i<nrhs; i+=2) {
123 string key(rhs[i].toString());
124 if (key ==
"NumIterations")
125 num_iterations = rhs[i+1].toInt();
127 mexErrMsgIdAndTxt(
"mexopencv:error",
128 "Unrecognized option %s", key.c_str());
130 obj->iterate(num_iterations);
132 else if (method ==
"getNumberOfSuperpixels") {
134 plhs[0] =
MxArray(obj->getNumberOfSuperpixels());
136 else if (method ==
"getLabels") {
139 obj->getLabels(labels_out);
142 else if (method ==
"getLabelContourMask") {
143 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
144 bool thick_line =
true;
145 for (
int i=2; i<nrhs; i+=2) {
146 string key(rhs[i].toString());
147 if (key ==
"ThickLine")
148 thick_line = rhs[i+1].toBool();
150 mexErrMsgIdAndTxt(
"mexopencv:error",
151 "Unrecognized option %s", key.c_str());
154 obj->getLabelContourMask(image, thick_line);
157 else if (method ==
"enforceLabelConnectivity") {
158 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
159 int min_element_size = 25;
160 for (
int i=2; i<nrhs; i+=2) {
161 string key(rhs[i].toString());
162 if (key ==
"MinElementSize")
163 min_element_size = rhs[i+1].toInt();
165 mexErrMsgIdAndTxt(
"mexopencv:error",
166 "Unrecognized option %s", key.c_str());
168 obj->enforceLabelConnectivity(min_element_size);
171 mexErrMsgIdAndTxt(
"mexopencv:error",
172 "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.
std::map wrapper with one-line initialization and lookup method.