mexopencv  3.4.1
MEX interface for OpenCV library
imdecode.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgcodecs.hpp"
10 using namespace std;
11 using namespace cv;
12 
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
21 {
22  // Check the number of arguments
23  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Option processing
29  bool unchanged = false,
30  anydepth = false,
31  anycolor = false,
32  color = true,
33  norotate = false;
34  int flags = 0;
35  bool override = false;
36  bool flip = true;
37  for (int i=1; i<nrhs; i+=2) {
38  string key(rhs[i].toString());
39  if (key == "Flags") {
40  flags = rhs[i+1].toInt();
41  override = true;
42  }
43  else if (key == "Unchanged")
44  unchanged = rhs[i+1].toBool();
45  else if (key == "AnyDepth")
46  anydepth = rhs[i+1].toBool();
47  else if (key == "AnyColor")
48  anycolor = rhs[i+1].toBool();
49  else if (key == "Grayscale") {
50  color = !rhs[i+1].toBool();
51  anycolor = false;
52  }
53  else if (key == "Color") {
54  color = rhs[i+1].toBool();
55  anycolor = false;
56  }
57  else if (key == "IgnoreOrientation")
58  norotate = rhs[i+1].toBool();
59  else if (key == "FlipChannels")
60  flip = rhs[i+1].toBool();
61  else
62  mexErrMsgIdAndTxt("mexopencv:error",
63  "Unrecognized option %s", key.c_str());
64  }
65 
66  // build flag value from options
67  if (!override) {
68  if (unchanged) {
69  // depth and cn as is (as determined by decoder).
70  // This is the only way to load alpha channel if present
71  flags = cv::IMREAD_UNCHANGED;
72  }
73  else {
74  // depth as is, otherwise CV_8U
75  flags |= (anydepth ? cv::IMREAD_ANYDEPTH : 0);
76  // channels as is (if gray then cn=1, else cn=3 [BGR])
77  flags |= (anycolor ? cv::IMREAD_ANYCOLOR :
78  // otherwise explicitly either cn = 3 or cn = 1
80 
81  // EXIF orientation
82  flags |= (norotate ? cv::IMREAD_IGNORE_ORIENTATION : 0);
83  }
84  }
85 
86  // Process
87  Mat buf(rhs[0].toMat(CV_8U));
88  Mat img = imdecode(buf, flags);
89  if (img.data == NULL)
90  mexErrMsgIdAndTxt("mexopencv:error", "imdecode failed");
91  if (flip && (img.channels() == 3 || img.channels() == 4)) {
92  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
93  cvtColor(img, img, (img.channels()==3 ?
95  }
96  plhs[0] = MxArray(img);
97 }
IMREAD_ANYCOLOR
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
COLOR_BGR2RGB
#define CV_8U
uchar * data
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imdecode.cpp:20
IMREAD_UNCHANGED
COLOR_BGRA2RGBA
IMREAD_IGNORE_ORIENTATION
IMREAD_ANYDEPTH
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...
Mat imdecode(InputArray buf, int flags)
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
IMREAD_GRAYSCALE
STL class.
Global constant definitions.
T c_str(T... args)
IMREAD_COLOR
void flip(InputArray src, OutputArray dst, int flipCode)
cv::Mat toMat() const
int channels() const