mexopencv  3.4.1
MEX interface for OpenCV library
imread.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  gdal = false,
34  norotate = false;
35  int scale_denom = 1;
36  int flags = 0;
37  bool override = false;
38  bool flip = true;
39  for (int i=1; i<nrhs; i+=2) {
40  string key(rhs[i].toString());
41  if (key == "Flags") {
42  flags = rhs[i+1].toInt();
43  override = true;
44  }
45  else if (key == "Unchanged")
46  unchanged = rhs[i+1].toBool();
47  else if (key == "AnyDepth")
48  anydepth = rhs[i+1].toBool();
49  else if (key == "AnyColor")
50  anycolor = rhs[i+1].toBool();
51  else if (key == "Grayscale") {
52  color = !rhs[i+1].toBool();
53  anycolor = false;
54  }
55  else if (key == "Color") {
56  color = rhs[i+1].toBool();
57  anycolor = false;
58  }
59  else if (key == "GDAL")
60  gdal = rhs[i+1].toBool();
61  else if (key == "ReduceScale") {
62  scale_denom = rhs[i+1].toInt();
63  CV_Assert(scale_denom==1 || scale_denom==2 ||
64  scale_denom==4 || scale_denom==8);
65  }
66  else if (key == "IgnoreOrientation")
67  norotate = rhs[i+1].toBool();
68  else if (key == "FlipChannels")
69  flip = rhs[i+1].toBool();
70  else
71  mexErrMsgIdAndTxt("mexopencv:error",
72  "Unrecognized option %s", key.c_str());
73  }
74 
75  // build flag value from options
76  if (!override) {
77  if (unchanged) {
78  // depth and cn as is (as determined by decoder).
79  // This is the only way to load alpha channel if present
80  flags = cv::IMREAD_UNCHANGED;
81  }
82  else if (gdal) {
83  // use GDAL as decoder
84  flags = cv::IMREAD_LOAD_GDAL;
85  }
86  else {
87  // depth as is, otherwise CV_8U
88  flags |= (anydepth ? cv::IMREAD_ANYDEPTH : 0);
89  // channels as is (if gray then cn=1, else cn=3 [BGR])
90  flags |= (anycolor ? cv::IMREAD_ANYCOLOR :
91  // otherwise explicitly either cn = 3 or cn = 1
93 
94  // image size reduction (this applies to both grayscale/color)
95  if (scale_denom > 1) {
96  if (scale_denom == 2)
98  else if (scale_denom == 4)
100  else if (scale_denom == 8)
102  }
103 
104  // EXIF orientation
105  flags |= (norotate ? cv::IMREAD_IGNORE_ORIENTATION : 0);
106  }
107  }
108 
109  // Process
110  string filename(rhs[0].toString());
111  Mat img = imread(filename, flags);
112  if (img.data == NULL)
113  mexErrMsgIdAndTxt("mexopencv:error", "imread failed");
114  if (flip && (img.channels() == 3 || img.channels() == 4)) {
115  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
116  cvtColor(img, img, (img.channels()==3 ?
118  }
119  plhs[0] = MxArray(img);
120 }
IMREAD_ANYCOLOR
#define CV_Assert(...)
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
COLOR_BGR2RGB
Mat imread(const String &filename, int flags=IMREAD_COLOR)
uchar * data
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
IMREAD_UNCHANGED
COLOR_BGRA2RGBA
IMREAD_LOAD_GDAL
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...
IMREAD_REDUCED_GRAYSCALE_4
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_REDUCED_GRAYSCALE_2
IMREAD_COLOR
void flip(InputArray src, OutputArray dst, int flipCode)
IMREAD_REDUCED_GRAYSCALE_8
int channels() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imread.cpp:20