mexopencv  3.4.1
MEX interface for OpenCV library
PCTSignatures_.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::xfeatures2d;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 
30 
36 }
37 
45 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
46 {
47  // Check the number of arguments
48  nargchk(nrhs>=2 && nlhs<=1);
49 
50  // Argument vector
51  vector<MxArray> rhs(prhs, prhs+nrhs);
52  int id = rhs[0].toInt();
53  string method(rhs[1].toString());
54 
55  // constructor call
56  if (method == "new") {
57  nargchk(nlhs<=1);
59  // second/third variants with custom sampling points
60  if (nrhs==4 && !rhs[2].isChar()) {
61  vector<Point2f> initSamplingPoints(rhs[2].toVector<Point2f>());
62  if (rhs[3].numel() == 1) {
63  int initSeedCount = rhs[3].toInt();
64  p = PCTSignatures::create(initSamplingPoints, initSeedCount);
65  }
66  else {
67  vector<int> initClusterSeedIndexes(rhs[3].toVector<int>());
68  p = PCTSignatures::create(initSamplingPoints, initClusterSeedIndexes);
69  }
70  }
71  // first variant
72  else {
73  nargchk(nrhs>=2 && (nrhs%2)==0);
74  int initSampleCount = 2000;
75  int initSeedCount = 400;
76  int pointDistribution = 0;
77  for (int i=2; i<nrhs; i+=2) {
78  string key(rhs[i].toString());
79  if (key == "InitSampleCount")
80  initSampleCount = rhs[i+1].toInt();
81  else if (key == "InitSeedCount")
82  initSeedCount = rhs[i+1].toInt();
83  else if (key == "PointDistributiontion")
84  pointDistribution = (rhs[i+1].isChar()) ?
85  PointDistributionMap[rhs[i+1].toString()] :
86  rhs[i+1].toInt();
87  else
88  mexErrMsgIdAndTxt("mexopencv:error",
89  "Unrecognized option %s", key.c_str());
90  }
92  initSampleCount, initSeedCount, pointDistribution);
93  }
94  obj_[++last_id] = p;
95  plhs[0] = MxArray(last_id);
96  mexLock();
97  return;
98  }
99  // static methods
100  else if (method == "generateInitPoints") {
101  nargchk(nrhs==4 && nlhs<=1);
102  int count = rhs[2].toInt();
103  int pointDistribution = (rhs[3].isChar()) ?
104  PointDistributionMap[rhs[3].toString()] : rhs[3].toInt();
105  vector<Point2f> initPoints;
106  PCTSignatures::generateInitPoints(initPoints, count, pointDistribution);
107  plhs[0] = MxArray(Mat(initPoints,false).reshape(1,0)); // N-by-2 numeric matrix
108  return;
109  }
110  else if (method == "drawSignature") {
111  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
112  float radiusToShorterSideRatio = 1.0f / 8;
113  int borderThickness = 1;
114  for (int i=4; i<nrhs; i+=2) {
115  string key(rhs[i].toString());
116  if (key == "RadiusToShorterSideRatio")
117  radiusToShorterSideRatio = rhs[i+1].toFloat();
118  else if (key == "BorderThickness")
119  borderThickness = rhs[i+1].toInt();
120  else
121  mexErrMsgIdAndTxt("mexopencv:error",
122  "Unrecognized option %s", key.c_str());
123  }
124  Mat source(rhs[2].toMat()),
125  signature(rhs[3].toMat(CV_32F)),
126  result;
127  PCTSignatures::drawSignature(source, signature, result,
128  radiusToShorterSideRatio, borderThickness);
129  plhs[0] = MxArray(result);
130  return;
131  }
132 
133  // Big operation switch
134  Ptr<PCTSignatures> obj = obj_[id];
135  if (obj.empty())
136  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
137  if (method == "delete") {
138  nargchk(nrhs==2 && nlhs==0);
139  obj_.erase(id);
140  mexUnlock();
141  }
142  else if (method == "clear") {
143  nargchk(nrhs==2 && nlhs==0);
144  obj->clear();
145  }
146  else if (method == "save") {
147  nargchk(nrhs==3 && nlhs==0);
148  obj->save(rhs[2].toString());
149  }
150  else if (method == "load") {
151  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
152  string objname;
153  bool loadFromString = false;
154  for (int i=3; i<nrhs; i+=2) {
155  string key(rhs[i].toString());
156  if (key == "ObjName")
157  objname = rhs[i+1].toString();
158  else if (key == "FromString")
159  loadFromString = rhs[i+1].toBool();
160  else
161  mexErrMsgIdAndTxt("mexopencv:error",
162  "Unrecognized option %s", key.c_str());
163  }
164  obj_[id] = (loadFromString ?
165  Algorithm::loadFromString<PCTSignatures>(rhs[2].toString(), objname) :
166  Algorithm::load<PCTSignatures>(rhs[2].toString(), objname));
167  }
168  else if (method == "empty") {
169  nargchk(nrhs==2 && nlhs<=1);
170  plhs[0] = MxArray(obj->empty());
171  }
172  else if (method == "getDefaultName") {
173  nargchk(nrhs==2 && nlhs<=1);
174  plhs[0] = MxArray(obj->getDefaultName());
175  }
176  else if (method == "computeSignature") {
177  nargchk(nrhs==3 && nlhs<=1);
178  Mat image(rhs[2].toMat(CV_8U)),
179  signature;
180  obj->computeSignature(image, signature);
181  plhs[0] = MxArray(signature);
182  }
183  else if (method == "computeSignatures") {
184  nargchk(nrhs==3 && nlhs<=1);
185  //vector<Mat> images(rhs[2].toVector<Mat>());
186  vector<Mat> images;
187  {
188  vector<MxArray> arr(rhs[2].toVector<MxArray>());
189  images.reserve(arr.size());
190  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
191  images.push_back(it->toMat(CV_8U));
192  }
193  vector<Mat> signatures;
194  obj->computeSignatures(images, signatures);
195  plhs[0] = MxArray(signatures);
196  }
197  else if (method == "getSampleCount") {
198  nargchk(nrhs==2 && nlhs<=1);
199  plhs[0] = MxArray(obj->getSampleCount());
200  }
201  else if (method == "getSamplingPoints") {
202  nargchk(nrhs==2 && nlhs<=1);
203  plhs[0] = MxArray(obj->getSamplingPoints());
204  }
205  else if (method == "setSamplingPoints") {
206  nargchk(nrhs==3 && nlhs==0);
207  obj->setSamplingPoints(rhs[2].toVector<Point2f>());
208  }
209  else if (method == "setWeight") {
210  nargchk(nrhs==4 && nlhs==0);
211  obj->setWeight(rhs[2].toInt(), rhs[3].toFloat());
212  }
213  else if (method == "setWeights") {
214  nargchk(nrhs==3 && nlhs==0);
215  obj->setWeights(rhs[2].toVector<float>());
216  }
217  else if (method == "setTranslation") {
218  nargchk(nrhs==4 && nlhs==0);
219  obj->setTranslation(rhs[2].toInt(), rhs[3].toFloat());
220  }
221  else if (method == "setTranslations") {
222  nargchk(nrhs==3 && nlhs==0);
223  obj->setTranslations(rhs[2].toVector<float>());
224  }
225  else if (method == "getInitSeedCount") {
226  nargchk(nrhs==2 && nlhs<=1);
227  plhs[0] = MxArray(obj->getInitSeedCount());
228  }
229  else if (method == "getInitSeedIndexes") {
230  nargchk(nrhs==2 && nlhs<=1);
231  plhs[0] = MxArray(obj->getInitSeedIndexes());
232  }
233  else if (method == "setInitSeedIndexes") {
234  nargchk(nrhs==3 && nlhs==0);
235  obj->setInitSeedIndexes(rhs[2].toVector<int>());
236  }
237  else if (method == "get") {
238  nargchk(nrhs==3 && nlhs<=1);
239  string prop(rhs[2].toString());
240  if (prop == "GrayscaleBits")
241  plhs[0] = MxArray(obj->getGrayscaleBits());
242  else if (prop == "WindowRadius")
243  plhs[0] = MxArray(obj->getWindowRadius());
244  else if (prop == "WeightX")
245  plhs[0] = MxArray(obj->getWeightX());
246  else if (prop == "WeightY")
247  plhs[0] = MxArray(obj->getWeightY());
248  else if (prop == "WeightL")
249  plhs[0] = MxArray(obj->getWeightL());
250  else if (prop == "WeightA")
251  plhs[0] = MxArray(obj->getWeightA());
252  else if (prop == "WeightB")
253  plhs[0] = MxArray(obj->getWeightB());
254  else if (prop == "WeightContrast")
255  plhs[0] = MxArray(obj->getWeightContrast());
256  else if (prop == "WeightEntropy")
257  plhs[0] = MxArray(obj->getWeightEntropy());
258  else if (prop == "IterationCount")
259  plhs[0] = MxArray(obj->getIterationCount());
260  else if (prop == "MaxClustersCount")
261  plhs[0] = MxArray(obj->getMaxClustersCount());
262  else if (prop == "ClusterMinSize")
263  plhs[0] = MxArray(obj->getClusterMinSize());
264  else if (prop == "JoiningDistance")
265  plhs[0] = MxArray(obj->getJoiningDistance());
266  else if (prop == "DropThreshold")
267  plhs[0] = MxArray(obj->getDropThreshold());
268  else if (prop == "DistanceFunction")
269  plhs[0] = MxArray(obj->getDistanceFunction());
270  else
271  mexErrMsgIdAndTxt("mexopencv:error",
272  "Unrecognized property %s", prop.c_str());
273  }
274  else if (method == "set") {
275  nargchk(nrhs==4 && nlhs==0);
276  string prop(rhs[2].toString());
277  if (prop == "GrayscaleBits")
278  obj->setGrayscaleBits(rhs[3].toInt());
279  else if (prop == "WindowRadius")
280  obj->setWindowRadius(rhs[3].toInt());
281  else if (prop == "WeightX")
282  obj->setWeightX(rhs[3].toFloat());
283  else if (prop == "WeightY")
284  obj->setWeightY(rhs[3].toFloat());
285  else if (prop == "WeightL")
286  obj->setWeightL(rhs[3].toFloat());
287  else if (prop == "WeightA")
288  obj->setWeightA(rhs[3].toFloat());
289  else if (prop == "WeightB")
290  obj->setWeightB(rhs[3].toFloat());
291  else if (prop == "WeightContrast")
292  obj->setWeightContrast(rhs[3].toFloat());
293  else if (prop == "WeightEntropy")
294  obj->setWeightEntropy(rhs[3].toFloat());
295  else if (prop == "IterationCount")
296  obj->setIterationCount(rhs[3].toInt());
297  else if (prop == "MaxClustersCount")
298  obj->setMaxClustersCount(rhs[3].toInt());
299  else if (prop == "ClusterMinSize")
300  obj->setClusterMinSize(rhs[3].toInt());
301  else if (prop == "JoiningDistance")
302  obj->setJoiningDistance(rhs[3].toFloat());
303  else if (prop == "DropThreshold")
304  obj->setDropThreshold(rhs[3].toFloat());
305  else if (prop == "DistanceFunction")
306  obj->setDistanceFunction(rhs[3].isChar() ?
307  DistanceFuncMap[rhs[3].toString()] : rhs[3].toInt());
308  else
309  mexErrMsgIdAndTxt("mexopencv:error",
310  "Unrecognized property %s", prop.c_str());
311  }
312  else
313  mexErrMsgIdAndTxt("mexopencv:error",
314  "Unrecognized operation %s", method.c_str());
315 }
virtual std::vector< Point2f > getSamplingPoints() const=0
virtual void setIterationCount(int iterationCount)=0
virtual void setJoiningDistance(float joiningDistance)=0
virtual void setWeight(int idx, float value)=0
virtual void setWeights(const std::vector< float > &weights)=0
virtual int getDistanceFunction() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setDropThreshold(float dropThreshold)=0
#define CV_8U
virtual float getWeightEntropy() const=0
virtual int getIterationCount() const=0
STL namespace.
virtual void setWeightA(float weight)=0
virtual int getSampleCount() const=0
virtual void setInitSeedIndexes(std::vector< int > initSeedIndexes)=0
virtual float getWeightA() const=0
T end(T... args)
virtual void setTranslations(const std::vector< float > &translations)=0
virtual std::vector< int > getInitSeedIndexes() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
virtual float getDropThreshold() const=0
STL class.
virtual void computeSignatures(const std::vector< Mat > &images, std::vector< Mat > &signatures) const=0
T push_back(T... args)
virtual void setWeightY(float weight)=0
virtual void setWindowRadius(int radius)=0
virtual void setWeightL(float weight)=0
virtual void clear()
#define CV_32F
virtual void setDistanceFunction(int distanceFunction)=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...
int last_id
Last object id to allocate.
virtual void setClusterMinSize(int clusterMinSize)=0
const ConstMap< string, int > DistanceFuncMap
Lp distance function selector for option processing.
const ConstMap< string, int > PointDistributionMap
Random point distributions for option processing.
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
T count(T... args)
map< int, Ptr< PCTSignatures > > obj_
Object container.
virtual float getWeightY() const=0
T size(T... args)
virtual float getWeightX() const=0
STL class.
bool empty() const
virtual void setSamplingPoints(std::vector< Point2f > samplingPoints)=0
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
virtual void setWeightEntropy(float weight)=0
T c_str(T... args)
virtual void setGrayscaleBits(int grayscaleBits)=0
virtual void setWeightB(float weight)=0
virtual int getWindowRadius() const=0
virtual int getGrayscaleBits() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void computeSignature(InputArray image, OutputArray signature) const=0
virtual float getWeightL() const=0
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
virtual void setWeightX(float weight)=0
virtual int getClusterMinSize() const=0
virtual int getInitSeedCount() const=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
virtual float getJoiningDistance() const=0
cv::Mat toMat() const
virtual float getWeightContrast() const=0
virtual void setTranslation(int idx, float value)=0
T reserve(T... args)
virtual int getMaxClustersCount() const=0
virtual void setWeightContrast(float weight)=0
virtual void setMaxClustersCount(int maxClustersCount)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual float getWeightB() const=0