//查找表LUT,建立一种颜色之间的映射关系。
#include "stdafx.h"
#include <opencv2/opencv.hpp>
using namespace cv;
//这里使用系统给定的函数applyColorMap函数进行处理并显示
void ColorMapUsingSystemFunction(Mat img,Mat& out)
{
//系统提供了不同的颜色模板,可以通过查看函数的定义获得。
cv::applyColorMap(img, out, COLORMAP_OCEAN);
}
void ColorMapUsingDefineFunction(Mat img, Mat &out)
{
out = Mat::zeros(img.size(), img.type());
int lut[256];
for (int k = 0; k < 256;k++)
{
if (k<127)
{
lut[k] = 0;
}
else
{
lut[k] = 255;
}
}
//这里建立了一种映射关系,在三通道的图像中,每种颜色两种选择,会有8种颜色,
//即(0,0,0)、(255,0,0)、(0,255,0)、(0,0,255)、(0,255,255)、(255,255,0)、(255,0,255)、(255,255,255)
//在单通道图像中,只有两种颜色,即黑白。
int rows = img.rows;
int cols = img.cols;
int channels = img.channels();
for (int i = 0; i < rows;i++)
{
for (int j = 0; j < cols;j++)
{
if (channels==3)
{
Vec3b bgr = img.at<Vec3b>(i, j);
bgr[0] = lut[bgr[0]];
bgr[1] = lut[bgr[1]];
bgr[2] = lut[bgr[2]];
out.at<Vec3b>(i, j) = bgr;
}
else if (channels==1)
{
uchar ch = img.at<uchar>(i, j);
ch = lut[ch];
out.at<uchar>(i, j) = ch;
}
}
}
}
int main()
{
Mat src = imread("E:\\1.jpg");
if (src.empty())
{
printf("Open file faild!");
return 0;
}
imshow("src", src);
Mat out1;
ColorMapUsingSystemFunction(src, out1);
imshow("out1", out1);
Mat out2;
ColorMapUsingDefineFunction(src, out2);
imshow("out2", out2);
waitKey(0);
return 0;
}