mexopencv  3.4.1
MEX interface for OpenCV library
Blender_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/stitching.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 using namespace cv::detail;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=2 && nlhs<=2);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38  int id = rhs[0].toInt();
39  string method(rhs[1].toString());
40 
41  // Constructor is called. Create a new object from argument
42  if (method == "new") {
43  nargchk(nrhs>=3 && nlhs<=1);
45  rhs[2].toString(), rhs.begin() + 3, rhs.end());
46  plhs[0] = MxArray(last_id);
47  mexLock();
48  return;
49  }
50  // static methods
51  else if (method == "createLaplacePyr") {
52  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
53  bool use_gpu = false;
54  for (int i=4; i<nrhs; i+=2) {
55  string key(rhs[i].toString());
56  if (key == "UseGPU")
57  use_gpu = rhs[i+1].toBool();
58  else
59  mexErrMsgIdAndTxt("mexopencv:error",
60  "Unrecognized option %s", key.c_str());
61  }
62  Mat img(rhs[2].toMat());
63  int num_levels = rhs[3].toInt();
64  vector<UMat> pyr;
65  if (use_gpu)
66  createLaplacePyrGpu(img, num_levels, pyr);
67  else
68  createLaplacePyr(img, num_levels, pyr);
69  vector<Mat> pyr_;
70  pyr_.reserve(pyr.size());
71  for (vector<UMat>::const_iterator it = pyr.begin(); it != pyr.end(); ++it)
72  pyr_.push_back(it->getMat(ACCESS_READ));
73  plhs[0] = MxArray(pyr_);
74  return;
75  }
76  else if (method == "restoreImageFromLaplacePyr") {
77  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
78  bool use_gpu = false;
79  for (int i=3; i<nrhs; i+=2) {
80  string key(rhs[i].toString());
81  if (key == "UseGPU")
82  use_gpu = rhs[i+1].toBool();
83  else
84  mexErrMsgIdAndTxt("mexopencv:error",
85  "Unrecognized option %s", key.c_str());
86  }
87  vector<UMat> pyr;
88  {
89  vector<MxArray> arr(rhs[2].toVector<MxArray>());
90  pyr.reserve(arr.size());
91  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
92  pyr.push_back(it->toMat(CV_16S).getUMat(ACCESS_RW));
93  }
94  if (use_gpu)
96  else
98  plhs[0] = MxArray(!pyr.empty() ? pyr[0].getMat(ACCESS_READ) : Mat());
99  return;
100  }
101  else if (method == "overlapRoi") {
102  nargchk(nrhs==6 && nlhs<=2);
103  Point tl1(rhs[2].toPoint()),
104  tl2(rhs[3].toPoint());
105  Size sz1(rhs[4].toSize()),
106  sz2(rhs[5].toSize());
107  Rect roi;
108  bool success = overlapRoi(tl1, tl2, sz1, sz2, roi);
109  if (nlhs > 1)
110  plhs[1] = MxArray(success);
111  else if (!success)
112  mexErrMsgIdAndTxt("mexopencv:error", "Operation failed");
113  plhs[0] = MxArray(roi);
114  return;
115  }
116  else if (method == "resultRoi") {
117  nargchk(nrhs==4 && nlhs<=1);
118  vector<Point> corners(rhs[2].toVector<Point>());
119  vector<Size> sizes(rhs[3].toVector<Size>());
120  Rect roi = resultRoi(corners, sizes);
121  plhs[0] = MxArray(roi);
122  return;
123  }
124  else if (method == "resultRoiIntersection") {
125  nargchk(nrhs==4 && nlhs<=1);
126  vector<Point> corners(rhs[2].toVector<Point>());
127  vector<Size> sizes(rhs[3].toVector<Size>());
128  Rect roi = resultRoiIntersection(corners, sizes);
129  plhs[0] = MxArray(roi);
130  return;
131  }
132  else if (method == "resultTl") {
133  nargchk(nrhs==3 && nlhs<=1);
134  vector<Point> corners(rhs[2].toVector<Point>());
135  Point tl = resultTl(corners);
136  plhs[0] = MxArray(tl);
137  return;
138  }
139 
140  // Big operation switch
141  Ptr<Blender> obj = obj_[id];
142  if (obj.empty())
143  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
144  if (method == "delete") {
145  nargchk(nrhs==2 && nlhs==0);
146  obj_.erase(id);
147  mexUnlock();
148  }
149  else if (method == "typeid") {
150  nargchk(nrhs==2 && nlhs<=1);
151  plhs[0] = MxArray(string(typeid(*obj).name()));
152  }
153  else if (method == "prepare") {
154  nargchk((nrhs==3 || nrhs==4) && nlhs==0);
155  if (nrhs == 4) {
156  vector<Point> corners(rhs[2].toVector<Point>());
157  vector<Size> sizes(rhs[3].toVector<Size>());
158  obj->prepare(corners, sizes);
159  }
160  else {
161  Rect dst_roi(rhs[2].toRect());
162  obj->prepare(dst_roi);
163  }
164  }
165  else if (method == "feed") {
166  nargchk(nrhs==5 && nlhs==0);
167  Mat img(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_16S)),
168  mask(rhs[3].toMat(CV_8U));
169  Point tl(rhs[4].toPoint());
170  obj->feed(img, mask, tl);
171  }
172  else if (method == "blend") {
173  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=2);
174  Mat dst, dst_mask;
175  for (int i=2; i<nrhs; i+=2) {
176  string key(rhs[i].toString());
177  if (key == "Dst")
178  dst = rhs[i+1].toMat(rhs[i+1].isUint8() ? CV_8U : CV_16S);
179  else if (key == "Mask")
180  dst_mask = rhs[i+1].toMat(CV_8U);
181  else
182  mexErrMsgIdAndTxt("mexopencv:error",
183  "Unrecognized option %s", key.c_str());
184  }
185  obj->blend(dst, dst_mask);
186  plhs[0] = MxArray(dst);
187  if (nlhs > 1)
188  plhs[1] = MxArray(dst_mask);
189  }
190  else if (method == "createWeightMaps") {
192  if (p.empty())
193  mexErrMsgIdAndTxt("mexopencv:error",
194  "Only supported for FeatherBlender");
195  vector<Point> corners(rhs[3].toVector<Point>());
196  vector<UMat> masks, weight_maps;
197  {
198  vector<MxArray> arr(rhs[2].toVector<MxArray>());
199  masks.reserve(arr.size());
200  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
201  masks.push_back(it->toMat(CV_8U).getUMat(ACCESS_READ));
202  }
203  Rect dst_roi = p->createWeightMaps(masks, corners, weight_maps);
204  vector<Mat> weight_maps_;
205  weight_maps_.reserve(weight_maps.size());
206  for (vector<UMat>::const_iterator it = weight_maps.begin(); it != weight_maps.end(); ++it)
207  weight_maps_.push_back(it->getMat(ACCESS_READ));
208  plhs[0] = MxArray(weight_maps_);
209  if (nlhs > 1)
210  plhs[1] = MxArray(dst_roi);
211  }
212  else
213  mexErrMsgIdAndTxt("mexopencv:error",
214  "Unrecognized operation %s", method.c_str());
215 }
void createLaplacePyrGpu(InputArray img, int num_levels, std::vector< UMat > &pyr)
T empty(T... args)
Common definitions for the stitching module.
void createLaplacePyr(InputArray img, int num_levels, std::vector< UMat > &pyr)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
Rect createWeightMaps(const std::vector< UMat > &masks, const std::vector< Point > &corners, std::vector< UMat > &weight_maps)
#define CV_8U
STL namespace.
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Blender_.cpp:31
cv::Ptr< cv::detail::Blender > createBlender(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of Blender using options in arguments.
ACCESS_RW
T push_back(T... args)
virtual void feed(InputArray img, InputArray mask, Point tl)
Ptr< Y > dynamicCast() const
map< int, Ptr< Blender > > obj_
Object container.
Definition: Blender_.cpp:21
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
void prepare(const std::vector< Point > &corners, const std::vector< Size > &sizes)
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
#define CV_16S
virtual void blend(InputOutputArray dst, InputOutputArray dst_mask)
T begin(T... args)
ACCESS_READ
T c_str(T... args)
bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
int last_id
Last object id to allocate.
Definition: Blender_.cpp:19
Rect resultRoiIntersection(const std::vector< Point > &corners, const std::vector< Size > &sizes)
void restoreImageFromLaplacePyrGpu(std::vector< UMat > &pyr)
void restoreImageFromLaplacePyr(std::vector< UMat > &pyr)
Point resultTl(const std::vector< Point > &corners)
Rect resultRoi(const std::vector< Point > &corners, const std::vector< UMat > &images)
cv::Mat toMat() const
T reserve(T... args)