mexopencv  0.1
mex interface for opencv library
imreadmulti.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs + nrhs);
26 
27  // Option processing
28  bool unchanged = false,
29  anydepth = false,
30  anycolor = true,
31  color = false,
32  gdal = false;
33  int flags = 0;
34  bool override = false;
35  bool flip = true;
36  for (int i=1; i<nrhs; i+=2) {
37  string key(rhs[i].toString());
38  if (key == "Flags") {
39  flags = rhs[i+1].toInt();
40  override = true;
41  }
42  else if (key == "Unchanged")
43  unchanged = rhs[i+1].toBool();
44  else if (key == "AnyDepth")
45  anydepth = rhs[i+1].toBool();
46  else if (key == "AnyColor")
47  anycolor = rhs[i+1].toBool();
48  else if (key == "Grayscale") {
49  color = !rhs[i+1].toBool();
50  anycolor = false;
51  }
52  else if (key == "Color") {
53  color = rhs[i+1].toBool();
54  anycolor = false;
55  }
56  else if (key == "GDAL")
57  gdal = rhs[i+1].toBool();
58  else if (key == "FlipChannels")
59  flip = rhs[i+1].toBool();
60  else
61  mexErrMsgIdAndTxt("mexopencv:error",
62  "Unrecognized option %s", key.c_str());
63  }
64 
65  // build flag value from options
66  if (!override) {
67  if (unchanged) {
68  // depth and cn as is (as determined by decoder).
69  // This is the only way to load alpha channel if present
70  flags = cv::IMREAD_UNCHANGED;
71  }
72  else if (gdal) {
73  // use GDAL as decoder
74  flags = cv::IMREAD_LOAD_GDAL;
75  }
76  else {
77  // depth as is, otherwise CV_8U
78  flags |= (anydepth ? cv::IMREAD_ANYDEPTH : 0);
79  // channels as is (if gray then cn=1, else cn=3 [BGR])
80  flags |= (anycolor ? cv::IMREAD_ANYCOLOR :
81  // otherwise explicitly either cn = 3 or cn = 1
82  (color ? cv::IMREAD_COLOR : cv::IMREAD_GRAYSCALE));
83  }
84  }
85 
86  // Process
87  string filename(rhs[0].toString());
88  vector<Mat> imgs;
89  bool status = imreadmulti(filename, imgs, flags);
90  if (!status)
91  mexErrMsgIdAndTxt("mexopencv:error", "imreadmulti failed");
92  for (vector<Mat>::iterator it = imgs.begin(); it != imgs.end(); ++it) {
93  if (it->data == NULL)
94  mexErrMsgIdAndTxt("mexopencv:error", "imreadmulti failed");
95  if (flip && (it->channels() == 3 || it->channels() == 4)) {
96  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
97  cvtColor(*it, *it, (it->channels()==3 ?
98  cv::COLOR_BGR2RGB : cv::COLOR_BGRA2RGBA));
99  }
100  }
101  plhs[0] = MxArray(imgs);
102 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imreadmulti.cpp:19
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.