最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++基于灰度圖上色GrayToColorFromOther的實現(xiàn)

 更新時間:2021年07月19日 11:17:59   作者:翟天保Steven  
本文主要介紹了C++基于灰度圖上色GrayToColorFromOther的實現(xiàn),文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧

場景需求

       之前有提到給灰度圖上色的需求,在此基礎(chǔ)上,還有一種需求,就是基于另一張參考灰度圖的色板來給當前的灰度圖上色,比如參考灰度圖的數(shù)值區(qū)間為-10到10,顏色從藍到綠再到紅,而當前的灰度圖的數(shù)據(jù)區(qū)間為-1到1,若基于參考灰度圖的色板確定數(shù)據(jù)對應(yīng)的顏色,則當前灰度圖的顏色應(yīng)該在綠色左右波動。

       下方為具體實現(xiàn)函數(shù)和測試代碼。

功能函數(shù)代碼

/**
 * @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;
	// 確定參考灰度圖的數(shù)據(jù)范圍
	cv::minMaxIdx(phase1, &min1, &max1, nullptr, nullptr, phase1 == phase1);
	// 將當前灰度圖以參考灰度圖的數(shù)據(jù)范圍作標準,進行數(shù)據(jù)變換
	temp = phase2.clone();
	for (int i = 0; i < row; ++i)
	{
		float *t2 = temp.ptr<float>(i);
		for (int j = 0; j < col; ++j)
		{
			t2[j] = 255.0f*(phase2.at<float>(i, j) - min1) / (max1 - min1);
		}
	}
	temp.convertTo(temp, CV_8UC1);
	// 創(chuàng)建掩膜,目的是為了隔離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<uchar>(i);
		uchar *c2 = color2.ptr<uchar>(i);
		uchar *c3 = color3.ptr<uchar>(i);
		uchar *r = temp.ptr<uchar>(i);
		uchar *m = mask.ptr<uchar>(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<cv::Mat> images;
	images.push_back(color3);
	images.push_back(color2);
	images.push_back(color1);
	cv::merge(images, result);
 
	return result;
}

C++測試代碼

#include<iostream>
#include<opencv2/opencv.hpp>
#include<ctime>
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<float>(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);                //產(chǎn)生指定范圍內(nèi)的指定數(shù)量點數(shù),相鄰數(shù)據(jù)跨度相同
	// OpenCV自帶的轉(zhuǎn)換有精度限制,導致結(jié)果有一定差異性
	//cv::cartToPolar(x, y, mag, ang, false); //坐標轉(zhuǎn)換
 
	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<float>(i);
		a = ang.ptr<float>(i);
		xx = x.ptr<float>(i);
		yy = y.ptr<float>(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);
	//設(shè)置邊界
	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);
	// 創(chuàng)建掩膜,目的是為了隔離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<uchar>(i);
		uchar *c2 = color2.ptr<uchar>(i);
		uchar *c3 = color3.ptr<uchar>(i);
		uchar *r = temp.ptr<uchar>(i);
		uchar *m = mask.ptr<uchar>(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<cv::Mat> 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;
	// 確定參考灰度圖的數(shù)據(jù)范圍
	cv::minMaxIdx(phase1, &min1, &max1, nullptr, nullptr, phase1 == phase1);
	// 將當前灰度圖以參考灰度圖的數(shù)據(jù)范圍作標準,進行數(shù)據(jù)變換
	temp = phase2.clone();
	for (int i = 0; i < row; ++i)
	{
		float *t2 = temp.ptr<float>(i);
		for (int j = 0; j < col; ++j)
		{
			t2[j] = 255.0f*(phase2.at<float>(i, j) - min1) / (max1 - min1);
		}
	}
	temp.convertTo(temp, CV_8UC1);
	// 創(chuàng)建掩膜,目的是為了隔離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<uchar>(i);
		uchar *c2 = color2.ptr<uchar>(i);
		uchar *c3 = color3.ptr<uchar>(i);
		uchar *r = temp.ptr<uchar>(i);
		uchar *m = mask.ptr<uchar>(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<cv::Mat> images;
	images.push_back(color3);
	images.push_back(color2);
	images.push_back(color1);
	cv::merge(images, result);
 
	return result;
}

測試效果

 

圖1 參考灰度圖上色效果

 

圖2 基于參考灰度圖色板的上色效果

       如上圖所示,為了方便,我生成了一個2001*2001的圖像矩陣,并設(shè)置了另一個對比圖像,該圖像為原圖像的1/2,那么原圖像就是參考灰度圖,而對比圖像就是需要基于參考灰度圖色板上色的灰度圖。圖1為參考灰度圖的上色效果,圖2是基于參考灰度圖色板給對比圖像上色的效果圖。原圖像的數(shù)據(jù)從0-1.3左右,其顏色變化從藍到綠再到紅,而對比圖像的數(shù)據(jù)從0-1.3/2左右,則顏色變化為藍到綠,滿足了前面提到的需求。

到此這篇關(guān)于C++基于灰度圖上色GrayToColorFromOther的實現(xiàn)的文章就介紹到這了,更多相關(guān)C++ 灰度圖上色GrayToColorFromOther內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++ const關(guān)鍵字分析詳解

    C++ const關(guān)鍵字分析詳解

    C++中的const關(guān)鍵字的用法非常靈活,而使用const將大大改善程序的健壯性。這篇文章主要介紹了C/C++ 中const關(guān)鍵字的用法,需要的朋友可以參考下
    2021-08-08
  • win32 api實現(xiàn)2048游戲示例

    win32 api實現(xiàn)2048游戲示例

    這篇文章主要介紹了win32 api實現(xiàn)2048游戲示例,需要的朋友可以參考下
    2014-05-05
  • 淺談C語言的字節(jié)對齊 #pragma pack(n)2

    淺談C語言的字節(jié)對齊 #pragma pack(n)2

    下面小編就為大家?guī)硪黄獪\談C語言的字節(jié)對齊 #pragma pack(n)2。小編覺得挺不錯的現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • C語言用棧模擬實現(xiàn)隊列問題詳解

    C語言用棧模擬實現(xiàn)隊列問題詳解

    本片文章帶你分析如何用兩個棧,并且只使用棧的基本功能來模擬實現(xiàn)隊列,其中同樣只實現(xiàn)隊列的基本功能,感興趣的朋友來看看吧
    2022-04-04
  • 關(guān)于C++復制構(gòu)造函數(shù)的實現(xiàn)講解

    關(guān)于C++復制構(gòu)造函數(shù)的實現(xiàn)講解

    今天小編就為大家分享一篇關(guān)于關(guān)于C++復制構(gòu)造函數(shù)的實現(xiàn)講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C語言詳解select函數(shù)的使用

    C語言詳解select函數(shù)的使用

    C語言中select函數(shù)的使用?一般用connect、accept、recv或recvfrom這類函數(shù),程序阻塞,直至該套接字上接受到數(shù)據(jù)后程序才能繼續(xù)運行。但是使用select函數(shù)可以實現(xiàn)非阻塞方式的程序
    2022-05-05
  • C/C++實現(xiàn)貪吃蛇逐步運動效果

    C/C++實現(xiàn)貪吃蛇逐步運動效果

    這篇文章主要為大家詳細介紹了C/C++實現(xiàn)貪吃蛇逐步運動效果的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 關(guān)于數(shù)組做函數(shù)參數(shù)的問題集合匯總

    關(guān)于數(shù)組做函數(shù)參數(shù)的問題集合匯總

    本文是對關(guān)于數(shù)組做函數(shù)參數(shù)的問題進行了詳細的匯總,需要的朋友可以過來參考下。希望對大家有所幫助
    2013-10-10
  • C語言設(shè)計實現(xiàn)掃描器的自動機的示例詳解

    C語言設(shè)計實現(xiàn)掃描器的自動機的示例詳解

    這篇文章主要為大家詳細介紹了如何利用C語言設(shè)計實現(xiàn)掃描器的自動機,可識別的單詞包括:關(guān)鍵字、界符、標識符和常整型數(shù),感興趣的小伙伴可以了解一下
    2022-12-12
  • VC++文件監(jiān)控之ReadDirectoryChangesW

    VC++文件監(jiān)控之ReadDirectoryChangesW

    文章主要介紹文件監(jiān)控的另一種實現(xiàn)方式,利用ReadDirectoryChangesW來實現(xiàn)文件的監(jiān)控,希望對大家有幫助
    2019-04-04

最新評論

多伦县| 红桥区| 通榆县| 德江县| 堆龙德庆县| 广安市| 自贡市| 义马市| 浙江省| 黑龙江省| 北票市| 天津市| 民勤县| 晋中市| 桑日县| 忻城县| 若尔盖县| 浮梁县| 武安市| 黎平县| 新干县| 云林县| 衡山县| 卓资县| 海丰县| 辽中县| 南丹县| 平定县| 广东省| 枣强县| 嘉祥县| 泸水县| 津南区| 东山县| 雷山县| 曲阳县| 泉州市| 甘谷县| 双鸭山市| 蒙自县| 通州区|