1 所用到的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 测试代码
#include "opencv2opencv.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... "); 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]; //这个for循环中可以进行其他的操作 使相应的像素值 改变为 指定的像素值 //这里相当于对单通道图像的像素进行二值化处理 for (int i = 0; i < 256; i++) { if (i < 127) { lut[i] = 0; } 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 代码运行结果