Color Transfer between Images code实现
上计算机视觉课老师布置的作业实现论文:Color Transfer between Images
基本思路是:
1.给定srcImg和targetImg
2.将RGB空间转为Lab空间
3.根据论文中公式:
计算每一个像素点
4.将resultImg转回到RGB空间显示
效果图:


见代码:
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
using namespace std;
using namespace cv; class ColorTransfer
{
public:
Mat resultImg; ColorTransfer(Mat src, Mat target)
{
src.convertTo(srcImg_32F, CV_32FC3,1.0f/.f);//这里切记要类型转换下
target.convertTo(targetImg_32F, CV_32FC3, 1.0f/255.0f);
resultImg = srcImg_32F; //将结果先初始化为源图像 srcImg_Lab = RGBToLab(srcImg_32F);
targetImg_Lab = RGBToLab(targetImg_32F);
srcMeans = computeMeans(srcImg_Lab);
targetMeans = computeMeans(targetImg_Lab);
srcVariances = computeVariances(srcImg_Lab, srcMeans);
targetVariances = computeVariances(targetImg_Lab, targetMeans);
computeResult();
} private:
//读入的RGB图像
Mat srcImg_32F;
Mat targetImg_32F;
//转换后的Lab空间图像
Mat srcImg_Lab;
Mat targetImg_Lab;
//计算得到的均值和方差
Vector<double> srcMeans;
Vector<double> targetMeans;
Vector<double> srcVariances;
Vector<double> targetVariances; //RGB转换到Lab空间
Mat RGBToLab(Mat m)
{
Mat_<Vec3f> I = m;
for(int i=;i<I.rows;++i)
{
for(int j=;j<I.cols;++j)
{
double L = 0.3811*I(i,j)[] + 0.5783*I(i,j)[] + 0.0402*I(i,j)[];
double M = 0.1967*I(i,j)[] + 0.7244*I(i,j)[] + 0.0782*I(i,j)[];
double S = 0.0241*I(i,j)[] + 0.1288*I(i,j)[] + 0.8444*I(i,j)[];
if(L == ) L = ;
if(M == ) M = ;
if(S == ) S = ;
L = log(L);
M = log(M);
S = log(S); I(i,j)[] = (L+M+S) / sqrt(3.0);
I(i,j)[] = (L+M-*S) / sqrt(6.0);
I(i,j)[] = (L-M) / sqrt(2.0);
}
} return I;
} //Lab转换到RGB空间
Mat LabToRGB(Mat m)
{
Mat_<Vec3f> I = m;
for(int i=;i<I.rows;++i)
for(int j=;j<I.cols;++j)
{
double L = I(i,j)[]/sqrt(3.0) + I(i,j)[]/sqrt(6.0) + I(i,j)[]/sqrt(2.0);
double M = I(i,j)[]/sqrt(3.0) + I(i,j)[]/sqrt(6.0) - I(i,j)[]/sqrt(2.0);
double S = I(i,j)[]/sqrt(3.0) - *I(i,j)[]/sqrt(6.0); L = exp(L);
M = exp(M);
S = exp(S); I(i,j)[] = 4.4679*L - 3.5873*M + 0.1193*S;
I(i,j)[] = -1.2186*L + 2.3809*M - 0.1624*S;
I(i,j)[] = 0.0497*L - 0.2439*M + 1.2045*S;
} return I;
} Vector<double> computeMeans(Mat m)
{
double sum[] = { };
int pixes = m.cols * m.rows;
Vector<double> means;
means.resize();
Mat_<Vec3f> I = m; for(int i=;i<I.rows;++i)
for(int j=;j<I.cols;++j)
{
for(int k = ;k < ;k++)
{
sum[k] += I(i,j)[k];
}
} for(int i = ;i < ;i++)
{
means[i] = sum[i] / pixes;
} return means;
} Vector<double> computeVariances(Mat m, Vector<double> means)
{
double sum[] = { };
int pixes = m.cols * m.rows;
Mat_<Vec3f> I = m;
Vector<double> variances;
variances.resize(); for(int i=;i<I.rows;++i)
for(int j=;j<I.cols;++j)
{
for(int chanel = ;chanel < ;chanel++)
{
sum[chanel] += abs(I(i,j)[chanel] - means[chanel]);
}
} for(int i = ;i < ;i++)
{
variances[i] = sqrt(sum[i] / pixes);
} return variances;
} void computeResult()
{
Mat_<Vec3f> I = resultImg;
double dataTemp[] = { }; for(int chanel =;chanel < ;chanel++)
{
dataTemp[chanel] = targetVariances[chanel] / srcVariances[chanel];
} for(int i=;i<I.rows;++i)
for(int j=;j<I.cols;++j)
{
for(int chanel = ;chanel < ;chanel++)
{
I(i,j)[chanel] = dataTemp[chanel] * (I(i,j)[chanel]-srcMeans[chanel]) + targetMeans[chanel];
}
}
resultImg = LabToRGB(resultImg);
}
}; int main()
{
Mat src = imread("11.jpg");
namedWindow("src");
imshow("src", src);
Mat target = imread("12.jpg");
namedWindow("target");
imshow("target", target);
ColorTransfer clt(src,target);
namedWindow("result");
imshow("result", clt.resultImg);
Mat saveImg;
clt.resultImg.convertTo(saveImg,CV_8U, 255.0, /255.0);//imwrite函数只支持8bit和16bit,前面将图像转为了float,保存前要转换
imwrite("result.jpg",saveImg); waitKey();
return ;
}
Color Transfer between Images code实现的更多相关文章
- 快速 图片颜色转换迁移 Color Transfer Opencv + Python
Super fast color transfer between images About a month ago, I spent a morning down at the beach, w ...
- Maven-008-Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 解决方案
我在部署构件至 maven nexus 私服时,有时会出现 Failed to deploy artifacts: Failed to transfer file: ... Return code i ...
- QA:Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, ReasonPhrase:Method Not Allowed.
QA: Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, Rea ...
- Machine code transfer into assembly code
#include <stdio.h> const char shell[]="\x0f\x01\xf8\xe8\5\0\0\0\x0f\x01\xf8\x48\xcf" ...
- Visual Studio Code 如何编写运行 C、C++ 程序?
0. 前言 VS Code 是微软发布一款跨平台的源代码编辑器,其拥有强大的功能和丰富的扩展,使之能适合编写许多语言. 本文面向初学者(但不是纯小白),分享一点我配置C/C++的经验. 本文所有内容均 ...
- maven报错:Return code is: 501 , ReasonPhrase:HTTPS Required
今天把一个去年没做完的项目翻出来做时,发现maven无法正常导入依赖.检查了一遍项目配置,没发现有什么问题.而且依赖在本地仓库存在. 随后发现报错:Failed to transfer file:** ...
- RGB Color Codes Chart
RGB Color Codes Chart RGB颜色空间 RGB颜色空间或RGB颜色系统,从红色.绿色和蓝色的组合中构造所有颜色. 红色.绿色和蓝色各使用8位,它们的整数值从0到255.这使得256 ...
- {ICIP2014}{收录论文列表}
This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...
- C#读取图片Exif信息
Exif是可交换图像文件的缩写,是专门为数码相机的照片设定的,可以记录数码照片的属性和拍摄数据 ////调用 //string strFile="fffff.jpg";//文件名 ...
随机推荐
- centos下为大硬盘分区(大于2T)
问题:centos/redhat下使用分区工具fdisk创建大于2TB的分区,无法创建 关键字:MBR.GPT.CHS.LBA MBR:主引导记录 GPT:GUID 分区表 CHS:磁柱 磁头 扇区 ...
- oracle--导出、导入blob类型的字段
blob是oracle中的一个数据类型,保存的是压缩后的二进制形式的大数据. 数据迁移如果涉及到blob字段,都不好处理,因为无法用常规方法进行操作,如:使用select查看该字段,也无法用inser ...
- Swift 3 and OpenGL on Linux and macOS with GLFW
https://solarianprogrammer.com/2016/11/19/swift-opengl-linux-macos-glfw/ Swift 3 and OpenGL on Linux ...
- (翻译)开始iOS 7中自动布局教程(二)
这篇教程的前半部分被翻译出来很久了,我也是通过这个教程学会的IOS自动布局.但是后半部分(即本篇)一直未有翻译,正好最近跳坑翻译,就寻来这篇教程,进行翻译.前半部分已经转载至本博客,后半部分即本篇.学 ...
- asp.net 使用DroDownList来实现二级联动
今天做新闻发布系统的时候,用到了二级联动,我把使用方法记录下来,以便日后查阅以及帮助新手朋友们.下面是效果图: 下面来讲解一下实现的方法: 1.在.aspx页面中,拖入两个DroDownList控件. ...
- ie8 jquery parents() 获取多个的问题
今天开发的时候碰到了一个奇怪的问题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3 ...
- 使用libjpeg.framework压缩UIImage
+(void)writeFile:(NSString *)filePath withQuality:(int)quality { //初始化图片参数 UIImage *image=[UIImage i ...
- 我的面板我做主 -- 淘宝UWP中自定义Panel的实现
在Windows10 UWP开发平台上内置的XMAL布局面板包括RelativePanel.StackPanel.Grid.VariableSizedWrapGrid 和 Canvas.在开发淘宝UW ...
- 浅谈算法和数据结构: 十 平衡查找树之B树
前面讲解了平衡查找树中的2-3树以及其实现红黑树.2-3树种,一个节点最多有2个key,而红黑树则使用染色的方式来标识这两个key. 维基百科对B树的定义为“在计算机科学中,B树(B-tree)是一种 ...
- delegate、notification、KVO场景差别
delegate: 编译器会给出没有实现代理方法的警告 一对一 使用weak而不是assign,或者vc消失时置为nil 可以传递参数,还可以接收返回值 notification: 编译期无法排错 一 ...