您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++基于灰度圖上色GrayToColorFromOther怎么實(shí)現(xiàn)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“C++基于灰度圖上色GrayToColorFromOther怎么實(shí)現(xiàn)”文章能幫助大家解決問(wèn)題。
之前有提到給灰度圖上色的需求,在此基礎(chǔ)上,還有一種需求,就是基于另一張參考灰度圖的色板來(lái)給當(dāng)前的灰度圖上色,比如參考灰度圖的數(shù)值區(qū)間為-10到10,顏色從藍(lán)到綠再到紅,而當(dāng)前的灰度圖的數(shù)據(jù)區(qū)間為-1到1,若基于參考灰度圖的色板確定數(shù)據(jù)對(duì)應(yīng)的顏色,則當(dāng)前灰度圖的顏色應(yīng)該在綠色左右波動(dòng)。
下方為具體實(shí)現(xiàn)函數(shù)和測(cè)試代碼。
/** * @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); // 將當(dāng)前灰度圖以參考灰度圖的數(shù)據(jù)范圍作標(biāo)準(zhǔn),進(jìn)行數(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()); // 基于灰度圖的灰度層級(jí),給其上色,最底的灰度值0為藍(lán)色(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; }
#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ù)量點(diǎn)數(shù),相鄰數(shù)據(jù)跨度相同 // OpenCV自帶的轉(zhuǎn)換有精度限制,導(dǎo)致結(jié)果有一定差異性 //cv::cartToPolar(x, y, mag, ang, false); //坐標(biāo)轉(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); //兩個(gè)元素的間隔 //計(jì)算其他位置的值 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; // 基于灰度圖的灰度層級(jí),給其上色,最底的灰度值0為藍(lán)色(255,0,0),最高的灰度值255為紅色(0,0,255),中間的灰度值127為綠色(0,255,0) // 不要驚訝藍(lán)色為什么是(255,0,0),因?yàn)镺penCV中是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); // 將當(dāng)前灰度圖以參考灰度圖的數(shù)據(jù)范圍作標(biāo)準(zhǔn),進(jìn)行數(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()); // 基于灰度圖的灰度層級(jí),給其上色,最底的灰度值0為藍(lán)色(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è)試效果
圖1 參考灰度圖上色效果
圖2 基于參考灰度圖色板的上色效果
關(guān)于“C++基于灰度圖上色GrayToColorFromOther怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。