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, //!< 
COLORMAP_BONE = 1, //!< 
COLORMAP_JET = 2, //!< 
COLORMAP_WINTER = 3, //!< 
COLORMAP_RAINBOW = 4, //!< 
COLORMAP_OCEAN = 5, //!< 
COLORMAP_SUMMER = 6, //!< 
COLORMAP_SPRING = 7, //!< 
COLORMAP_COOL = 8, //!< 
COLORMAP_HSV = 9, //!< 
COLORMAP_PINK = 10, //!< 
COLORMAP_HOT = 11, //!< 
COLORMAP_PARULA = 12 //!< 
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 代码运行结果