mexopencv  3.4.1
MEX interface for OpenCV library
applyColorMap.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
16  ("Autumn", cv::COLORMAP_AUTUMN)
17  ("Bone", cv::COLORMAP_BONE)
18  ("Jet", cv::COLORMAP_JET)
19  ("Winter", cv::COLORMAP_WINTER)
20  ("Rainbow", cv::COLORMAP_RAINBOW)
21  ("Ocean", cv::COLORMAP_OCEAN)
22  ("Summer", cv::COLORMAP_SUMMER)
23  ("Spring", cv::COLORMAP_SPRING)
24  ("Cool", cv::COLORMAP_COOL)
25  ("HSV", cv::COLORMAP_HSV)
26  ("Pink", cv::COLORMAP_PINK)
27  ("Hot", cv::COLORMAP_HOT)
28  ("Parula", cv::COLORMAP_PARULA);
29 }
30 
38 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
39 {
40  // Check the number of arguments
41  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42 
43  // Argument vector
44  vector<MxArray> rhs(prhs, prhs+nrhs);
45 
46  // Option processing
47  bool flip = true;
48  for (int i=2; i<nrhs; i+=2) {
49  string key(rhs[i].toString());
50  if (key == "FlipChannels")
51  flip = rhs[i+1].toBool();
52  else
53  mexErrMsgIdAndTxt("mexopencv:error",
54  "Unrecognized option %s", key.c_str());
55  }
56 
57  // Process
58  Mat src(rhs[0].toMat(CV_8U)), dst;
59  if (rhs[1].isChar()) {
60  int colormap = ColormapTypesMap[rhs[1].toString()];
61  applyColorMap(src, dst, colormap);
62  }
63  else {
64  Mat userColor(rhs[1].toMat(CV_8U));
65  if (flip && userColor.channels() == 3 && src.channels() == 3)
66  cvtColor(userColor, userColor, cv::COLOR_BGR2RGB);
67  applyColorMap(src, dst, userColor);
68  }
69  // OpenCV's default is BGR while MATLAB's is RGB
70  if (flip && dst.channels() == 3)
71  cvtColor(dst, dst, cv::COLOR_BGR2RGB); // flip 3rd dim
72  plhs[0] = MxArray(dst);
73 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
COLORMAP_OCEAN
COLORMAP_SUMMER
COLORMAP_AUTUMN
COLORMAP_BONE
COLOR_BGR2RGB
#define CV_8U
COLORMAP_RAINBOW
STL namespace.
COLORMAP_JET
COLORMAP_PARULA
void colormap(const Mat &quantized, Mat &dst)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
COLORMAP_HOT
COLORMAP_HSV
const ConstMap< string, int > ColormapTypesMap
colormap types for option processing
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...
COLORMAP_PINK
COLORMAP_SPRING
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
COLORMAP_WINTER
COLORMAP_COOL
STL class.
Global constant definitions.
void applyColorMap(InputArray src, OutputArray dst, int colormap)
T c_str(T... args)
void flip(InputArray src, OutputArray dst, int flipCode)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const
int channels() const