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