mexopencv  0.1
mex interface for opencv library
imread.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 = false,
31  color = true,
32  gdal = false;
33  int scale_denom = 1;
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 == "GDAL")
58  gdal = rhs[i+1].toBool();
59  else if (key == "ReduceScale") {
60  scale_denom = rhs[i+1].toInt();
61  CV_Assert(scale_denom==1 || scale_denom==2 ||
62  scale_denom==4 || scale_denom==8);
63  }
64  else if (key == "FlipChannels")
65  flip = rhs[i+1].toBool();
66  else
67  mexErrMsgIdAndTxt("mexopencv:error",
68  "Unrecognized option %s", key.c_str());
69  }
70 
71  // build flag value from options
72  if (!override) {
73  if (unchanged) {
74  // depth and cn as is (as determined by decoder).
75  // This is the only way to load alpha channel if present
76  flags = cv::IMREAD_UNCHANGED;
77  }
78  else if (gdal) {
79  // use GDAL as decoder
80  flags = cv::IMREAD_LOAD_GDAL;
81  }
82  else {
83  // depth as is, otherwise CV_8U
84  flags |= (anydepth ? cv::IMREAD_ANYDEPTH : 0);
85  // channels as is (if gray then cn=1, else cn=3 [BGR])
86  flags |= (anycolor ? cv::IMREAD_ANYCOLOR :
87  // otherwise explicitly either cn = 3 or cn = 1
88  (color ? cv::IMREAD_COLOR : cv::IMREAD_GRAYSCALE));
89 
90  // image size reduction (this applies to both grayscale/color)
91  if (scale_denom > 1) {
92  if (scale_denom == 2)
93  flags |= cv::IMREAD_REDUCED_GRAYSCALE_2;
94  else if (scale_denom == 4)
95  flags |= cv::IMREAD_REDUCED_GRAYSCALE_4;
96  else if (scale_denom == 8)
97  flags |= cv::IMREAD_REDUCED_GRAYSCALE_8;
98  }
99  }
100  }
101 
102  // Process
103  string filename(rhs[0].toString());
104  Mat img = imread(filename, flags);
105  if (img.data == NULL)
106  mexErrMsgIdAndTxt("mexopencv:error", "imread failed");
107  if (flip && (img.channels() == 3 || img.channels() == 4)) {
108  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
109  cvtColor(img, img, (img.channels()==3 ?
110  cv::COLOR_BGR2RGB : cv::COLOR_BGRA2RGBA));
111  }
112  plhs[0] = MxArray(img);
113 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imread.cpp:19