使用对数变换将图像中低灰度值部分扩展,并压缩高灰度值部分,达到强调低灰度的目的,可用于图像偏暗的图像增强。其逆变换可以强调高灰度。与前述直方图均衡化类似,其对应灰度变换函数:
s = c ∗ l o g v + 1 ( 1 + v ∗ r ) s=c*{\rm log}_{v+1}(1+v*r) s=c∗logv+1(1+v∗r)
这里的对数变换的底数是 v + 1 v+1 v+1,输入输出的取值范围都是 [ 0 , 1 ] [0,1] [0,1]。不同底数对应变换曲线如图:
显然底数越大时,对低灰度的强调就越强,对高灰度部分的压缩也就也强。
f = 0:0.01:1;
v1 = 1;
y1 = log2( 1+v1*f)/log2(1+v1);
v2 = 10;
y2 = log2(1 + v2*f)/log2(v2+1);
v3 = 50;
y3 = log2(1 + v3*f)/log2(v3+1);
v4 = 100;
y4 = log2(1 + v4*f)/log2(v4+1);
v5 = 200;
y5 = log2(1 + v5*f)/log2(v5+1);
plot(f,y1,'g',f,y2,'r',f,y3,'b',f,y4,'c',f,y5,'y');
xlabel('Input gray level');
ylabel('Output gray level');
title('Log transformation: g = log(1+v*r)/log(1+v)');
lgd = legend('Log(v=1)','Log(v=10)','Log(v=50)','Log(v=100)','Log(v=200)');
legend('Location','southeast');
title(lgd,'The Legend Title')
%% logTransform_f.m
function [res]= logTransform_f(img,v)
res=log2(1+v*img)/log2(1+v);
end
%% logTransform_base.m
close all;
clear
img = double(dicomread('D:\dataset\AMRGAtlas\UOA0000101\IM-0004-0012.dcm'));
img_gray = mat2gray(img);
figure(1);
x=0:0.01:1;
y1 = logTransform_f(x,10);
y2 = logTransform_f(x,100);
y3 = x;
plot(x,y1,x,y2,x,y3);
xlabel ('x 轴');
ylabel ('y 轴');
text(0.7,logTransform_f(0.7,10),'log_{10}','FontSize',15);
text(0.2,logTransform_f(0.2,100),'log_{100}','FontSize',15);
text(0.5,0.5,'x','FontSize',15);
figure(2);
res1 = logTransform_f(img_gray,10);
res2 = logTransform_f(img_gray,100);
subplot(3,2,1);
imshow(img_gray,[0 1]);
xlabel('gray');
subplot(3,2,3);
imshow(res1,[0 1]);
xlabel('log10');
subplot(3,2,5);
imshow(res2,[0 1]);
xlabel('log100');
a=256;
subplot(3,2,2);
imhist(img_gray,a)
title('灰度图hist');
subplot(3,2,4);
imhist(res1,a)
title('log10 hist');
subplot(3,2,6);
imhist(res2,a)
title('log100 hist');