mexopencv  3.4.1
MEX interface for OpenCV library
AlignMTB_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/photo.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 
28 {
29  ptrdiff_t len = std::distance(first, last);
30  nargchk((len%2)==0);
31  int max_bits = 6;
32  int exclude_range = 4;
33  bool cut = true;
34  for (; first != last; first += 2) {
35  string key(first->toString());
36  const MxArray& val = *(first + 1);
37  if (key == "MaxBits")
38  max_bits = val.toInt();
39  else if (key == "ExcludeRange")
40  exclude_range = val.toInt();
41  else if (key == "Cut")
42  cut = val.toBool();
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47  return createAlignMTB(max_bits, exclude_range, cut);
48 }
49 }
50 
58 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
59 {
60  // Check the number of arguments
61  nargchk(nrhs>=2 && nlhs<=2);
62 
63  // Argument vector
64  vector<MxArray> rhs(prhs, prhs+nrhs);
65  int id = rhs[0].toInt();
66  string method(rhs[1].toString());
67 
68  // Constructor is called. Create a new object from argument
69  if (method == "new") {
70  nargchk(nrhs>=2 && nlhs<=1);
72  rhs.begin() + 2, rhs.end());
73  plhs[0] = MxArray(last_id);
74  mexLock();
75  return;
76  }
77 
78  // Big operation switch
79  Ptr<AlignMTB> obj = obj_[id];
80  if (obj.empty())
81  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
82  if (method == "delete") {
83  nargchk(nrhs==2 && nlhs==0);
84  obj_.erase(id);
85  mexUnlock();
86  }
87  else if (method == "clear") {
88  nargchk(nrhs==2 && nlhs==0);
89  obj->clear();
90  }
91  else if (method == "load") {
92  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
93  string objname;
94  bool loadFromString = false;
95  for (int i=3; i<nrhs; i+=2) {
96  string key(rhs[i].toString());
97  if (key == "ObjName")
98  objname = rhs[i+1].toString();
99  else if (key == "FromString")
100  loadFromString = rhs[i+1].toBool();
101  else
102  mexErrMsgIdAndTxt("mexopencv:error",
103  "Unrecognized option %s", key.c_str());
104  }
105  /*
106  obj_[id] = (loadFromString ?
107  Algorithm::loadFromString<AlignMTB>(rhs[2].toString(), objname) :
108  Algorithm::load<AlignMTB>(rhs[2].toString(), objname));
109  */
111  // HACK: workaround for missing AlignMTB::create()
112  FileStorage fs(rhs[2].toString(), FileStorage::READ +
113  (loadFromString ? FileStorage::MEMORY : 0));
114  if (!fs.isOpened())
115  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
116  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
117  if (fn.empty())
118  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
119  obj->read(fn);
120  //*/
121  }
122  else if (method == "save") {
123  nargchk(nrhs==3 && nlhs==0);
124  obj->save(rhs[2].toString());
125  }
126  else if (method == "empty") {
127  nargchk(nrhs==2 && nlhs<=1);
128  plhs[0] = MxArray(obj->empty());
129  }
130  else if (method == "getDefaultName") {
131  nargchk(nrhs==2 && nlhs<=1);
132  plhs[0] = MxArray(obj->getDefaultName());
133  }
134  else if (method == "process") {
135  nargchk(nrhs==3 && nlhs<=1);
136  vector<Mat> src, dst;
137  {
138  vector<MxArray> arr(rhs[2].toVector<MxArray>());
139  src.reserve(arr.size());
140  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
141  src.push_back(it->toMat(CV_8U));
142  }
143  obj->process(src, dst);
144  plhs[0] = MxArray(dst);
145  }
146  else if (method == "calculateShift") {
147  nargchk(nrhs==4 && nlhs<=1);
148  Mat img0(rhs[2].toMat(CV_8U)),
149  img1(rhs[3].toMat(CV_8U));
150  Point shift = obj->calculateShift(img0, img1);
151  plhs[0] = MxArray(shift);
152  }
153  else if (method == "shiftMat") {
154  nargchk(nrhs==4 && nlhs<=1);
155  Mat src(rhs[2].toMat()), dst;
156  Point shift(rhs[3].toPoint());
157  obj->shiftMat(src, dst, shift);
158  plhs[0] = MxArray(dst);
159  }
160  else if (method == "computeBitmaps") {
161  nargchk(nrhs==3 && nlhs<=2);
162  Mat img(rhs[2].toMat(CV_8U)), tb, eb;
163  obj->computeBitmaps(img, tb, eb);
164  plhs[0] = MxArray(tb);
165  if (nlhs>1)
166  plhs[1] = MxArray(eb);
167  }
168  else if (method == "get") {
169  nargchk(nrhs==3 && nlhs<=1);
170  string prop(rhs[2].toString());
171  if (prop == "MaxBits")
172  plhs[0] = MxArray(obj->getMaxBits());
173  else if (prop == "ExcludeRange")
174  plhs[0] = MxArray(obj->getExcludeRange());
175  else if (prop == "Cut")
176  plhs[0] = MxArray(obj->getCut());
177  else
178  mexErrMsgIdAndTxt("mexopencv:error",
179  "Unrecognized property %s", prop.c_str());
180  }
181  else if (method == "set") {
182  nargchk(nrhs==4 && nlhs==0);
183  string prop(rhs[2].toString());
184  if (prop == "MaxBits")
185  obj->setMaxBits(rhs[3].toInt());
186  else if (prop == "ExcludeRange")
187  obj->setExcludeRange(rhs[3].toInt());
188  else if (prop == "Cut")
189  obj->setCut(rhs[3].toBool());
190  else
191  mexErrMsgIdAndTxt("mexopencv:error",
192  "Unrecognized property %s", prop.c_str());
193  }
194  else
195  mexErrMsgIdAndTxt("mexopencv:error",
196  "Unrecognized operation %s", method.c_str());
197 }
virtual void setCut(bool value)=0
virtual void setExcludeRange(int exclude_range)=0
virtual Point calculateShift(InputArray img0, InputArray img1)=0
virtual int getMaxBits() const=0
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
T empty(T... args)
virtual bool getCut() const=0
T distance(T... args)
Ptr< AlignMTB > create_AlignMTB(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of AlignMTB using options in arguments.
Definition: AlignMTB_.cpp:25
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
map< int, Ptr< AlignMTB > > obj_
Object container.
Definition: AlignMTB_.cpp:18
#define CV_8U
STL namespace.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: AlignMTB_.cpp:58
T end(T... args)
virtual bool isOpened() const
Ptr< AlignMTB > createAlignMTB(int max_bits=6, int exclude_range=4, bool cut=true)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setMaxBits(int max_bits)=0
virtual void process(InputArrayOfArrays src, std::vector< Mat > &dst, InputArray times, InputArray response)=0
T push_back(T... args)
virtual void computeBitmaps(InputArray img, OutputArray tb, OutputArray eb)=0
virtual void clear()
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
FileNode getFirstTopLevelNode() const
T size(T... args)
STL class.
bool empty() const
virtual void shiftMat(InputArray src, OutputArray dst, const Point shift)=0
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
int last_id
Last object id to allocate.
Definition: AlignMTB_.cpp:16
virtual int getExcludeRange() const=0
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const
T reserve(T... args)