分享

C++-灰度图上色GrayToColor

 翟天保的图书馆 2022-01-13

作者:翟天保Steven
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处

场景需求

       最近有客户提出,想要将解包裹图像转化为有颜色的图像,具备更佳的视觉效果。解包裹图是一个float类型的灰度图像,里面的数值范围类似于从-10.25到20.56这种,客户想要最低的数值为蓝色,最高的数值为红色,中间的数值为绿色。

       针对该需求,我们首先需要将灰度值图转化为0-255的8通道(uchar)灰度图,运用归一化函数可以实现,之后考虑到颜色和灰度的关系,比如最低的颜色为蓝色(0,0,255)对应灰度值0,最高的颜色为红色(255,0,0)对应灰度值255,只需要找出其变化的规律即可。

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

功能函数代码

/**
 * @brief GrayToColor                      灰度图上色
 * @param phase                            输入的灰色图像,通道为1
 * @return                                 上色后的图像
 */
static 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<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);
int main(void)
{
cv::Mat mag, ang,result,result3;
UnitPolar(2001, mag, ang);
mag.at<float>(10, 10) = nan("");

clock_t start, end;
start = clock();
result= GrayToColor(mag);
end = clock();
double diff = end - start;
cout << "time:" << diff/ CLOCKS_PER_SEC << endl;

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<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);
//设置边界
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                                 上色后的图像
 */
static 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<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的图像矩阵,图1为灰度图,图2是经过颜色处理后的颜色图,满足了前面提到的需求。

       如果函数有什么可以改进完善的地方,非常欢迎大家指出,一同进步何乐而不为呢~

       如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多