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<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);
  // 创建掩膜,目的是为了隔离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);                //产生指定范围内的指定数量点数,相邻数据跨度相同
  // 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                                 上色后的图像
 */
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;
}
/**
 * @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<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);
  // 创建掩膜,目的是为了隔离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的图像矩阵,并设置了另一个对比图像,该图像为原图像的1/2,那么原图像就是参考灰度图,而对比图像就是需要基于参考灰度图色板上色的灰度图。图1为参考灰度图的上色效果,图2是基于参考灰度图色板给对比图像上色的效果图。原图像的数据从0-1.3左右,其颜色变化从蓝到绿再到红,而对比图像的数据从0-1.3/2左右,则颜色变化为蓝到绿,满足了前面提到的需求。


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


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

相关文章
|
11月前
|
C++
C++-灰度图上色GrayToColor
C++-灰度图上色GrayToColor
|
存储 编译器 C++
自写动态数组——参考《C++语言程序设计》(清华大学第4版)
自写动态数组——参考《C++语言程序设计》(清华大学第4版)
138 0
|
C++ iOS开发 开发工具
2014秋C++第16周 项目5参考 编程处理C++源代码
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。  【项目5-编程处理C++源代码】  在CodeBlocks等IDE中都提供了代码格式整理的功能。我们可以编写程序,处理的数据就是用C++写的源代码文件。C++源文件是一种文本文件,可以通过程序进行操
1234 2
|
C++ 容器
C++参考的翻译或校对
做新年规划的时候,我说过要翻译C++常用类的参考。C++的参考,其实别人已经翻译完了,只是部分内容需要校对。由于网站结构中大量使用了模板,同一个函数只需要翻译一个地方,所以四天就弄完了。
736 0
|
C++
2014秋C++第17周 项目2参考 引用作形参
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。  【项目2-引用作形参】设计一个程序,输入3个整数,将其按从大到小的顺序输出,要求(1)排序功能通过函数实现,3个整数用3个变量,不必定义数组;(2)写出两个版本的函数,一个采用传地址值的方法,另一个
1080 0
|
C++
2014秋C++第17周 项目3参考 胖子伤不起
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。  【项目3 - 胖子伤不起】根据世界卫生组织推荐的体重标准,男性的标准体重=(身高cm-80)×70﹪,女性的标准体重=(身高cm-70)×60﹪。标准体重正负10﹪为正常体重;标准体重正负10﹪~
1157 0
|
C++
2014秋C++第17周 项目4参考 日期结构体
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。  【项目4 - 日期结构体】(1)定义一个结构体变量(包括年、月、日),要求输入年、月、日,计算输出该日是该年的第几天。 #include &lt;iostream&gt; using namespa
1014 0
|
C++ Windows
2014秋C++第17周 项目5参考 玩日期时间
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。  【项目5-玩日期时间】定义一个表示时间(包括年、月、日、时、分、秒)的结构体,然后完成下面的功能。提示:将各个功能分别设计成函数实现,在main函数中调用,进行测试。可以设计一个函数,即刻进行测试,
1250 0
|
C++ iOS开发 存储
2014秋C++第17周 OJ题目及参考
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。  A. 3数求平均 Description 输入3个整数,输出它们的平均值,保留3位小数 Input 3个整数 Output 3数的平均值,要求输出3位小数 Sample Input 3 5 2 S
1233 0