mexopencv  3.4.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 {
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 = false;
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) {
58  if (src.channels() == 3)
59  cvtColor(src, src, cv::COLOR_RGB2BGR);
60  if (dst.channels() == 3)
61  cvtColor(dst, dst, cv::COLOR_RGB2BGR);
62  if (mask.channels() == 3)
63  cvtColor(mask, mask, cv::COLOR_RGB2BGR);
64  }
65  seamlessClone(src, dst, mask, p, blend, flags);
66  // OpenCV's default is BGR while MATLAB's is RGB
67  if (flip && blend.channels() == 3)
68  cvtColor(blend, blend, cv::COLOR_BGR2RGB);
69  plhs[0] = MxArray(blend);
70 }
const ConstMap< string, int > CloningMethodMap
Cloning method types for option processing.
COLOR_RGB2BGR
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
COLOR_BGR2RGB
#define CV_8U
NORMAL_CLONE
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
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...
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
STL class.
Global constant definitions.
MIXED_CLONE
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
MONOCHROME_TRANSFER
void flip(InputArray src, OutputArray dst, int flipCode)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const
int channels() const
void seamlessClone(InputArray src, InputArray dst, InputArray mask, Point p, OutputArray blend, int flags)