mexopencv  3.4.1
MEX interface for OpenCV library
edgePreservingFilter.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  ("Recursive", cv::RECURS_FILTER)
17  ("NormConv", cv::NORMCONV_FILTER);
18 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34 
35  // Option processing
36  int flags = cv::RECURS_FILTER;
37  float sigma_s = 60;
38  float sigma_r = 0.04f;
39  bool flip = false;
40  for (int i=1; i<nrhs; i+=2) {
41  string key(rhs[i].toString());
42  if (key == "Filter")
43  flags = EdgePreseveFilterMap[rhs[i+1].toString()];
44  else if (key == "SigmaS")
45  sigma_s = rhs[i+1].toFloat();
46  else if (key == "SigmaR")
47  sigma_r = rhs[i+1].toFloat();
48  else if (key == "FlipChannels")
49  flip = rhs[i+1].toBool();
50  else
51  mexErrMsgIdAndTxt("mexopencv:error",
52  "Unrecognized option %s", key.c_str());
53  }
54 
55  // Process
56  Mat src(rhs[0].toMat(CV_8U)), dst;
57  if (flip && src.channels() == 3) {
58  // MATLAB's default is RGB while OpenCV's is BGR
59  cvtColor(src, src, cv::COLOR_RGB2BGR);
60  }
61  edgePreservingFilter(src, dst, flags, sigma_s, sigma_r);
62  if (flip && dst.channels() == 3) {
63  // OpenCV's default is BGR while MATLAB's is RGB
64  cvtColor(dst, dst, cv::COLOR_BGR2RGB);
65  }
66  plhs[0] = MxArray(dst);
67 }
COLOR_RGB2BGR
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
COLOR_BGR2RGB
#define CV_8U
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void edgePreservingFilter(InputArray src, OutputArray dst, int flags=1, float sigma_s=60, float sigma_r=0.4f)
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.
NORMCONV_FILTER
Global constant definitions.
T c_str(T... args)
void flip(InputArray src, OutputArray dst, int flipCode)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
RECURS_FILTER
const ConstMap< string, int > EdgePreseveFilterMap
Edge preserving filters for option processing.
cv::Mat toMat() const
int channels() const