直方图均衡是一种图像增强的常用手段,但全局直方均衡经常会给图像带来失真等问题,为了处理相关问题,本文采取了全局直方均衡、局部直方均衡和Retinex算法来对图像进行处理。
img = imread('img.tif');
In1 = RemoveFogByGolbalHisteq(img, 1);
In2 = RemoveFogByLocalHisteq(img, 1);
In3 = RemoveFogByRetinex(img, 1);
function In = RemoveFogByGolbalHisteq(I, flag)
%I为输入图像
%flag为显示标记
%In为输出图像
if nargin < 2
flag = 1;
end
%提取R、G、B分量
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
%分别对R、G、B分量做全局直方均衡
M = histeq(R);
N = histeq(G);
L = histeq(B);
%合成图像
In = cat(3, M, N, L);
%结果显示
if flag
figure;
subplot(2, 2, 1); imshow(I); title('原图像');
subplot(2, 2, 2); imshow(In); title('处理后的图像');
%灰度画
Q = rgb2gray(I);
W = rgb2gray(In);
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图');
subplot(2, 2, 4); imhist(W, 64); title('处理后的灰度直方图');
end
function In = RemoveFogByLocalHisteq(I, flag)
if nargin < 2
flag = 1;
end
%对R、G、B三层向量分别做局部均衡化处理
g1 = GetLocalHisteq(I(:,:,1));
g2 = GetLocalHisteq(I(:,:,2));
g3 = GetLocalHisteq(I(:,:,3));
In = cat(3, g1, g2, g3);
if flag
figure;
subplot(2, 2, 1); imshow(I); title('原图像');
subplot(2, 2, 2); imshow(In); title('处理后的图像');
%灰度画
Q = rgb2gray(I);
W = rgb2gray(In);
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图');
subplot(2, 2, 4); imhist(W, 64); title('处理后的灰度直方图');
end
function g = GetLocalHisteq(I)
g = adapthisteq(I, 'clipLimit', 0.02, 'Distribution', 'rayleigh');
function In = RemoveFogByRetinex(I, flag)
%f为输入图像
%flag为显示标记
if nargin < 2
flag = 1;
end
%提取R、G、B分量
R = I(:, :, 1);
G = I(:, :, 2);
B = I(:, :, 3);
%数据类型归一化
mr = mat2gray(im2double(R));
mg = mat2gray(im2double(G));
mb = mat2gray(im2double(B));
%定义alpha参数
alpha = randi([80, 100], 1) * 20; %生成一个在80~100之内的数乘以20
%定义模板大小
n = floor(min([size(I, 1) size(I, 2)]) * 0.5); %模板尺寸为原图矩阵最小边的一半
%计算中心
n1 = floor((n + 1) / 2);
for i = 1:n
for j = 1:n
%高斯函数
b(i, j) = exp(-((i-n1)^2 + (j-n1)^2)/(4*alpha))/(pi*alpha);
end
end
%卷积滤波
nr1 = imfilter(mr, b, 'conv', 'replicate');
ng1 = imfilter(mg, b, 'conv', 'replicate');
nb1 = imfilter(mb, b, 'conv', 'replicate');
ur1 = log(nr1);
ug1 = log(ng1);
ub1 = log(nb1);
tr1 = log(mr);
tg1 = log(mg);
tb1 = log(mb);
yr1 = (tr1 - ur1) / 3;
yg1 = (tg1 - ug1) / 3;
yb1 = (tb1 - ub1) / 3;
%定义beta参数
beta = randi([80 100], 1) * 1;
%定义模板大小
for i = 1:n
for j = 1:n
a(i, j) = exp(-((i-n1)^2 + (j-n1)^2)/(4*beta))/(6*pi*beta);
end
end
%卷积滤波
nr2 = imfilter(mr, a, 'conv', 'replicate');
ng2 = imfilter(mg, a, 'conv', 'replicate');
nb2 = imfilter(mb, a, 'conv', 'replicate');
ur2 = log(nr2);
ug2 = log(ng2);
ub2 = log(nb2);
tr2 = log(mr);
tg2 = log(mg);
tb2 = log(mb);
yr2 = (tr2 - ur2) / 3;
yg2 = (tg2 - ug2) / 3;
yb2 = (tb2 - ub2) / 3;
%定义eta参数
eta = randi([80 100], 1) * 200;
for i = 1:n
for j = 1:n
a(i, j) = exp(-((i-n1)^2 + (j-n1)^2)/(4*beta))/(4*pi*beta);
end
end
%卷积滤波
nr3 = imfilter(mr, a, 'conv', 'replicate');
ng3 = imfilter(mg, a, 'conv', 'replicate');
nb3 = imfilter(mb, a, 'conv', 'replicate');
ur3 = log(nr3);
ug3 = log(ng3);
ub3 = log(nb3);
tr3 = log(mr);
tg3 = log(mg);
tb3 = log(mb);
yr3 = (tr3 - ur3) / 3;
yg3 = (tg3 - ug3) / 3;
yb3 = (tb3 - ub3) / 3;
dr = yr1 + yr2 + yr3;
dg = yg1 + yg2 + yg3;
db = yb1 + yb2 + yb3;
cr = im2uint8(dr);
cg = im2uint8(dg);
cb = im2uint8(db);
%通过集成处理后的分量得到结果图像
In = cat(3, cr, cg, cb);
%结果显示
if flag
figure;
subplot(2, 2, 1); imshow(I); title('原图像');
subplot(2, 2, 2); imshow(In); title('处理后的图像');
%灰度画
Q = rgb2gray(I);
W = rgb2gray(In);
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图');
subplot(2, 2, 4); imhist(W, 64); title('处理后的灰度直方图');
end