mexopencv  3.4.1
MEX interface for OpenCV library
imreadmulti.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 = true,
32  color = false,
33  gdal = false,
34  norotate = false;
35  int flags = 0;
36  bool override = false;
37  bool flip = true;
38  for (int i=1; i<nrhs; i+=2) {
39  string key(rhs[i].toString());
40  if (key == "Flags") {
41  flags = rhs[i+1].toInt();
42  override = true;
43  }
44  else if (key == "Unchanged")
45  unchanged = rhs[i+1].toBool();
46  else if (key == "AnyDepth")
47  anydepth = rhs[i+1].toBool();
48  else if (key == "AnyColor")
49  anycolor = rhs[i+1].toBool();
50  else if (key == "Grayscale") {
51  color = !rhs[i+1].toBool();
52  anycolor = false;
53  }
54  else if (key == "Color") {
55  color = rhs[i+1].toBool();
56  anycolor = false;
57  }
58  else if (key == "GDAL")
59  gdal = rhs[i+1].toBool();
60  else if (key == "IgnoreOrientation")
61  norotate = rhs[i+1].toBool();
62  else if (key == "FlipChannels")
63  flip = rhs[i+1].toBool();
64  else
65  mexErrMsgIdAndTxt("mexopencv:error",
66  "Unrecognized option %s", key.c_str());
67  }
68 
69  // build flag value from options
70  if (!override) {
71  if (unchanged) {
72  // depth and cn as is (as determined by decoder).
73  // This is the only way to load alpha channel if present
74  flags = cv::IMREAD_UNCHANGED;
75  }
76  else if (gdal) {
77  // use GDAL as decoder
78  flags = cv::IMREAD_LOAD_GDAL;
79  }
80  else {
81  // depth as is, otherwise CV_8U
82  flags |= (anydepth ? cv::IMREAD_ANYDEPTH : 0);
83  // channels as is (if gray then cn=1, else cn=3 [BGR])
84  flags |= (anycolor ? cv::IMREAD_ANYCOLOR :
85  // otherwise explicitly either cn = 3 or cn = 1
87 
88  // EXIF orientation
89  flags |= (norotate ? cv::IMREAD_IGNORE_ORIENTATION : 0);
90  }
91  }
92 
93  // Process
94  string filename(rhs[0].toString());
95  vector<Mat> imgs;
96  bool status = imreadmulti(filename, imgs, flags);
97  if (!status)
98  mexErrMsgIdAndTxt("mexopencv:error", "imreadmulti failed");
99  for (vector<Mat>::iterator it = imgs.begin(); it != imgs.end(); ++it) {
100  if (it->data == NULL)
101  mexErrMsgIdAndTxt("mexopencv:error", "imreadmulti failed");
102  if (flip && (it->channels() == 3 || it->channels() == 4)) {
103  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
104  cvtColor(*it, *it, (it->channels()==3 ?
106  }
107  }
108  plhs[0] = MxArray(imgs);
109 }
IMREAD_ANYCOLOR
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
COLOR_BGR2RGB
bool imreadmulti(const String &filename, std::vector< Mat > &mats, int flags=IMREAD_ANYCOLOR)
STL namespace.
T end(T... args)
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: imreadmulti.cpp:20
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...
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 begin(T... args)
T c_str(T... args)
IMREAD_COLOR
void flip(InputArray src, OutputArray dst, int flipCode)