Color Transfer between images 实现及Code

转载自:http://blog.csdn.net/chenhuaijin/article/details/9292811

Color Transfer between images 是一片比较经典的色调映射的文章,应用率较高,算法也比较简单。适合做较为简单的色调处理。

算法主要步骤;

1. RGB转化为Lab 

2.计算lab空间每个channel的 均值和方差。

3.根据公式

实验结果:


[cpp]  view plain copy
  1. #include <opencv2/opencv.hpp>  
  2. #include <opencv2/core/core.hpp>  
  3. #include <opencv2/imgproc/imgproc.hpp>  
  4. using namespace std;  
  5. using namespace cv;  
  6.   
  7. class ColorTranfer  
  8. {  
  9. private:  
  10.     Mat srcImg;  
  11.     Mat targetImg;  
  12.     Mat srcImg_lab;//CV_32FC3  
  13.     Mat targetImg_lab;//CV_32FC3  
  14.     //Mat result_lab;  
  15.     vector<float> srcMeans;  
  16.     vector<float> srcVariances;  
  17.     vector<float> targetMeans;  
  18.     vector<float> targetVariances;  
  19.     void initialize()  
  20.     {  
  21.         Mat srcImg_32F, targetImg_32F;  
  22.         srcImg.convertTo(srcImg_32F, CV_32FC3,1.0f/255.f);  
  23.         targetImg.convertTo(targetImg_32F, CV_32FC3, 1.0f/255.0f);  
  24.         cvtColor(srcImg_32F, srcImg_lab, CV_BGR2Lab );  
  25.         cvtColor(targetImg_32F, targetImg_lab, CV_BGR2Lab);  
  26.         //if(srcImg_lab.depth() == CV_32FC3) cout << "Yes" << endl;  
  27.         cout << srcImg_lab.depth() << endl;  
  28.         cout << srcImg.depth() << endl;  
  29.         computeMeans();  
  30.         computeVariances();  
  31.   
  32.     }  
  33.     void computeMeans(){  
  34.         int srcPixels = srcImg.rows * srcImg.cols;  
  35.         int targetPixels = targetImg.rows * targetImg.cols;  
  36.         //computing the mean of source image in lab space  
  37.         float sum[3] ={ 0 };  
  38.         for (int row = 0; row < srcImg.rows; row++)  
  39.         {  
  40.             for (int col = 0; col < srcImg.cols; col++)  
  41.             {  
  42.                 //Point pt(col, row);  
  43.                 Vec3f color = srcImg_lab.at<Vec3f>(row, col);  
  44.                 for(int layer = 0; layer < 3; layer++)  
  45.                     sum[layer] += color[layer];               
  46.             }  
  47.         }  
  48.         for (int i = 0; i < 3; i++)  
  49.             srcMeans[i] = sum[i] / srcPixels;  
  50.   
  51.         //computing the mean of target image int lab color space  
  52.         for(int i = 0; i < 3; i++)  
  53.             sum[i] = 0;  
  54.         for (int row = 0; row < targetImg.rows; row++)  
  55.         {  
  56.             for (int col = 0; col < targetImg.cols; col++)  
  57.             {  
  58.                 //Point pt(col, row);  
  59.                 Vec3f color = targetImg_lab.at<Vec3f>(row, col);  
  60.                 for(int layer = 0; layer < 3; layer++)  
  61.                     sum[layer] += color[layer];               
  62.             }  
  63.         }  
  64.         for (int i = 0; i < 3; i++)  
  65.             targetMeans[i] = sum[i] / targetPixels;  
  66.     }//end function compuetMeans  
  67.     void computeVariances(){  
  68.         int srcPixels =  srcImg_lab.cols * srcImg_lab.rows;  
  69.         int targetPixels = targetImg_lab.cols * targetImg_lab.rows;  
  70.         //computing the variance of source image   
  71.         float sum_variance[3] ={0.f};  
  72.         for (int y = 0; y < srcImg_lab.rows; y++)  
  73.         {  
  74.             for (int x = 0; x < srcImg_lab.cols; x++)  
  75.             {  
  76.                 Vec3f color = srcImg_lab.at<Vec3f>(y,x);  
  77.                 sum_variance[0] += (color[0] - srcMeans[0])*(color[0] - srcMeans[0]);  
  78.                 sum_variance[1] += (color[1] - srcMeans[1])*(color[1] - srcMeans[1]);  
  79.                 sum_variance[2] += (color[2] - srcMeans[2])*(color[2] - srcMeans[2]);  
  80.             }  
  81.         }  
  82.         srcVariances[0] = sqrt( sum_variance[0] / srcPixels );  
  83.         srcVariances[1] = sqrt( sum_variance[1] / srcPixels );  
  84.         srcVariances[2] = sqrt( sum_variance[2] / srcPixels );        
  85.           
  86.         //computing the variance of target image   
  87.         for(int i = 0; i < 3; i++)  
  88.             sum_variance[i] = 0.f;  
  89.   
  90.         for (int y = 0; y < targetImg_lab.rows; y++)  
  91.         {  
  92.             for (int x = 0; x < targetImg_lab.cols; x++)  
  93.             {  
  94.                 Vec3f color = targetImg_lab.at<Vec3f>(y,x);  
  95.                 sum_variance[0] += (color[0] - targetMeans[0])*(color[0] - targetMeans[0]);  
  96.                 sum_variance[1] += (color[1] - targetMeans[1])*(color[1] - targetMeans[1]);  
  97.                 sum_variance[2] += (color[2] - targetMeans[2])*(color[2] - targetMeans[2]);  
  98.             }  
  99.         }  
  100.         targetVariances[0] = sqrt( sum_variance[0] / targetPixels );  
  101.         targetVariances[1] = sqrt( sum_variance[1] / targetPixels );  
  102.         targetVariances[2] = sqrt( sum_variance[2] / targetPixels );  
  103.   
  104.   
  105.     }  
  106. public:  
  107.     Mat result;  
  108.     void solve()  
  109.     {  
  110.         initialize();  
  111.         //constructing the final value in very pixel and store the value in result matrix  
  112.         int width = srcImg.cols;  
  113.         int height = srcImg.rows;  
  114.         //Mat result(height, width, CV_32FC3);  
  115.         Mat result_lab(height, width, CV_32FC3);  
  116.         float deta_rate[3];  
  117.         for (int k = 0; k < 3; k++)  
  118.         {  
  119.             deta_rate[k] = targetVariances[k] / srcVariances[k];  
  120.         }  
  121.         for (int y = 0; y < height; y++ )  
  122.         {  
  123.             for (int x = 0; x < width; x++)  
  124.             {  
  125.                 Vec3f color = srcImg_lab.at<Vec3f>(y,x);  
  126.                 Vec3f value;   
  127.                 for(int channel = 0; channel < 3; channel++)  
  128.                     value[channel] = deta_rate[channel]*(color[channel] - srcMeans[channel]) + targetMeans[channel];  
  129.                 result_lab.at<Vec3f>(y,x) = value;   
  130.   
  131.             }  
  132.         }  
  133.         //construct the final image  
  134.         cvtColor(result_lab, result, CV_Lab2BGR);  
  135.     }  
  136.       
  137.     ColorTranfer(Mat src, Mat target):srcImg(src), targetImg(target)  
  138.     {  
  139.         srcMeans.resize(3, 0.f);  
  140.         srcVariances.resize(3, 0.f);  
  141.         targetMeans.resize(3, 0.f);  
  142.         targetVariances.resize(3, 0.f);  
  143.         solve();  
  144.     }  
  145. };  
  146.   
  147. int main()  
  148. {  
  149.     Mat src = imread("11.jpg");  
  150.     Mat target = imread("12.jpg");  
  151.     namedWindow("src");  
  152.     imshow("src", src);  
  153.     namedWindow("target");  
  154.     imshow("target", target);  
  155.     ColorTranfer clt(src, target);  
  156.     namedWindow("result");  
  157.     imshow("result", clt.result);  
  158.     waitKey(0);  
  159.     return 0;  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值