灰度图转RGB(伪彩色图)

前言

灰度图可以转化成伪彩色图以显示出更多的细节,简单整理了三种转化的方法,结合openCV通过C++进行了实现。

代码

该函数将二维数组转化为伪彩色图并返回,可通过type参数选择不同的转换方法。

Mat Array2RGB(uchar **arr, int rows, int cols, int type)
{
          
   
	//array2gray
	Mat gray(rows, cols, CV_8UC1);
	for (int i = 0; i < rows; i++)
	{
          
   
		uchar *p = gray.ptr<uchar>(i);
		for (int j = 0; j < cols; j++)
		{
          
   
			*(p + j) = *(*(arr + i) + j);
		}
	}
	//gray2rgb
	/*将灰度图像转换为彩色图像,称为灰度图像的伪彩色处理。
	//伪彩色处理技术的实现方式有很多,如:灰度分割法、灰度级-彩色变换法、滤波法等等。
	*/
	Mat M(gray.rows, gray.cols, CV_8UC3);
	if (type == 0)//灰度级-彩色变换法
	{
          
   
		for (int i = 0; i < gray.rows; i++)
		{
          
   
			uchar *p = gray.ptr<uchar>(i);
			uchar temp;
			for (int j = 0; j < gray.cols; j++)
			{
          
   
				temp = *(p + j);
				if (0 <= temp && temp <= 63)
				{
          
   
					M.at<cv::Vec3b>(i, j)[0] = 255;//B
					M.at<cv::Vec3b>(i, j)[1] = 254 - 4 * temp;//G
					M.at<cv::Vec3b>(i, j)[2] = 0;//R
				}
				else if (64 <= temp && temp <= 127)
				{
          
   
					M.at<cv::Vec3b>(i, j)[0] = 510 - 4 * temp;//B
					M.at<cv::Vec3b>(i, j)[1] = 4 * temp - 254;//G
					M.at<cv::Vec3b>(i, j)[2] = 0;//R
				}
				else if (128 <= temp && temp <= 191)
				{
          
   
					M.at<cv::Vec3b>(i, j)[0] = 0;//B
					M.at<cv::Vec3b>(i, j)[1] = 255;//G
					M.at<cv::Vec3b>(i, j)[2] = 4 * temp - 510;//R
				}
				else if (192 <= temp && temp <= 255)
				{
          
   
					M.at<cv::Vec3b>(i, j)[0] = 0;//B
					M.at<cv::Vec3b>(i, j)[1] = 1022 - 4 * temp;//G
					M.at<cv::Vec3b>(i, j)[2] = 255;//R
				}
			}
		}
	}
	else if (type == 1)//gray2pseudocolor
	{
          
   
		for (int i = 0; i < gray.rows; i++)
		{
          
   
			uchar *p = gray.ptr<uchar>(i);
			uchar grayValue;
			for (int j = 0; j < gray.cols; j++)
			{
          
   
				grayValue = *(p + j);
				Vec3b& pixel = M.at<Vec3b>(i, j);
				pixel[0] = abs(255 - grayValue);
				pixel[1] = abs(127 - grayValue);
				pixel[2] = abs(0 - grayValue);
			}
		}
	}
	else//gray2rainbow
	{
          
   
		unsigned char grayValue;
		for (int i = 0; i < gray.rows; i++)
		{
          
   
			uchar *p = gray.ptr<uchar>(i);
			uchar grayValue;
			for (int j = 0; j < gray.cols; j++)
			{
          
   
				grayValue = *(p + j);
				Vec3b& pixel = M.at<Vec3b>(i, j);
				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 M;
}
//以上代码可以优化的两个点:
//1.返回值为Mat类型可太吓人了,最好用一个Mat类型的引用做函数形参,返回类型改为void
//2.用at会很慢,最好改为ptr

参考

如下为上述代码的方法参考,个人更喜欢参考1。

参考1: . 参考2: .

经验分享 程序员 微信小程序 职场和发展