基本的灰度变换是基于图像的空间域(图像空间域:即指的是图像平面本身;对应的是图像的变换域)进行像素点对点的操作,具体的变换有:图像反转、对数变换、幂律(伽马)变换、分段线性变换等等。
1、图像反转:主要是依据公式:s = L - 1 - r 进行图像的灰度范围进行反转。具体看代码。
<span style="font-size:18px;">% 图像反转
% 适用于增强嵌入在一副图像的暗区域中的白色或灰色细节
im = imread('1.tif');
figure;subplot(121); imshow(im);
im = 256 - 1 - im;
subplot(122); imshow(im);</span>
输出:左边是原图,右边为反转后图像
2、图像的对数变换
<span style="font-size:18px;">%%
% 对数变换
% 用来压缩像素值变化较大的图像的动态范围
% 在傅里叶频谱分析中,一般都要用对数变换对很大范围的傅里叶频谱值域进行压缩,以便于显示。
im = imread('2.tif');
figure;subplot(121); imshow(im);
c = 1;
im = c * log( 1 + double(im) );
subplot(122); imshow(im, []);</span>
3、幂律(伽马)变换
<span style="font-size:18px;">% 幂律(伽马)变换
% 常见应用:伽马校正, 改变图像的对比度
% 伽马大于1时,得到的图像比原图像暗,相反gimgmimg值小于1时,得到的图像比原始图像亮
im = imread('3.tif');
figure;subplot(221);imshow(im); title('原图');
c = 1;
imf = double(im);
gamma = 0.6;
imf = c * ( imf ).^gamma ;
subplot(222);imshow(imf, []); title('gamma = 0.6');
gamma = 0.1;
imf = double(im);
imf = c * ( imf ).^gamma ;
subplot(223);imshow(imf, []); title('gamma = 0.1')
gamma = 1.6;
imf = double(im);
imf = c * ( imf ).^gamma ;
subplot(224);imshow(imf, []); title('gamma = 1.6');</span>
4、分段线性变换
主要有对比度拉伸、灰度级分层、比特平面分层。其中比特平面分层:高阶比特平面包含了视觉上很重要的大多数数据,而低阶比特平面在图像中贡献了更精细的灰度细节。常常应用在图像压缩领域。
img =imread('lena.bmp');
img1 = img;
figure;subplot(3,3,1);imshow( img ) ;title('原始图像');
%比特平面编码
%方法一:
for i = 8: -1: 1
A_bitplane = bitshift( bitget(img,i), i - 1 );
subplot( 3, 3, 9 - i + 1 );
imshow( A_bitplane );
title( ['位平面 ' num2str(i)] );
end
%方法二:
img2 = img;
figure;subplot(3,3,1);imshow( img ) ;title('原始图像');
imd = cell(1,8); %新建元胞数组方便解码使用
[m,n] = size(img);
for i = 8: -1:1
for x = 1: m
for y = 1: n
if ( bitget( img(x, y), i ) == 1 )
img2(x,y) = 2.^(i - 1); %将属于该比特平面的像素点映射为 2.^(i - 1)
else
img2(x,y) = 0; %将不属于该比特平面的像素点映射为0 这样,最后得到每一层的比特平面都只包含两个灰度级。
end
end
end
subplot( 3, 3, 8 - i + 2 );
imshow( img2 );
title( ['My_ 位平面 ' num2str(i)] );
imd{i} = img2;
end
%比特平面解码:编码的逆过程,简单的灰度相加就行。
figure;subplot(2, 2, 1);imshow(img);title('原始图像');
for i = 6 : 8
recover = zeros( size(img) );
recover = uint8( recover );
for j = i: 8
tmp = imd{j} ;
recover = recover + tmp ;
end
subplot(2, 2, i - 5 + 1);imshow(recover); title( ['位平面' num2str(i) '-----8合成']);
end
方法一:充分利用matlab库函数,简单快捷而且速度快。
方法二:为自己实现,原理同方法一,程序相对简单容易理解,但速度较慢。
推荐使用方法一。
从解码过程可以看出,仅用6、7、8比特平面就可以很好的实现原始图像的恢复;也就是将8比特的原始图像压缩到3比特的图像,实现了图像的压缩存储。