A used OpenCv API:
/** @brief Applies a GNU Octave/MATLAB equivalent colormap on a given image.
@param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3.
@param dst The result is the colormapped source image. Note: Mat::create is called on dst.
@param colormap The colormap to apply, see #ColormapTypes
*/
CV_EXPORTS_W void applyColorMap(InputArray src, OutputArray dst, int colormap);
#ColormapTypes
COLORMAP_AUTUMN = 0, //!< ![autumn](pics/colormaps/colorscale_autumn.jpg)
COLORMAP_BONE = 1, //!< ![bone](pics/colormaps/colorscale_bone.jpg)
COLORMAP_JET = 2, //!< ![jet](pics/colormaps/colorscale_jet.jpg)
COLORMAP_WINTER = 3, //!< ![winter](pics/colormaps/colorscale_winter.jpg)
COLORMAP_RAINBOW = 4, //!< ![rainbow](pics/colormaps/colorscale_rainbow.jpg)
COLORMAP_OCEAN = 5, //!< ![ocean](pics/colormaps/colorscale_ocean.jpg)
COLORMAP_SUMMER = 6, //!< ![summer](pics/colormaps/colorscale_summer.jpg)
COLORMAP_SPRING = 7, //!< ![spring](pics/colormaps/colorscale_spring.jpg)
COLORMAP_COOL = 8, //!< ![cool](pics/colormaps/colorscale_cool.jpg)
COLORMAP_HSV = 9, //!< ![HSV](pics/colormaps/colorscale_hsv.jpg)
COLORMAP_PINK = 10, //!< ![pink](pics/colormaps/colorscale_pink.jpg)
COLORMAP_HOT = 11, //!< ![hot](pics/colormaps/colorscale_hot.jpg)
COLORMAP_PARULA = 12 //! <! [Parula] (pics / colormaps / colorscale_parula.jpg)
2 test code
#include "opencv2\opencv.hpp" #include <iostream> using namespace std; using namespace cv; void myColorMap(Mat &grayImg); int main(int argc, char**argv) { Mat src = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\Blender_Suzanne1.jpg"); if (src.empty()) { printf("Could not load image...\n"); return -1; } imshow("sourceImg", src); Mat grayLutDemo, dst; // 使用LUT applyColorMap(src, dst, COLORMAP_HSV); // 显示结果 imshow("ColorMap", dst); cvtColor(src, grayLutDemo, COLOR_BGR2GRAY); myColorMap(grayLutDemo); imshow("GrayLutDemo", grayLutDemo); waitKey(0); return 0; } void myColorMap(Mat &grayImg) { int lut[256 ]; // this loop may be performed for other operations of the corresponding pixel value to a pixel value specified // here corresponds to the single-channel image pixel binarization processing is performed for ( int I = 0 ; I < 256 ; I ++ ) { IF (I < 127 ) { LUT [I] = 0 ; } the else LUT [I] = 255 ; } int Hight = grayImg.rows; int width = grayImg.cols; for ( int Row = 0 ; row <hight; row ++) { for (int col = 0; col < width; col++) { uchar pv = grayImg.at<uchar>(row, col); grayImg.at<uchar>(row, col) = lut[pv]; } } }
3 code running results