网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++基于灰度图上色GrayToColorFromOther怎么实现

这篇文章主要介绍“C++基于灰度图上色GrayToColorFromOther怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++基于灰度图上色GrayToColorFromOther怎么实现”文章能帮助大家解决问题。

为源城等地区用户提供了全套网页设计制作服务,及源城网站建设行业解决方案。主营业务为网站建设、做网站、源城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

场景需求

       之前有提到给灰度图上色的需求,在此基础上,还有一种需求,就是基于另一张参考灰度图的色板来给当前的灰度图上色,比如参考灰度图的数值区间为-10到10,颜色从蓝到绿再到红,而当前的灰度图的数据区间为-1到1,若基于参考灰度图的色板确定数据对应的颜色,则当前灰度图的颜色应该在绿色左右波动。

       下方为具体实现函数和测试代码。

功能函数代码

/**
 * @brief GrayToColorFromOther             灰度图上色,基于参考灰度图的色板
 * @param phase1                           输入的灰色图像,通道为1,提供色板
 * @param phase2                           输入的灰色图像,通道为1,基于phase1的色板绘色
 * @return                                 上色后的图像
 */
cv::Mat GrayToColorFromOther(cv::Mat &phase1, cv::Mat &phase2)
{
	CV_Assert(phase1.channels() == 1);
	CV_Assert(phase2.channels() == 1);
	if (phase1.empty() || phase2.empty())
	{
		cv::Mat result = cv::Mat::zeros(100, 100, CV_8UC3);
		return result;
	}
	cv::Mat temp, result, mask;
	double max1, min1;
	int row = phase2.rows;
	int col = phase2.cols;
	// 确定参考灰度图的数据范围
	cv::minMaxIdx(phase1, &min1, &max1, nullptr, nullptr, phase1 == phase1);
	// 将当前灰度图以参考灰度图的数据范围作标准,进行数据变换
	temp = phase2.clone();
	for (int i = 0; i < row; ++i)
	{
		float *t2 = temp.ptr(i);
		for (int j = 0; j < col; ++j)
		{
			t2[j] = 255.0f*(phase2.at(i, j) - min1) / (max1 - min1);
		}
	}
	temp.convertTo(temp, CV_8UC1);
	// 创建掩膜,目的是为了隔离nan值的干扰
	mask = cv::Mat::zeros(phase2.size(), CV_8UC1);
	mask.setTo(255, phase2 == phase2);
 
	// 初始化三通道颜色图
	cv::Mat color1, color2, color3;
	color1 = cv::Mat::zeros(temp.size(), temp.type());
	color2 = cv::Mat::zeros(temp.size(), temp.type());
	color3 = cv::Mat::zeros(temp.size(), temp.type());
 
	// 基于灰度图的灰度层级,给其上色,最底的灰度值0为蓝色(255,0,0),最高的灰度值255为红色(0,0,255),中间的灰度值127为绿色(0,255,0)
	for (int i = 0; i < row; ++i)
	{
		uchar *c1 = color1.ptr(i);
		uchar *c2 = color2.ptr(i);
		uchar *c3 = color3.ptr(i);
		uchar *r = temp.ptr(i);
		uchar *m = mask.ptr(i);
		for (int j = 0; j < col; ++j)
		{
			if (m[j] == 255)
			{
				if (r[j] > (3 * 255 / 4) && r[j] <= 255)
				{
					c1[j] = 255;
					c2[j] = 4 * (255 - r[j]);
					c3[j] = 0;
				}
				else if (r[j] <= (3 * 255 / 4) && r[j] > (255 / 2))
				{
					c1[j] = 255 - 4 * (3 * 255 / 4 - r[j]);
					c2[j] = 255;
					c3[j] = 0;
				}
				else if (r[j] <= (255 / 2) && r[j] > (255 / 4))
				{
					c1[j] = 0;
					c2[j] = 255;
					c3[j] = 4 * (255 / 2 - r[j]);
				}
				else if (r[j] <= (255 / 4) && r[j] >= 0)
				{
					c1[j] = 0;
					c2[j] = 255 - 4 * (255 / 4 - r[j]);
					c3[j] = 255;
				}
				else {
					c1[j] = 0;
					c2[j] = 0;
					c3[j] = 0;
				}
			}
		}
	}
 
	// 三通道合并,得到颜色图
	vector images;
	images.push_back(color3);
	images.push_back(color2);
	images.push_back(color1);
	cv::merge(images, result);
 
	return result;
}

C++测试代码

#include
#include
#include
using namespace std;
using namespace cv;
 
void UnitPolar(int squaresize, cv::Mat& mag,cv::Mat& ang);
void UnitCart(int squaresize, cv::Mat& x, cv::Mat& y);
cv::Mat GrayToColor(cv::Mat &phase);
cv::Mat GrayToColorFromOther(cv::Mat &phase1, cv::Mat &phase2);
 
int main(void)
{
	cv::Mat mag, ang,result,result2;
	UnitPolar(2001, mag, ang);
	mag.at(10, 10) = nan("");
	cv::Mat mag2 = mag / 2;
 
	result = GrayToColor(mag);
	result2= GrayToColorFromOther(mag,mag2);
 
	system("pause");
	return 0;
}
 
void UnitPolar(int squaresize, cv::Mat& mag,cv::Mat& ang) {
	cv::Mat x;
	cv::Mat y;
	UnitCart(squaresize, x, y);                //产生指定范围内的指定数量点数,相邻数据跨度相同
	// OpenCV自带的转换有精度限制,导致结果有一定差异性
	//cv::cartToPolar(x, y, mag, ang, false); //坐标转换
 
	mag = cv::Mat(x.size(), x.type());
	ang = cv::Mat(x.size(), x.type());
	int row = mag.rows;
	int col = mag.cols;
	float *m, *a, *xx, *yy;
	for (int i = 0; i < row; ++i)
	{
		m = mag.ptr(i);
		a = ang.ptr(i);
		xx = x.ptr(i);
		yy = y.ptr(i);
		for (int j = 0; j < col; ++j)
		{
			m[j] = sqrt(xx[j] * xx[j] + yy[j] * yy[j]);
			a[j] = atan2(yy[j], xx[j]);
		}
	}
}
 
void UnitCart(int squaresize, cv::Mat& x, cv::Mat& y) {
	CV_Assert(squaresize % 2 == 1);
	x.create(squaresize, squaresize, CV_32FC1);
	y.create(squaresize, squaresize, CV_32FC1);
	//设置边界
	x.col(0).setTo(-1.0);
	x.col(squaresize - 1).setTo(1.0f);
	y.row(0).setTo(1.0);
	y.row(squaresize - 1).setTo(-1.0f);
 
	float delta = 2.0f / (squaresize - 1.0f);  //两个元素的间隔
 
	//计算其他位置的值
	for (int i = 1; i < squaresize - 1; ++i) {
		x.col(i) = -1.0f + i * delta;
		y.row(i) = 1.0f - i * delta;
	}
}
 
/**
 * @brief GrayToColor                      灰度图上色
 * @param phase                            输入的灰色图像,通道为1
 * @return                                 上色后的图像
 */
cv::Mat GrayToColor(cv::Mat &phase)
{
	CV_Assert(phase.channels() == 1);
 
	cv::Mat temp, result, mask;
	// 将灰度图重新归一化至0-255
	cv::normalize(phase, temp, 255, 0, cv::NORM_MINMAX);
	temp.convertTo(temp, CV_8UC1);
	// 创建掩膜,目的是为了隔离nan值的干扰
	mask = cv::Mat::zeros(phase.size(), CV_8UC1);
	mask.setTo(255, phase == phase);
 
	// 初始化三通道颜色图
	cv::Mat color1, color2, color3;
	color1 = cv::Mat::zeros(temp.size(), temp.type());
	color2 = cv::Mat::zeros(temp.size(), temp.type());
	color3 = cv::Mat::zeros(temp.size(), temp.type());
	int row = phase.rows;
	int col = phase.cols;
 
	// 基于灰度图的灰度层级,给其上色,最底的灰度值0为蓝色(255,0,0),最高的灰度值255为红色(0,0,255),中间的灰度值127为绿色(0,255,0)
	// 不要惊讶蓝色为什么是(255,0,0),因为OpenCV中是BGR而不是RGB
	for (int i = 0; i < row; ++i)
	{
		uchar *c1 = color1.ptr(i);
		uchar *c2 = color2.ptr(i);
		uchar *c3 = color3.ptr(i);
		uchar *r = temp.ptr(i);
		uchar *m = mask.ptr(i);
		for (int j = 0; j < col; ++j)
		{
			if (m[j] == 255)
			{
				if (r[j] > (3 * 255 / 4) && r[j] <= 255)
				{
					c1[j] = 255;
					c2[j] = 4 * (255 - r[j]);
					c3[j] = 0;
				}
				else if (r[j] <= (3 * 255 / 4) && r[j] > (255 / 2))
				{
					c1[j] = 255 - 4 * (3 * 255 / 4 - r[j]);
					c2[j] = 255;
					c3[j] = 0;
				}
				else if (r[j] <= (255 / 2) && r[j] > (255 / 4))
				{
					c1[j] = 0;
					c2[j] = 255;
					c3[j] = 4 * (255 / 2 - r[j]);
				}
				else if (r[j] <= (255 / 4) && r[j] >= 0)
				{
					c1[j] = 0;
					c2[j] = 255 - 4 * (255 / 4 - r[j]);
					c3[j] = 255;
				}
				else {
					c1[j] = 0;
					c2[j] = 0;
					c3[j] = 0;
				}
			}
		}
	}
 
	// 三通道合并,得到颜色图
	vector images;
	images.push_back(color3);
	images.push_back(color2);
	images.push_back(color1);
	cv::merge(images, result);
 
	return result;
}
 
/**
 * @brief GrayToColorFromOther             灰度图上色,基于参考灰度图的色板
 * @param phase1                           输入的灰色图像,通道为1,提供色板
 * @param phase2                           输入的灰色图像,通道为1,基于phase1的色板绘色
 * @return                                 上色后的图像
 */
cv::Mat GrayToColorFromOther(cv::Mat &phase1, cv::Mat &phase2)
{
	CV_Assert(phase1.channels() == 1);
	CV_Assert(phase2.channels() == 1);
	if (phase1.empty() || phase2.empty())
	{
		cv::Mat result = cv::Mat::zeros(100, 100, CV_8UC3);
		return result;
	}
	cv::Mat temp, result, mask;
	double max1, min1;
	int row = phase2.rows;
	int col = phase2.cols;
	// 确定参考灰度图的数据范围
	cv::minMaxIdx(phase1, &min1, &max1, nullptr, nullptr, phase1 == phase1);
	// 将当前灰度图以参考灰度图的数据范围作标准,进行数据变换
	temp = phase2.clone();
	for (int i = 0; i < row; ++i)
	{
		float *t2 = temp.ptr(i);
		for (int j = 0; j < col; ++j)
		{
			t2[j] = 255.0f*(phase2.at(i, j) - min1) / (max1 - min1);
		}
	}
	temp.convertTo(temp, CV_8UC1);
	// 创建掩膜,目的是为了隔离nan值的干扰
	mask = cv::Mat::zeros(phase2.size(), CV_8UC1);
	mask.setTo(255, phase2 == phase2);
 
	// 初始化三通道颜色图
	cv::Mat color1, color2, color3;
	color1 = cv::Mat::zeros(temp.size(), temp.type());
	color2 = cv::Mat::zeros(temp.size(), temp.type());
	color3 = cv::Mat::zeros(temp.size(), temp.type());
 
	// 基于灰度图的灰度层级,给其上色,最底的灰度值0为蓝色(255,0,0),最高的灰度值255为红色(0,0,255),中间的灰度值127为绿色(0,255,0)
	for (int i = 0; i < row; ++i)
	{
		uchar *c1 = color1.ptr(i);
		uchar *c2 = color2.ptr(i);
		uchar *c3 = color3.ptr(i);
		uchar *r = temp.ptr(i);
		uchar *m = mask.ptr(i);
		for (int j = 0; j < col; ++j)
		{
			if (m[j] == 255)
			{
				if (r[j] > (3 * 255 / 4) && r[j] <= 255)
				{
					c1[j] = 255;
					c2[j] = 4 * (255 - r[j]);
					c3[j] = 0;
				}
				else if (r[j] <= (3 * 255 / 4) && r[j] > (255 / 2))
				{
					c1[j] = 255 - 4 * (3 * 255 / 4 - r[j]);
					c2[j] = 255;
					c3[j] = 0;
				}
				else if (r[j] <= (255 / 2) && r[j] > (255 / 4))
				{
					c1[j] = 0;
					c2[j] = 255;
					c3[j] = 4 * (255 / 2 - r[j]);
				}
				else if (r[j] <= (255 / 4) && r[j] >= 0)
				{
					c1[j] = 0;
					c2[j] = 255 - 4 * (255 / 4 - r[j]);
					c3[j] = 255;
				}
				else {
					c1[j] = 0;
					c2[j] = 0;
					c3[j] = 0;
				}
			}
		}
	}
 
	// 三通道合并,得到颜色图
	vector images;
	images.push_back(color3);
	images.push_back(color2);
	images.push_back(color1);
	cv::merge(images, result);
 
	return result;
}

测试效果

C++基于灰度图上色GrayToColorFromOther怎么实现 

图1 参考灰度图上色效果

C++基于灰度图上色GrayToColorFromOther怎么实现 

图2 基于参考灰度图色板的上色效果

关于“C++基于灰度图上色GrayToColorFromOther怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。


网页题目:C++基于灰度图上色GrayToColorFromOther怎么实现
URL标题:http://bjjierui.cn/article/jgjogh.html

其他资讯