mexopencv  3.4.1
MEX interface for OpenCV library
imwrite.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 
13 namespace {
21 
24  //("Int", cv::IMWRITE_EXR_TYPE_UNIT)
26  ("Float", cv::IMWRITE_EXR_TYPE_FLOAT);
27 
33  ("GrayscaleAlpha", cv::IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA)
36 }
37 
45 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
46 {
47  // Check the number of arguments
48  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
49 
50  // Argument vector
51  vector<MxArray> rhs(prhs, prhs+nrhs);
52 
53  // Option processing
54  vector<int> params;
55  bool flip = true;
56  for (int i=2; i<nrhs; i+=2) {
57  string key(rhs[i].toString());
58  if (key == "JpegQuality") {
60  params.push_back(rhs[i+1].toInt());
61  }
62  else if (key == "JpegProgressive") {
64  params.push_back(rhs[i+1].toBool() ? 1 : 0);
65  }
66  else if (key == "JpegOptimize") {
68  params.push_back(rhs[i+1].toBool() ? 1 : 0);
69  }
70  else if (key == "JpegResetInterval") {
72  params.push_back(rhs[i+1].toInt());
73  }
74  else if (key == "JpegLumaQuality") {
76  params.push_back(rhs[i+1].toInt());
77  }
78  else if (key == "JpegChromaQuality") {
80  params.push_back(rhs[i+1].toInt());
81  }
82  else if (key == "PngCompression") {
84  params.push_back(rhs[i+1].toInt());
85  }
86  else if (key == "PngStrategy") {
88  params.push_back(PngStrategyMap[rhs[i+1].toString()]);
89  }
90  else if (key == "PngBilevel") {
92  params.push_back(rhs[i+1].toBool() ? 1 : 0);
93  }
94  else if (key == "PxmBinary") {
96  params.push_back(rhs[i+1].toBool() ? 1 : 0);
97  }
98  else if (key == "ExrType") {
100  params.push_back(ExrTypeMap[rhs[i+1].toString()]);
101  }
102  else if (key == "WebpQuality") {
104  params.push_back(rhs[i+1].toInt());
105  }
106  else if (key == "PamTupleType") {
108  params.push_back(PamFormatMap[rhs[i+1].toString()]);
109  }
110  else if (key == "Params") {
111  // append to parameters by directly passing a vector of integers
112  vector<int> pvec(rhs[i+1].toVector<int>());
113  if ((pvec.size()%2) != 0)
114  mexErrMsgIdAndTxt("mexopencv:error",
115  "Params vectors must contain pairs of id/value.");
116  params.insert(params.end(), pvec.begin(), pvec.end());
117  }
118  else if (key == "FlipChannels")
119  flip = rhs[i+1].toBool();
120  else
121  mexErrMsgIdAndTxt("mexopencv:error",
122  "Unrecognized option %s", key.c_str());
123  }
124 
125  // Process
126  string filename(rhs[0].toString());
127  bool success;
128  if (rhs[1].isNumeric()) {
129  Mat img(rhs[1].toMat(rhs[1].isFloat() ? CV_32F :
130  (rhs[1].isUint16() ? CV_16U : CV_8U)));
131  if (flip && (img.channels() == 3 || img.channels() == 4)) {
132  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
133  cvtColor(img, img, (img.channels()==3 ?
135  }
136  success = imwrite(filename, img, params);
137  }
138  else if (rhs[1].isCell()) {
139  //vector<Mat> img_vec(rhs[1].toVector<Mat>());
140  vector<MxArray> vec(rhs[1].toVector<MxArray>());
141  vector<Mat> img_vec;
142  img_vec.reserve(vec.size());
143  for (vector<MxArray>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
144  Mat img(it->toMat(it->isFloat() ? CV_32F :
145  (it->isUint16() ? CV_16U : CV_8U)));
146  if (flip && (img.channels() == 3 || img.channels() == 4)) {
147  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
148  cvtColor(img, img, (img.channels()==3 ?
150  }
151  img_vec.push_back(img);
152  }
153  success = imwrite(filename, img_vec, params);
154  }
155  else
156  mexErrMsgIdAndTxt("mexopencv:error", "Invalid image argument");
157  if (nlhs > 0)
158  plhs[0] = MxArray(success);
159  else if (!success)
160  mexErrMsgIdAndTxt("mexopencv:error", "imwrite failed");
161 }
const ConstMap< string, int > PngStrategyMap
PNG encoding strategies for option processing.
Definition: imwrite.cpp:15
bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
COLOR_RGB2BGR
IMWRITE_PAM_TUPLETYPE
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
IMWRITE_JPEG_QUALITY
IMWRITE_PNG_STRATEGY_FILTERED
COLOR_RGBA2BGRA
#define CV_8U
IMWRITE_JPEG_OPTIMIZE
IMWRITE_PNG_COMPRESSION
STL namespace.
const ConstMap< string, int > PamFormatMap
PAM tuple types for option processing.
Definition: imwrite.cpp:29
IMWRITE_PNG_STRATEGY_FIXED
IMWRITE_WEBP_QUALITY
T end(T... args)
IMWRITE_PAM_FORMAT_RGB_ALPHA
IMWRITE_PNG_BILEVEL
IMWRITE_EXR_TYPE_FLOAT
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T push_back(T... args)
IMWRITE_EXR_TYPE_HALF
#define CV_32F
IMWRITE_JPEG_RST_INTERVAL
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...
IMWRITE_PAM_FORMAT_RGB
IMWRITE_PNG_STRATEGY
IMWRITE_PXM_BINARY
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
IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY
#define CV_16U
IMWRITE_JPEG_LUMA_QUALITY
IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA
T insert(T... args)
T size(T... args)
IMWRITE_PAM_FORMAT_GRAYSCALE
STL class.
IMWRITE_PNG_STRATEGY_DEFAULT
Global constant definitions.
T begin(T... args)
IMWRITE_JPEG_PROGRESSIVE
T c_str(T... args)
IMWRITE_PNG_STRATEGY_RLE
IMWRITE_PAM_FORMAT_BLACKANDWHITE
void flip(InputArray src, OutputArray dst, int flipCode)
IMWRITE_PAM_FORMAT_NULL
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
const ConstMap< string, int > ExrTypeMap
EXR storage types for option processing.
Definition: imwrite.cpp:23
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imwrite.cpp:45
cv::Mat toMat() const
int channels() const
IMWRITE_EXR_TYPE
T reserve(T... args)
IMWRITE_JPEG_CHROMA_QUALITY