mexopencv  3.4.1
MEX interface for OpenCV library
imencode.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<=2);
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 ext(rhs[0].toString());
127  Mat img(rhs[1].toMat(rhs[1].isFloat() ? CV_32F :
128  (rhs[1].isUint16() ? CV_16U : CV_8U)));
129  if (flip && (img.channels() == 3 || img.channels() == 4)) {
130  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
131  cvtColor(img, img, (img.channels()==3 ?
133  }
134  vector<uchar> buf;
135  bool success = imencode(ext, img, buf, params);
136  if (nlhs > 1)
137  plhs[1] = MxArray(success);
138  else if (!success)
139  mexErrMsgIdAndTxt("mexopencv:error", "imencode failed");
140  plhs[0] = MxArray(Mat(buf), mxUINT8_CLASS, false);
141 }
COLOR_RGB2BGR
IMWRITE_PAM_TUPLETYPE
const ConstMap< string, int > PamFormatMap
PAM tuple types for option processing.
Definition: imencode.cpp:29
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.
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
bool imencode(const String &ext, InputArray img, std::vector< uchar > &buf, const std::vector< int > &params=std::vector< int >())
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
const ConstMap< string, int > PngStrategyMap
PNG encoding strategies for option processing.
Definition: imencode.cpp:15
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
const ConstMap< string, int > ExrTypeMap
EXR storage types for option processing.
Definition: imencode.cpp:23
T c_str(T... args)
IMWRITE_PNG_STRATEGY_RLE
IMWRITE_PAM_FORMAT_BLACKANDWHITE
Identifies a numeric mxArray whose data is stored as the type specified in the MATLAB Primitive Types...
Definition: matrix.h:300
void flip(InputArray src, OutputArray dst, int flipCode)
IMWRITE_PAM_FORMAT_NULL
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imencode.cpp:45
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const
int channels() const
IMWRITE_EXR_TYPE
IMWRITE_JPEG_CHROMA_QUALITY