将RGB颜色空间转换到Lab,修改一幅图像的颜色;
/*
* color_transfer.cpp
*
* Created on: May 21, 2018
* Author: cui
*/
#include
#include
using namespace cv;
using namespace std;
int main( int argc, char *argv[] )
{
Mat src, tar;
src = imread( argv[1] );
if( src.empty() ){
cout << "Couldn't load image" << endl;
return -1;
}
tar = imread( argv[2] );
if( tar.empty() ){
cout << "Couldn't load image" << endl;
return -1;
}
//变换到Lab和CV_32FC1
Mat src_lab, tar_lab;
cvtColor( src, src_lab, COLOR_BGR2Lab );
cvtColor( tar, tar_lab, COLOR_BGR2Lab );
src_lab.convertTo( src_lab, CV_32FC1);
tar_lab.convertTo( tar_lab, CV_32FC1);
//为每幅图像找到每个通道的标准值和std值
Mat mean_src, mean_tar, stdd_src, stdd_tar;
meanStdDev( src_lab, mean_src, stdd_src );
meanStdDev( tar_lab, mean_tar, stdd_tar );
//拆分成独立的通道
vector
split( src_lab, src_chan );
split( tar_lab, tar_chan );
//为每个通道计算颜色分布
for( int i = 0; i < 3; i++ ){
tar_chan[i] -= mean_tar.at
tar_chan[i] *= (stdd_src.at
tar_chan[i] += mean_src.at
cout << "mean_tar[" << i << "]: " << mean_tar.at
cout << "stdd_src.at
cout << "(stdd_src.at
(stdd_src.at
}
//合并通道,转换到CV_8UC1的每个通道,并转换到BGR
Mat output;
merge( tar_chan, output );
output.convertTo( output, CV_8UC1 );
cvtColor( output, output, COLOR_Lab2BGR );
namedWindow( "Source Image", WINDOW_AUTOSIZE );
imshow("Source Image", src);
namedWindow( "Target Image", WINDOW_AUTOSIZE );
imshow("Target Image", tar);
namedWindow( "Result Image", WINDOW_AUTOSIZE );
imshow("Result Image", output);
imwrite("result.jpg", output);
waitKey(0);
return 0;
}
结果