mexopencv  3.4.1
MEX interface for OpenCV library
mexopencv_shape.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv_shape.hpp"
9 #include <typeinfo>
10 using std::vector;
11 using std::string;
12 using namespace cv;
13 
14 
15 // ==================== XXX ====================
16 
18 {
20  if (!p.empty()) {
21  s.set("TypeId", string(typeid(*p).name()));
22  s.set("NDummies", p->getNDummies());
23  s.set("DefaultCost", p->getDefaultCost());
24  {
27  if (!pp.empty())
28  s.set("NormFlag", DistTypeInv[pp->getNormFlag()]);
29  }
30  {
33  if (!pp.empty())
34  s.set("NormFlag", DistTypeInv[pp->getNormFlag()]);
35  }
36  }
37  return s;
38 }
39 
41 {
43  if (!p.empty()) {
44  s.set("TypeId", string(typeid(*p).name()));
45  {
48  if (!pp.empty())
49  s.set("RegularizationParameter", pp->getRegularizationParameter());
50  }
51  {
53  if (!pp.empty())
54  s.set("FullAffine", pp->getFullAffine());
55  }
56  }
57  return s;
58 }
59 
60 
61 // ==================== XXX ====================
62 
66 {
67  ptrdiff_t len = std::distance(first, last);
68  nargchk((len%2)==0);
69  int flag = cv::DIST_L2;
70  int nDummies = 25;
71  float defaultCost = 0.2f;
72  for (; first != last; first += 2) {
73  string key(first->toString());
74  const MxArray& val = *(first + 1);
75  if (key == "NDummies")
76  nDummies = val.toInt();
77  else if (key == "DefaultCost")
78  defaultCost = val.toFloat();
79  else if (key == "NormFlag")
80  flag = DistType[val.toString()];
81  else
82  mexErrMsgIdAndTxt("mexopencv:error",
83  "Unrecognized option %s", key.c_str());
84  }
85  return createNormHistogramCostExtractor(flag, nDummies, defaultCost);
86 }
87 
91 {
92  ptrdiff_t len = std::distance(first, last);
93  nargchk((len%2)==0);
94  int flag = cv::DIST_L2;
95  int nDummies = 25;
96  float defaultCost = 0.2f;
97  for (; first != last; first += 2) {
98  string key(first->toString());
99  const MxArray& val = *(first + 1);
100  if (key == "NDummies")
101  nDummies = val.toInt();
102  else if (key == "DefaultCost")
103  defaultCost = val.toFloat();
104  else if (key == "NormFlag")
105  flag = DistType[val.toString()];
106  else
107  mexErrMsgIdAndTxt("mexopencv:error",
108  "Unrecognized option %s", key.c_str());
109  }
110  return createEMDHistogramCostExtractor(flag, nDummies, defaultCost);
111 }
112 
116 {
117  ptrdiff_t len = std::distance(first, last);
118  nargchk((len%2)==0);
119  int nDummies = 25;
120  float defaultCost = 0.2f;
121  for (; first != last; first += 2) {
122  string key(first->toString());
123  const MxArray& val = *(first + 1);
124  if (key == "NDummies")
125  nDummies = val.toInt();
126  else if (key == "DefaultCost")
127  defaultCost = val.toFloat();
128  else
129  mexErrMsgIdAndTxt("mexopencv:error",
130  "Unrecognized option %s", key.c_str());
131  }
132  return createChiHistogramCostExtractor(nDummies, defaultCost);
133 }
134 
138 {
139  ptrdiff_t len = std::distance(first, last);
140  nargchk((len%2)==0);
141  int nDummies = 25;
142  float defaultCost = 0.2f;
143  for (; first != last; first += 2) {
144  string key(first->toString());
145  const MxArray& val = *(first + 1);
146  if (key == "NDummies")
147  nDummies = val.toInt();
148  else if (key == "DefaultCost")
149  defaultCost = val.toFloat();
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized option %s", key.c_str());
153  }
154  return createEMDL1HistogramCostExtractor(nDummies, defaultCost);
155 }
156 
158  const string& type,
161 {
163  if (type == "NormHistogramCostExtractor")
164  p = create_NormHistogramCostExtractor(first, last);
165  else if (type == "EMDHistogramCostExtractor")
166  p = create_EMDHistogramCostExtractor(first, last);
167  else if (type == "ChiHistogramCostExtractor")
168  p = create_ChiHistogramCostExtractor(first, last);
169  else if (type == "EMDL1HistogramCostExtractor")
170  p = create_EMDL1HistogramCostExtractor(first, last);
171  else
172  mexErrMsgIdAndTxt("mexopencv:error",
173  "Unrecognized histogram cost extractor %s", type.c_str());
174  if (p.empty())
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Failed to create HistogramCostExtractor");
177  return p;
178 }
179 
183 {
184  ptrdiff_t len = std::distance(first, last);
185  nargchk((len%2)==0);
186  double regularizationParameter = 0;
187  for (; first != last; first += 2) {
188  string key(first->toString());
189  const MxArray& val = *(first + 1);
190  if (key == "RegularizationParameter")
191  regularizationParameter = val.toDouble();
192  else
193  mexErrMsgIdAndTxt("mexopencv:error",
194  "Unrecognized option %s", key.c_str());
195  }
196  return createThinPlateSplineShapeTransformer(regularizationParameter);
197 }
198 
202 {
203  ptrdiff_t len = std::distance(first, last);
204  nargchk((len%2)==0);
205  bool fullAffine = true;
206  for (; first != last; first += 2) {
207  string key(first->toString());
208  const MxArray& val = *(first + 1);
209  if (key == "FullAffine")
210  fullAffine = val.toBool();
211  else
212  mexErrMsgIdAndTxt("mexopencv:error",
213  "Unrecognized option %s", key.c_str());
214  }
215  return createAffineTransformer(fullAffine);
216 }
217 
219  const string& type,
222 {
224  if (type == "ThinPlateSplineShapeTransformer")
226  else if (type == "AffineTransformer")
227  p = create_AffineTransformer(first, last);
228  else
229  mexErrMsgIdAndTxt("mexopencv:error",
230  "Unrecognized shape transformer %s", type.c_str());
231  if (p.empty())
232  mexErrMsgIdAndTxt("mexopencv:error",
233  "Failed to create ShapeTransformer");
234  return p;
235 }
236 
240 {
241  ptrdiff_t len = std::distance(first, last);
242  nargchk((len%2)==0);
243  int nAngularBins = 12;
244  int nRadialBins = 4;
245  float innerRadius = 0.2f;
246  float outerRadius = 2;
247  int iterations = 3;
249  Ptr<ShapeTransformer> transformer;
250  for (; first != last; first += 2) {
251  string key(first->toString());
252  const MxArray& val = *(first + 1);
253  if (key == "AngularBins")
254  nAngularBins = val.toInt();
255  else if (key == "RadialBins")
256  nRadialBins = val.toInt();
257  else if (key == "InnerRadius")
258  innerRadius = val.toFloat();
259  else if (key == "OuterRadius")
260  outerRadius = val.toFloat();
261  else if (key == "Iterations")
262  iterations = val.toInt();
263  else if (key == "CostExtractor") {
264  vector<MxArray> args(val.toVector<MxArray>());
265  nargchk(args.size() >= 1);
267  args[0].toString(), args.begin() + 1, args.end());
268  }
269  else if (key == "TransformAlgorithm") {
270  vector<MxArray> args(val.toVector<MxArray>());
271  nargchk(args.size() >= 1);
272  transformer = create_ShapeTransformer(
273  args[0].toString(), args.begin() + 1, args.end());
274  }
275  else
276  mexErrMsgIdAndTxt("mexopencv:error",
277  "Unrecognized option %s", key.c_str());
278  }
279  if (comparer.empty())
280  comparer = createChiHistogramCostExtractor();
281  if (transformer.empty())
283  return createShapeContextDistanceExtractor(nAngularBins, nRadialBins,
284  innerRadius, outerRadius, iterations, comparer, transformer);
285 }
286 
290 {
291  ptrdiff_t len = std::distance(first, last);
292  nargchk((len%2)==0);
293  int distanceFlag = cv::NORM_L2;
294  float rankProp = 0.6f;
295  for (; first != last; first += 2) {
296  string key(first->toString());
297  const MxArray& val = *(first + 1);
298  if (key == "DistanceFlag")
299  distanceFlag = NormType[val.toString()];
300  else if (key == "RankProportion")
301  rankProp = val.toFloat();
302  else
303  mexErrMsgIdAndTxt("mexopencv:error",
304  "Unrecognized option %s", key.c_str());
305  }
306  return createHausdorffDistanceExtractor(distanceFlag, rankProp);
307 }
Common definitions for the shape module.
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
Ptr< HistogramCostExtractor > create_HistogramCostExtractor(const string &type, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of HistogramCostExtractor using options in arguments.
T distance(T... args)
MxArray toStruct(Ptr< HistogramCostExtractor > p)
Convert a HistogramCostExtractor to MxArray.
virtual double getRegularizationParameter() const=0
Ptr< ShapeContextDistanceExtractor > create_ShapeContextDistanceExtractor(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of ShapeContextDistanceExtractor using options in arguments.
virtual int getNDummies() const=0
Ptr< ShapeContextDistanceExtractor > createShapeContextDistanceExtractor(int nAngularBins=12, int nRadialBins=4, float innerRadius=0.2f, float outerRadius=2, int iterations=3, const Ptr< HistogramCostExtractor > &comparer=createChiHistogramCostExtractor(), const Ptr< ShapeTransformer > &transformer=createThinPlateSplineShapeTransformer())
Ptr< HistogramCostExtractor > createChiHistogramCostExtractor(int nDummies=25, float defaultCost=0.2f)
T end(T... args)
Ptr< HistogramCostExtractor > create_NormHistogramCostExtractor(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of NormHistogramCostExtractor using options in arguments.
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
virtual int getNormFlag() const=0
Ptr< ShapeTransformer > create_ShapeTransformer(const string &type, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of ShapeTransformer using options in arguments.
const ConstMap< int, std::string > DistTypeInv
Inverse Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:113
STL class.
Ptr< HistogramCostExtractor > create_EMDL1HistogramCostExtractor(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of EMDL1HistogramCostExtractor using options in arguments.
Ptr< HausdorffDistanceExtractor > create_HausdorffDistanceExtractor(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of HausdorffDistanceExtractor using options in arguments.
Ptr< HistogramCostExtractor > create_EMDHistogramCostExtractor(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of EMDHistogramCostExtractor using options in arguments.
Ptr< HistogramCostExtractor > createEMDL1HistogramCostExtractor(int nDummies=25, float defaultCost=0.2f)
Ptr< HausdorffDistanceExtractor > createHausdorffDistanceExtractor(int distanceFlag=cv::NORM_L2, float rankProp=0.6f)
bool toBool() const
Convert MxArray to bool.
Definition: MxArray.cpp:510
Ptr< Y > dynamicCast() const
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...
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:102
virtual int getNormFlag() const=0
T size(T... args)
STL class.
bool empty() const
T begin(T... args)
Ptr< HistogramCostExtractor > createEMDHistogramCostExtractor(int flag=DIST_L2, int nDummies=25, float defaultCost=0.2f)
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
Ptr< HistogramCostExtractor > createNormHistogramCostExtractor(int flag=DIST_L2, int nDummies=25, float defaultCost=0.2f)
Ptr< AffineTransformer > createAffineTransformer(bool fullAffine)
virtual bool getFullAffine() const=0
Ptr< ThinPlateSplineShapeTransformer > createThinPlateSplineShapeTransformer(double regularizationParameter=0)
int type() const
Ptr< HistogramCostExtractor > create_ChiHistogramCostExtractor(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of ChiHistogramCostExtractor using options in arguments.
Ptr< AffineTransformer > create_AffineTransformer(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of AffineTransformer using options in arguments.
Ptr< ThinPlateSplineShapeTransformer > create_ThinPlateSplineShapeTransformer(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of ThinPlateSplineShapeTransformer using options in arguments. ...
const ConstMap< std::string, int > NormType
Norm type map for option processing.
Definition: mexopencv.hpp:150
virtual float getDefaultCost() const=0