mexopencv  3.4.1
MEX interface for OpenCV library
HfsSegment_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/hfs.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::hfs;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 
24  ("CPU", HFS_BACKEND_CPU)
25  ("GPU", HFS_BACKEND_GPU);
26 }
27 
35 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
36 {
37  // Check the number of arguments
38  nargchk(nrhs>=2 && nlhs<=1);
39 
40  // Argument vector
41  vector<MxArray> rhs(prhs, prhs+nrhs);
42  int id = rhs[0].toInt();
43  string method(rhs[1].toString());
44 
45  // Constructor is called. Create a new object from argument
46  if (method == "new") {
47  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
48  float segEgbThresholdI = 0.08f;
49  int minRegionSizeI = 100;
50  float segEgbThresholdII = 0.28f;
51  int minRegionSizeII = 200;
52  float spatialWeight = 0.6f;
53  int slicSpixelSize = 8;
54  int numSlicIter = 5;
55  for (int i=4; i<nrhs; i+=2) {
56  string key(rhs[i].toString());
57  if (key == "SegEgbThresholdI")
58  segEgbThresholdI = rhs[i+1].toFloat();
59  else if (key == "MinRegionSizeI")
60  minRegionSizeI = rhs[i+1].toInt();
61  else if (key == "SegEgbThresholdII")
62  segEgbThresholdII = rhs[i+1].toFloat();
63  else if (key == "MinRegionSizeII")
64  minRegionSizeII = rhs[i+1].toInt();
65  else if (key == "SpatialWeight")
66  spatialWeight = rhs[i+1].toFloat();
67  else if (key == "SlicSpixelSize")
68  slicSpixelSize = rhs[i+1].toInt();
69  else if (key == "NumSlicIter")
70  numSlicIter = rhs[i+1].toInt();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
75  int height = rhs[2].toInt(),
76  width = rhs[3].toInt();
77  obj_[++last_id] = HfsSegment::create(height, width,
78  segEgbThresholdI, minRegionSizeI, segEgbThresholdII,
79  minRegionSizeII, spatialWeight, slicSpixelSize, numSlicIter);
80  plhs[0] = MxArray(last_id);
81  mexLock();
82  return;
83  }
84 
85  // Big operation switch
86  Ptr<HfsSegment> obj = obj_[id];
87  if (obj.empty())
88  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
89  if (method == "delete") {
90  nargchk(nrhs==2 && nlhs==0);
91  obj_.erase(id);
92  mexUnlock();
93  }
94  else if (method == "clear") {
95  nargchk(nrhs==2 && nlhs==0);
96  obj->clear();
97  }
98  else if (method == "load") {
99  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
100  string objname;
101  bool loadFromString = false;
102  for (int i=3; i<nrhs; i+=2) {
103  string key(rhs[i].toString());
104  if (key == "ObjName")
105  objname = rhs[i+1].toString();
106  else if (key == "FromString")
107  loadFromString = rhs[i+1].toBool();
108  else
109  mexErrMsgIdAndTxt("mexopencv:error",
110  "Unrecognized option %s", key.c_str());
111  }
112  /*
113  obj_[id] = (loadFromString ?
114  Algorithm::loadFromString<HfsSegment>(rhs[2].toString(), objname) :
115  Algorithm::load<HfsSegment>(rhs[2].toString(), objname));
116  */
118  // HACK: workaround for missing HfsSegment::create()
119  FileStorage fs(rhs[2].toString(), FileStorage::READ +
120  (loadFromString ? FileStorage::MEMORY : 0));
121  if (!fs.isOpened())
122  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
123  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
124  if (fn.empty())
125  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
126  obj->read(fn);
127  //*/
128  }
129  else if (method == "save") {
130  nargchk(nrhs==3 && nlhs==0);
131  obj->save(rhs[2].toString());
132  }
133  else if (method == "empty") {
134  nargchk(nrhs==2 && nlhs<=1);
135  plhs[0] = MxArray(obj->empty());
136  }
137  else if (method == "getDefaultName") {
138  nargchk(nrhs==2 && nlhs<=1);
139  plhs[0] = MxArray(obj->getDefaultName());
140  }
141  else if (method == "performSegment") {
142  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
143  bool ifDraw = true;
144  int backend = HFS_BACKEND_CPU;
145  for (int i=3; i<nrhs; i+=2) {
146  string key(rhs[i].toString());
147  if (key == "Draw")
148  ifDraw = rhs[i+1].toBool();
149  else if (key == "Backend")
150  backend = BackendsMap[rhs[i+1].toString()];
151  else
152  mexErrMsgIdAndTxt("mexopencv:error",
153  "Unrecognized option %s", key.c_str());
154  }
155  Mat src(rhs[2].toMat()), dst;
156  dst = (backend == HFS_BACKEND_CPU) ?
157  obj->performSegmentCpu(src, ifDraw) :
158  obj->performSegmentGpu(src, ifDraw);
159  plhs[0] = MxArray(dst);
160  }
161  else if (method == "get") {
162  nargchk(nrhs==3 && nlhs<=1);
163  string prop(rhs[2].toString());
164  if (prop == "SegEgbThresholdI")
165  plhs[0] = MxArray(obj->getSegEgbThresholdI());
166  else if (prop == "MinRegionSizeI")
167  plhs[0] = MxArray(obj->getMinRegionSizeI());
168  else if (prop == "SegEgbThresholdII")
169  plhs[0] = MxArray(obj->getSegEgbThresholdII());
170  else if (prop == "MinRegionSizeII")
171  plhs[0] = MxArray(obj->getMinRegionSizeII());
172  else if (prop == "SpatialWeight")
173  plhs[0] = MxArray(obj->getSpatialWeight());
174  else if (prop == "SlicSpixelSize")
175  plhs[0] = MxArray(obj->getSlicSpixelSize());
176  else if (prop == "NumSlicIter")
177  plhs[0] = MxArray(obj->getNumSlicIter());
178  else
179  mexErrMsgIdAndTxt("mexopencv:error",
180  "Unrecognized property %s", prop.c_str());
181  }
182  else if (method == "set") {
183  nargchk(nrhs==4 && nlhs==0);
184  string prop(rhs[2].toString());
185  if (prop == "SegEgbThresholdI")
186  obj->setSegEgbThresholdI(rhs[3].toFloat());
187  else if (prop == "MinRegionSizeI")
188  obj->setMinRegionSizeI(rhs[3].toInt());
189  else if (prop == "SegEgbThresholdII")
190  obj->setSegEgbThresholdII(rhs[3].toFloat());
191  else if (prop == "MinRegionSizeII")
192  obj->setMinRegionSizeII(rhs[3].toInt());
193  else if (prop == "SpatialWeight")
194  obj->setSpatialWeight(rhs[3].toFloat());
195  else if (prop == "SlicSpixelSize")
196  obj->setSlicSpixelSize(rhs[3].toInt());
197  else if (prop == "NumSlicIter")
198  obj->setNumSlicIter(rhs[3].toInt());
199  else
200  mexErrMsgIdAndTxt("mexopencv:error",
201  "Unrecognized property %s", prop.c_str());
202  }
203  else
204  mexErrMsgIdAndTxt("mexopencv:error",
205  "Unrecognized operation %s", method.c_str());
206 }
virtual float getSegEgbThresholdII()=0
T empty(T... args)
virtual void setSpatialWeight(float w)=0
virtual void setMinRegionSizeII(int n)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
STL namespace.
virtual bool isOpened() const
virtual void setSlicSpixelSize(int n)=0
virtual int getMinRegionSizeII()=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setSegEgbThresholdII(float c)=0
virtual void clear()
virtual void read(const FileNode &fn)
virtual float getSpatialWeight()=0
virtual void setSegEgbThresholdI(float c)=0
virtual Mat performSegmentCpu(InputArray src, bool ifDraw=true)=0
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
virtual Mat performSegmentGpu(InputArray src, bool ifDraw=true)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
virtual void setNumSlicIter(int n)=0
FileNode getFirstTopLevelNode() const
virtual void setMinRegionSizeI(int n)=0
map< int, Ptr< HfsSegment > > obj_
Object container.
Definition: HfsSegment_.cpp:19
STL class.
bool empty() const
virtual String getDefaultName() const
virtual int getSlicSpixelSize()=0
Global constant definitions.
T c_str(T... args)
virtual int getMinRegionSizeI()=0
int last_id
Last object id to allocate.
Definition: HfsSegment_.cpp:17
virtual void save(const String &filename) const
virtual bool empty() const
const ConstMap< string, int > BackendsMap
Definition: HfsSegment_.cpp:23
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
virtual float getSegEgbThresholdI()=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
virtual int getNumSlicIter()=0
cv::Mat toMat() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: HfsSegment_.cpp:35