将脑图像转换成伪彩色

基于opencv

#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("F://Visual Studio 2015//Gq_1//Gq_1//03-3.png", 0);

	Mat scaledGray = scaleGray(inputGray);

	Mat pseudocolor = gray2pseudocolor(scaledGray);

	Mat rainbow = gray2rainbow(scaledGray);

	//imwrite("001.png", scaledGray);
	//imwrite("002.png", pseudocolor);
	imwrite("03-4.png", rainbow);



	return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值