mexopencv  3.4.1
MEX interface for OpenCV library
TransientAreasSegmentationModule_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::bioinspired;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 
27  SegmentationParameters &params,
30 {
31  ptrdiff_t len = std::distance(first, last);
32  nargchk((len%2)==0);
33  for (; first != last; first += 2) {
34  string key(first->toString());
35  const MxArray& val = *(first + 1);
36  if (key == "ThresholdON")
37  params.thresholdON = val.toFloat();
38  else if (key == "ThresholdOFF")
39  params.thresholdOFF = val.toFloat();
40  else if (key == "LocalEnergyTemporalConstant")
41  params.localEnergy_temporalConstant = val.toFloat();
42  else if (key == "LocalEnergySpatialConstant")
43  params.localEnergy_spatialConstant = val.toFloat();
44  else if (key == "NeighborhoodEnergyTemporalConstant")
45  params.neighborhoodEnergy_temporalConstant = val.toFloat();
46  else if (key == "NeighborhoodEnergySpatialConstant")
47  params.neighborhoodEnergy_spatialConstant = val.toFloat();
48  else if (key == "ContextEnergyTemporalConstant")
49  params.contextEnergy_temporalConstant = val.toFloat();
50  else if (key == "ContextEnergySpatialConstant")
51  params.contextEnergy_spatialConstant = val.toFloat();
52  else
53  mexErrMsgIdAndTxt("mexopencv:error",
54  "Unrecognized option %s", key.c_str());
55  }
56 }
57 
63 {
64  const char *fields[] = {"ThresholdON", "ThresholdOFF",
65  "LocalEnergyTemporalConstant", "LocalEnergySpatialConstant",
66  "NeighborhoodEnergyTemporalConstant",
67  "NeighborhoodEnergySpatialConstant", "ContextEnergyTemporalConstant",
68  "ContextEnergySpatialConstant"};
69  MxArray s = MxArray::Struct(fields, 8);
70  s.set("ThresholdON", params.thresholdON);
71  s.set("ThresholdOFF", params.thresholdOFF);
72  s.set("LocalEnergyTemporalConstant", params.localEnergy_temporalConstant);
73  s.set("LocalEnergySpatialConstant", params.localEnergy_spatialConstant);
74  s.set("NeighborhoodEnergyTemporalConstant", params.neighborhoodEnergy_temporalConstant);
75  s.set("NeighborhoodEnergySpatialConstant", params.neighborhoodEnergy_spatialConstant);
76  s.set("ContextEnergyTemporalConstant", params.contextEnergy_temporalConstant);
77  s.set("ContextEnergySpatialConstant", params.contextEnergy_spatialConstant);
78  return s;
79 }
80 }
81 
89 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
90 {
91  // Check the number of arguments
92  nargchk(nrhs>=2 && nlhs<=1);
93 
94  // Argument vector
95  vector<MxArray> rhs(prhs, prhs+nrhs);
96  int id = rhs[0].toInt();
97  string method(rhs[1].toString());
98 
99  // Constructor is called. Create a new object from argument
100  if (method == "new") {
101  nargchk(nrhs==3 && nlhs<=1);
103  rhs[2].toSize());
104  plhs[0] = MxArray(last_id);
105  mexLock();
106  return;
107  }
108 
109  // Big operation switch
111  if (obj.empty())
112  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
113  if (method == "delete") {
114  nargchk(nrhs==2 && nlhs==0);
115  obj_.erase(id);
116  mexUnlock();
117  }
118  else if (method == "clear") {
119  nargchk(nrhs==2 && nlhs==0);
120  obj->clear();
121  }
122  else if (method == "load") {
123  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
124  string objname;
125  bool loadFromString = false;
126  for (int i=3; i<nrhs; i+=2) {
127  string key(rhs[i].toString());
128  if (key == "ObjName")
129  objname = rhs[i+1].toString();
130  else if (key == "FromString")
131  loadFromString = rhs[i+1].toBool();
132  else
133  mexErrMsgIdAndTxt("mexopencv:error",
134  "Unrecognized option %s", key.c_str());
135  }
136  /*
137  obj_[id] = (loadFromString ?
138  Algorithm::loadFromString<TransientAreasSegmentationModule>(rhs[2].toString(), objname) :
139  Algorithm::load<TransientAreasSegmentationModule>(rhs[2].toString(), objname));
140  */
142  // HACK: workaround for missing TransientAreasSegmentationModule::create()
143  FileStorage fs(rhs[2].toString(), FileStorage::READ +
144  (loadFromString ? FileStorage::MEMORY : 0));
145  if (!fs.isOpened())
146  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
147  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
148  if (fn.empty())
149  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
150  obj->read(fn);
151  //*/
152  }
153  else if (method == "save") {
154  nargchk(nrhs==3 && nlhs==0);
155  obj->save(rhs[2].toString());
156  }
157  else if (method == "empty") {
158  nargchk(nrhs==2 && nlhs<=1);
159  plhs[0] = MxArray(obj->empty());
160  }
161  else if (method == "getDefaultName") {
162  nargchk(nrhs==2 && nlhs<=1);
163  plhs[0] = MxArray(obj->getDefaultName());
164  }
165  else if (method == "getSize") {
166  nargchk(nrhs==2 && nlhs<=1);
167  plhs[0] = MxArray(obj->getSize());
168  }
169  else if (method == "setup") {
170  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
171  string segmentationParameterFile(rhs[2].toString());
172  bool applyDefaultSetupOnFailure = true;
173  for (int i=3; i<nrhs; i+=2) {
174  string key(rhs[i].toString());
175  if (key == "ApplyDefaultSetupOnFailure")
176  applyDefaultSetupOnFailure = rhs[i+1].toBool();
177  else
178  mexErrMsgIdAndTxt("mexopencv:error",
179  "Unrecognized option %s", key.c_str());
180  }
181  obj->setup(segmentationParameterFile, applyDefaultSetupOnFailure);
182  }
183  else if (method == "setupParameters") {
184  nargchk(nrhs>=2 && nlhs==0);
185  SegmentationParameters newParameters;
186  createSegmentationParameters(newParameters, rhs.begin() + 2, rhs.end());
187  obj->setup(newParameters);
188  }
189  else if (method == "getParameters") {
190  nargchk(nrhs==2 && nlhs<=1);
191  plhs[0] = toStruct(obj->getParameters());
192  }
193  else if (method == "printSetup") {
194  nargchk(nrhs==2 && nlhs<=1);
195  plhs[0] = MxArray(obj->printSetup());
196  }
197  else if (method == "write") {
198  nargchk(nrhs==3 && nlhs<=1);
199  string fname(rhs[2].toString());
200  if (nlhs > 0) {
201  FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
202  if (!fs.isOpened())
203  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
204  obj->write(fs);
205  plhs[0] = MxArray(fs.releaseAndGetString());
206  }
207  else
208  obj->write(fname);
209  }
210  else if (method == "run") {
211  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
212  int channelIndex = 0;
213  for (int i=3; i<nrhs; i+=2) {
214  string key(rhs[i].toString());
215  if (key == "ChannelIndex")
216  channelIndex = rhs[i+1].toInt();
217  else
218  mexErrMsgIdAndTxt("mexopencv:error",
219  "Unrecognized option %s", key.c_str());
220  }
221  Mat inputToSegment(rhs[2].toMat(CV_32F));
222  obj->run(inputToSegment, channelIndex);
223  }
224  else if (method == "getSegmentationPicture") {
225  nargchk(nrhs==2 && nlhs<=1);
226  Mat transientAreas;
227  obj->getSegmentationPicture(transientAreas);
228  plhs[0] = MxArray(transientAreas);
229  }
230  else if (method == "clearAllBuffers") {
231  nargchk(nrhs==2 && nlhs==0);
232  obj->clearAllBuffers();
233  }
234  else
235  mexErrMsgIdAndTxt("mexopencv:error",
236  "Unrecognized operation %s", method.c_str());
237 }
MxArray toStruct(const SegmentationParameters &params)
Convert segmentation parameters to scalar struct.
T empty(T... args)
T distance(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
STL namespace.
T end(T... args)
virtual bool isOpened() const
virtual void run(InputArray inputToSegment, const int channelIndex=0)=0
virtual void setup(String segmentationParameterFile="", const bool applyDefaultSetupOnFailure=true)=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
STL class.
void createSegmentationParameters(SegmentationParameters &params, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of SegmentationParameters using options in arguments.
virtual void clear()
map< int, Ptr< TransientAreasSegmentationModule > > obj_
Object container.
virtual String releaseAndGetString()
#define CV_32F
virtual void read(const FileNode &fn)
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...
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
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Definition: MxArray.hpp:312
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
virtual SegmentationParameters getParameters()=0
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
virtual void getSegmentationPicture(OutputArray transientAreas)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const