mexopencv  0.1
mex interface for opencv library
seamlessClone.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 namespace {
15 const ConstMap<string,int> CloningMethodMap = ConstMap<string,int>
16  ("NormalClone", cv::NORMAL_CLONE)
17  ("MixedClone", cv::MIXED_CLONE)
18  ("MonochromeTransfer", cv::MONOCHROME_TRANSFER);
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35 
36  // Option processing
37  int flags = cv::NORMAL_CLONE;
38  bool flip = true;
39  for (int i=4; i<nrhs; i+=2) {
40  string key(rhs[i].toString());
41  if (key == "Method")
42  flags = CloningMethodMap[rhs[i+1].toString()];
43  else if (key == "FlipChannels")
44  flip = rhs[i+1].toBool();
45  else
46  mexErrMsgIdAndTxt("mexopencv:error",
47  "Unrecognized option %s", key.c_str());
48  }
49 
50  // Process
51  Mat src(rhs[0].toMat(CV_8U)),
52  dst(rhs[1].toMat(CV_8U)),
53  mask(rhs[2].toMat(CV_8U)),
54  blend;
55  Point p(rhs[3].toPoint());
56  // MATLAB's default is RGB while OpenCV's is BGR
57  if (flip && src.channels() == 3)
58  cvtColor(src, src, cv::COLOR_RGB2BGR);
59  if (flip && dst.channels() == 3)
60  cvtColor(dst, dst, cv::COLOR_RGB2BGR);
61  if (flip && mask.channels() == 3)
62  cvtColor(mask, mask, cv::COLOR_RGB2BGR);
63  seamlessClone(src, dst, mask, p, blend, flags);
64  // OpenCV's default is BGR while MATLAB's is RGB
65  if (flip && blend.channels() == 3)
66  cvtColor(blend, blend, cv::COLOR_BGR2RGB);
67  plhs[0] = MxArray(blend);
68 }
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
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927