#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat scaleGray(const Mat& inputGray)
{
Mat outputGray(inputGray.size(), CV_8U);
unsigned char grayValue, maxValue = 1;
for (int y = 0; y < inputGray.rows; y++)
{
for (int x = 0; x < inputGray.cols; x++)
{
grayValue = inputGray.at<uchar>(y, x);
maxValue = max(maxValue, grayValue);
}
}
float scale = 255.0 / maxValue;
for (int y = 0; y < inputGray.rows; y++)
{
for (int x = 0; x < inputGray.cols; x++)
{
outputGray.at<uchar>(y, x) = static_cast<unsigned char>(inputGray.at<uchar>(y, x)*scale + 0.5);
}
}
return outputGray;
}
Mat gray2pseudocolor(const Mat& scaledGray)
{
Mat outputPseudocolor(scaledGray.size(), CV_8UC3);
unsigned char grayValue;
for (int y = 0; y < scaledGray.rows; y++)
{
for (int x = 0; x < scaledGray.cols; x++)
{
grayValue = scaledGray.at<uchar>(y, x);
Vec3b & pixel = outputPseudocolor.at<Vec3b>(y, x);
pixel[0] = abs(255 - grayValue);
pixel[1] = abs(127 - grayValue);
pixel[2] = abs(0 - grayValue);
}
}
return outputPseudocolor;
}
/*
* color R G B gray
* red 255 0 0 255
* orange 255 127 0 204
* yellow 255 255 0 153
* green 0 255 0 102
* cyan 0 255 255 51
* blue 0 0 255 0
*/
Mat gray2rainbow(const Mat & scaledGray)
{
Mat outputRainbow(scaledGray.size(), CV_8UC3);
unsigned char grayValue;
for (int y = 0; y < scaledGray.rows; y++)
{
for (int x = 0; x < scaledGray.cols; x++)
{
grayValue = scaledGray.at<uchar>(y, x);
Vec3b & pixel = outputRainbow.at<Vec3b>(y, x);
if (grayValue <= 51)
{
pixel[0] = 255;
pixel[1] = grayValue * 5;
pixel[2] = 0;
}
else if (grayValue <= 102)
{
grayValue -= 51;
pixel[0] = 255 - grayValue * 5;
pixel[1] = 255;
pixel[2] = 0;
}
else if (grayValue <= 153)
{
grayValue -= 102;
pixel[0] = 0;
pixel[1] = 255;
pixel[2] = grayValue * 5;
}
else if (grayValue <= 204)
{
grayValue -= 153;
pixel[0] = 0;
pixel[1] = 255 - static_cast<unsigned char>(grayValue*128.0 / 51 + 0.5);
pixel[2] = 255;
}
else if (grayValue <= 255)
{
grayValue -= 204;
pixel[0] = 0;
pixel[1] = 127 - static_cast<unsigned char>(grayValue*127.0 / 51 + 0.5);
pixel[2] = 255;
}
}
}
return outputRainbow;
}
int main()
{
//输入灰度图像
Mat inputGray = imread("L:\\StudyLesson\\000000_10_D1Result.png", 0);
/// Mat inputGray = imread("L:\\data_scene_flow\\training\\results\\000185_11\\WeightedHoughResults\\depth_left2.png", 0);
/// Mat scaledGray = scaleGray(inputGray);
Mat pseudocolor = gray2pseudocolor(scaledGray);
/// Mat rainbow = gray2rainbow(scaledGray);
//输出伪彩色图像
imwrite("L:\\data_scene_flow\\training\\results\\000026_10\\WeightedHoughResults\\groundtruth_pseudocolor.png", pseudocolor);
return 0;
}
Visual Studio + OpenCV实现灰度图像转变成伪彩色图像
猜你喜欢
转载自blog.csdn.net/zdluffy/article/details/84962867
今日推荐
周排行