mexopencv  3.4.1
MEX interface for OpenCV library
ShapeTransformer_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "mexopencv_shape.hpp"
10 #include "opencv2/shape.hpp"
11 using namespace std;
12 using namespace cv;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=2);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs>=3 && nlhs<=1);
43  rhs[2].toString(), rhs.begin() + 3, rhs.end());
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48 
49  // Big operation switch
50  Ptr<ShapeTransformer> obj = obj_[id];
51  if (obj.empty())
52  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
53  if (method == "delete") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj_.erase(id);
56  mexUnlock();
57  }
58  else if (method == "clear") {
59  nargchk(nrhs==2 && nlhs==0);
60  obj->clear();
61  }
62  else if (method == "load") {
63  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
64  string objname;
65  bool loadFromString = false;
66  for (int i=3; i<nrhs; i+=2) {
67  string key(rhs[i].toString());
68  if (key == "ObjName")
69  objname = rhs[i+1].toString();
70  else if (key == "FromString")
71  loadFromString = rhs[i+1].toBool();
72  else
73  mexErrMsgIdAndTxt("mexopencv:error",
74  "Unrecognized option %s", key.c_str());
75  }
76  /*
77  obj_[id] = (loadFromString ?
78  Algorithm::loadFromString<ShapeTransformer>(rhs[2].toString(), objname) :
79  Algorithm::load<ShapeTransformer>(rhs[2].toString(), objname));
80  */
82  // HACK: ShapeTransformer is abstract, use polymorphic obj->read
83  FileStorage fs(rhs[2].toString(), FileStorage::READ +
84  (loadFromString ? FileStorage::MEMORY : 0));
85  if (!fs.isOpened())
86  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
87  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
88  if (fn.empty())
89  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
90  obj->read(fn);
91  //*/
92  }
93  else if (method == "save") {
94  nargchk(nrhs==3 && nlhs==0);
95  obj->save(rhs[2].toString());
96  }
97  else if (method == "empty") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->empty());
100  }
101  else if (method == "getDefaultName") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(obj->getDefaultName());
104  }
105  else if (method == "estimateTransformation") {
106  nargchk(nrhs==5 && nlhs==0);
107  vector<DMatch> matches(rhs[4].toVector<DMatch>());
108  if (rhs[2].isNumeric() && rhs[3].isNumeric()) {
109  // contours expected as 1xNx2
110  Mat transformingShape(rhs[2].toMat(CV_32F).reshape(2,1)),
111  targetShape(rhs[3].toMat(CV_32F).reshape(2,1));
112  obj->estimateTransformation(transformingShape, targetShape, matches);
113  }
114  else if (rhs[2].isCell() && rhs[3].isCell()) {
115  vector<Point2f> transformingShape(rhs[2].toVector<Point2f>()),
116  targetShape(rhs[3].toVector<Point2f>());
117  obj->estimateTransformation(transformingShape, targetShape, matches);
118  }
119  else
120  mexErrMsgIdAndTxt("mexopencv:error", "Invalid contour argument");
121  }
122  else if (method == "applyTransformation") {
123  nargchk(nrhs==3 && nlhs<=2);
124  float transformCost = 0;
125  if (rhs[2].isNumeric()) {
126  // Nx2/1xNx2/Nx1x2 -> 1xNx2
127  Mat input(rhs[2].toMat(CV_32F).reshape(2,1)),
128  output;
129  transformCost = obj->applyTransformation(input,
130  (nlhs>1 ? output : noArray()));
131  if (nlhs > 1)
132  // 1xNx2 -> Nx2
133  plhs[1] = MxArray(output.reshape(1, output.cols));
134  }
135  else if (rhs[2].isCell() && rhs[3].isCell()) {
136  vector<Point2f> input(rhs[2].toVector<Point2f>()),
137  output;
138  transformCost = obj->applyTransformation(input,
139  (nlhs>1 ? output : noArray()));
140  if (nlhs > 1)
141  plhs[1] = MxArray(output);
142  }
143  plhs[0] = MxArray(transformCost);
144  }
145  else if (method == "warpImage") {
146  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
147  int flags = cv::INTER_LINEAR;
148  int borderMode = cv::BORDER_CONSTANT;
149  Scalar borderValue;
150  for (int i=3; i<nrhs; i+=2) {
151  string key(rhs[i].toString());
152  if (key == "Interpolation")
153  flags = (rhs[i+1].isChar()) ?
154  InterpType[rhs[i+1].toString()] : rhs[i+1].toInt();
155  else if (key == "BorderType")
156  borderMode = (rhs[i+1].isChar()) ?
157  BorderType[rhs[i+1].toString()] : rhs[i+1].toInt();
158  else if (key == "BorderValue")
159  borderValue = rhs[i+1].toScalar();
160  else
161  mexErrMsgIdAndTxt("mexopencv:error",
162  "Unrecognized option %s", key.c_str());
163  }
164  Mat transformingImage(rhs[2].toMat()),
165  output;
166  obj->warpImage(transformingImage, output,
167  flags, borderMode, borderValue);
168  plhs[0] = MxArray(output);
169  }
170  else if (method == "get") {
171  nargchk(nrhs==3 && nlhs<=1);
172  string prop(rhs[2].toString());
173  if (prop == "RegularizationParameter") {
176  if (p.empty())
177  mexErrMsgIdAndTxt("mexopencv:error",
178  "Only supported for ThinPlateSplineShapeTransformer");
179  plhs[0] = MxArray(p->getRegularizationParameter());
180  }
181  else if (prop == "FullAffine") {
183  if (p.empty())
184  mexErrMsgIdAndTxt("mexopencv:error",
185  "Only supported for AffineTransformer");
186  plhs[0] = MxArray(p->getFullAffine());
187  }
188  else
189  mexErrMsgIdAndTxt("mexopencv:error",
190  "Unrecognized property %s", prop.c_str());
191  }
192  else if (method == "set") {
193  nargchk(nrhs==4 && nlhs==0);
194  string prop(rhs[2].toString());
195  if (prop == "RegularizationParameter") {
198  if (p.empty())
199  mexErrMsgIdAndTxt("mexopencv:error",
200  "Only supported for ThinPlateSplineShapeTransformer");
201  p->setRegularizationParameter(rhs[3].toDouble());
202  }
203  else if (prop == "FullAffine") {
205  if (p.empty())
206  mexErrMsgIdAndTxt("mexopencv:error",
207  "Only supported for AffineTransformer");
208  p->setFullAffine(rhs[3].toBool());
209  }
210  else
211  mexErrMsgIdAndTxt("mexopencv:error",
212  "Unrecognized property %s", prop.c_str());
213  }
214  else
215  mexErrMsgIdAndTxt("mexopencv:error",
216  "Unrecognized operation %s", method.c_str());
217 }
Common definitions for the shape module.
T empty(T... args)
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.
virtual void warpImage(InputArray transformingImage, OutputArray output, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar()) const=0
virtual double getRegularizationParameter() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
const ConstMap< std::string, int > InterpType
Interpolation type map for option processing.
Definition: mexopencv.hpp:85
STL namespace.
virtual void estimateTransformation(InputArray transformingShape, InputArray targetShape, std::vector< DMatch > &matches)=0
virtual void setFullAffine(bool fullAffine)=0
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
map< int, Ptr< ShapeTransformer > > obj_
Object container.
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
Mat reshape(int cn, int rows=0) const
Ptr< Y > dynamicCast() const
InputOutputArray noArray()
virtual void setRegularizationParameter(double beta)=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...
const ConstMap< std::string, int > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:66
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
FileNode getFirstTopLevelNode() const
int last_id
Last object id to allocate.
STL class.
bool empty() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
INTER_LINEAR
T c_str(T... args)
virtual float applyTransformation(InputArray input, OutputArray output=noArray())=0
BORDER_CONSTANT
virtual void save(const String &filename) const
virtual bool empty() const
virtual bool getFullAffine() const=0
cv::Mat toMat() const