【OPENCV】图像颜色转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/RachelKong/article/details/51416100

程序功能:读取源图像和目标图像,进行图像的颜色转换。

注意:使用Open CV,需要进行环境配置。处理的图片放在项目中。

#include <opencv2/highgui.hpp>
#include <D:\OpenCVWork\opencv\opencv\include\opencv\cv.h>
#include<iostream>  
#include<cmath>  
using namespace std;
using namespace cv;

//get mean
void getMean(IplImage *img,double *mean){
	int i, j;
	CvScalar s;

	for (i = 0; i<img->height; i++)
	{
		for (j = 0; j<img->width; j++)
		{
			s = cvGet2D(img, i, j);
			mean[0] = mean[0] + s.val[0];
			mean[1] = mean[1] + s.val[1];
			mean[2] = mean[2] + s.val[2];
		}
	}

	for (i = 0; i<3; i++)
	{
		mean[i] = mean[i] / ((img->height)*(img->width));
	}
}

//get Std
void getStd(IplImage *img, double *mean,double *std){
	int i, j;
	CvScalar s;
	for (i = 0; i<img->height; i++)
	{
		for (j = 0; j<img->width; j++)
		{
			s = cvGet2D(img, i, j);
			std[0] = std[0] + (s.val[0] - mean[0])*(s.val[0] - mean[0]);
			std[1] = std[1] + (s.val[1] - mean[1])*(s.val[1] - mean[1]);
			std[2] = std[2] + (s.val[2] - mean[2])*(s.val[2] - mean[2]);
		}
	}

	for (i = 0; i<3; i++)
	{
		std[i] = sqrt(std[i] / ((img->width)*(img->height)));
		
	}

}
int main()
{
	IplImage *src1 = cvLoadImage("pic15.jpg");//the picture you want to transform color
	IplImage *src2 = cvCreateImage(cvGetSize(src1), src1->depth, src1->nChannels);//to deal with it

	cvCvtColor(src1, src2, CV_BGR2Lab);//src2 : transform from RGB to LAB
	//cvCvtColor(src1, src2, CV_BGR2HSV);//mode2
	//cvCvtColor(src1, src2, CV_BGR2XYZ);//mode3
	//cvCvtColor(src1, src2, CV_BGR2YUV);//mode4
	CvMat *out = cvCreateMat(src2->height, src2->width, CV_8UC3);//the target picture to put out

	IplImage *dst1 = cvLoadImage("pic16.jpg");//reference color
	IplImage *dst2 = cvCreateImage(cvGetSize(dst1), dst1->depth, dst1->nChannels);//to deal with it
	
    cvCvtColor(dst1, dst2, CV_BGR2Lab);
	//cvCvtColor(dst1, dst2, CV_BGR2HSV);//mode2
	//cvCvtColor(dst1, dst2, CV_BGR2XYZ);//mode3
	//cvCvtColor(dst1, dst2, CV_BGR2YUV);//mode4

	int i, j;
	double mean1[3], mean2[3];
	double std1[3], std2[3];

	for (i = 0; i<3; i++)//initialize the arrays
	{
		mean1[i] = 0;
		mean2[i] = 0;
		std1[i] = 0;
		std2[i] = 0;
	}

	getMean(src2, mean1);//to calculate the value of mean (src)
	getMean(dst2, mean2);//mean of dst
	getStd(src2, mean1,std1);//std of src
	getStd(dst2, mean2, std2);//std of dst

	CvScalar s;//have at most four dimensions
	for (i = 0; i<src2->height; i++)
	{
		for (j = 0; j<src2->width; j++)
		{
			s = cvGet2D(src2, i, j);
			s.val[0] = (s.val[0] - mean1[0])*(std2[0] / std1[0]) + mean2[0];
			s.val[1] = (s.val[1] - mean1[1])*(std2[1] / std1[1]) + mean2[1];
			s.val[2] = (s.val[2] - mean1[2])*(std2[2] / std1[2]) + mean2[2];
			cvSet2D(src2, i, j, s);//img height width cvScalarValue
		}
	}

	IplImage *src3 = cvCreateImage(cvGetSize(src1), src1->depth, src1->nChannels);
	cvCvtColor(src2, src3, CV_Lab2BGR);//src3: transform from lab to RGB
	//cvCvtColor(src2, src3, CV_HSV2BGR);//mode2
	//cvCvtColor(src2, src3, CV_XYZ2BGR);//mode3
	//cvCvtColor(src2, src3, CV_YUV2BGR);//mode4

	cvNamedWindow("source", 1);
	cvNamedWindow("refer", 1);
	cvNamedWindow("result", 1);

	cvShowImage("source", src1);
	cvShowImage("refer", dst1);
	cvShowImage("result", src3);
	cvSaveImage("save.jpg", src3);
	cvWaitKey(0);
	cvReleaseImage(&src1);
	cvReleaseImage(&src3);
	cvReleaseImage(&dst1);
	cvDestroyWindow("source");
	cvDestroyWindow("refer");
	cvDestroyWindow("result");
	return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页