去光照不均匀

论文地址:http://journal.bit.edu.cn/zr/ch/reader/create_pdf.aspx?file_no=20160216&year_id=2016&quarter_id=2&falg=1

 

Matlab代码:

clc, close all; tic;
im = imread('test.png'); 
figure; imshow(im); 
title('原图');
[h, s, v] = rgb2hsv(im);    %转到hsv空间,对亮度h处理% 高斯滤波
HSIZE = min(size(im, 1), size(im, 2)); %高斯卷积核尺寸
q = sqrt(2);
SIGMA1 = 15; %论文里面的c
SIGMA2 = 80; 
SIGMA3 = 250; 
F1 = fspecial('gaussian', HSIZE, SIGMA1 / q);
F2 = fspecial('gaussian', HSIZE, SIGMA2 / q);
F3 = fspecial('gaussian', HSIZE, SIGMA3 / q); 
gaus1 = imfilter(v, F1, 'replicate'); 
gaus2 = imfilter(v, F2, 'replicate');
gaus3 = imfilter(v, F3, 'replicate');
gaus = (gaus1 + gaus2 + gaus3) / 3;    %多尺度高斯卷积,加权,权重为1 / 3 
% gaus = (gaus * 255); 
figure; imshow(gaus, []); 
title('光照分量'); 
%二维伽马卷积
m = mean(gaus(:)); 
[w, height] = size(v); 
out = zeros(size(v)); 
gama = power(0.5, ((m - gaus) / m)); %根据公式gamma校正处理,论文公式有误
out = (power(v, gama)); 
figure; imshow(out, []); 
rgb = hsv2rgb(h, s, out);   %转回rgb空间显示
figure; imshow(rgb); 
title('处理结果')
imwrite(rgb, 'test1.png');
toc;

C++代码:

void ImageInhomogeneity(Mat & src, Mat &dst)
{
	Mat src_hsv, src_h, src_s, src_v;
	cvtColor(src, src_hsv, COLOR_BGR2HSV);
	vector rgb_planes;
	split(src_hsv, rgb_planes);
	src_h = rgb_planes[0];
	src_s = rgb_planes[1];
	src_v = rgb_planes[2];


	Mat v = Mat::zeros(src_v.size(), CV_64FC1);
	for (int i = 0; i < src_v.rows; i++)
	{
		for (int j = 0; j < src_v.cols; j++)
		{
			v.at(i, j) = (double)src_v.at(i, j) / 255;
		}
	}

	double hsize = min(src.cols, src.rows);
	double q = sqrtf(2);
	double SIGMA1 = 15;
	double SIGMA2 = 80;
	double SIGMA3 = 250;

	Mat F1 = getGaussianKernel(hsize, SIGMA1 / q);
	Mat F2 = getGaussianKernel(hsize, SIGMA2 / q);
	Mat F3 = getGaussianKernel(hsize, SIGMA3 / q);

	Mat F1_1 = Mat::zeros(Size(hsize, hsize),CV_64FC1);
	Mat F2_1 = Mat::zeros(Size(hsize, hsize), CV_64FC1);
	Mat F3_1 = Mat::zeros(Size(hsize, hsize), CV_64FC1);
	for (int i = 0; i < hsize; i++)
	{
		for (int j = 0; j < hsize; j++)
		{
			F1_1.at(i, j) = F1.at(i, 0) * F1.at(j, 0);
			F2_1.at(i, j) = F2.at(i, 0) * F2.at(j, 0);
			F3_1.at(i, j) = F3.at(i, 0) * F3.at(j, 0);
		}
	}


	Mat gaus1, gaus2, gaus3;


	filter2D(v, gaus1, v.type(), F1_1);
	filter2D(v, gaus2, v.type(), F2_1);
	filter2D(v, gaus3, v.type(), F3_1);
	Mat gaus = (gaus1 + gaus2 + gaus3) / 3;


	Scalar mm = mean(gaus);
	double m = mm[0];

	Mat gama = gaus.clone();
	Mat out = gaus.clone();
	for (int i = 0; i < gama.rows; i++)
	{
		for (int j = 0; j < gama.cols; j++)
		{
			gama.at(i, j) = pow(0.5, ((m - gaus.at(i, j)) / m));
			out.at(i, j) = pow(v.at(i, j), gama.at(i, j));
		}
	}

	for (int i = 0; i < src_v.rows; i++)
	{
		for (int j = 0; j < src_v.cols; j++)
		{
			src_v.at(i, j) = out.at(i, j)*(double)255;
		}
	}


	Mat outtt, outtt1;
	vector hsv_planes;
	hsv_planes.push_back(src_h);
	hsv_planes.push_back(src_s);
	hsv_planes.push_back(src_v);
	merge(hsv_planes, outtt);
	cvtColor(outtt, dst, COLOR_HSV2BGR);

	cout << "";
}

效果:

原图:

 

去光照不均匀_第1张图片

处理后的图:

 

 

去光照不均匀_第2张图片

你可能感兴趣的:(视觉算法)