1. 灰度变换
1.1. 灰度线性变换
f =imread('../pic/lema.png');
f0 = rgb2gray(f);
[M,N] =size(f0);
k=5;b =-55;
g0= uint8(double(f0)*k +b);
k=0.8;b =-55;
g1= f0*k +b;
k=1;b =55;
g2= uint8(double(f0)*k+b);
k=-1;b =255;
g3= uint8(double(f0)*k+b);
figure;
subplot(231);imshow(f0);title('原始图像');
subplot(232);imshow(g0);title('线性变换,k=2,b=-55');
subplot(233);imshow(g1);title('线性变换,k=0.8,b=-55');
subplot(234);imshow(g2);title('线性变换,k=1,b=55');
subplot(235);imshow(g3);title('线性变换,k=-1,b=255');
figure;
[counts,x]=imhist(f0,30);
count =counts/M/N;
subplot(231);stem(x,count);title('原始图像');
[counts,x]=imhist(g0,30);
count =counts/M/N;
subplot(232);stem(x,count);title('线性变换,k=2,b=-55');
[counts,x]=imhist(g1,30);
count =counts/M/N;
subplot(233);stem(x,count);title('线性变换,k=0.8,b=-55');
[counts,x]=imhist(g2,30);
count =counts/M/N;
subplot(234);stem(x,count);title('线性变换,k=1,b=55');
[counts,x]=imhist(g3,30);
count =counts/M/N;
subplot(235);stem(x,count);title('线性变换,k=-1,b=0');
结果:
![](images/24.jpg)
![](images/25.jpg)
1.2. 使用对数变换
f = imread('../pic/1.jpg');
f = rgb2gray(f);
figure;
subplot(121),imshow(f),subplot(122),imhist(f),axis tight
title('原图像')
g = im2uint8(mat2gray(2*log(1 + double(f))));
figure;
subplot(121),imshow(g),subplot(122),imhist(g),axis tight
title('使用对数变换减小动态范围')
axis([0 255 0 4000])
结果:
![](images/26.jpg)
![](images/27.jpg)
1.3. 灰度的伽马变换
f =imread('../pic/lema.png');
figure;
imshow(f);
title('原图像');
g1 = imadjust(f,[0,1],[1,0]);
figure;
imshow(g1);
title('灰度反转图像');
g2 = imadjust(f,[0.5,0.75],[0,1]);
figure;
imshow(g2);title('感兴趣的亮度带图像');
g3 = imadjust(f,[],[],2);
figure;
imshow(g3);title('压缩灰度级的低端并扩展灰度级的高端');
![](images/28.jpg)
1.4. 直方图
- 直方图归一化
f = imread('../pic/2.jpg');
f= rgb2gray(f);
[m,n] = size(f);
[counts,x] = imhist(f,32);
counts = counts/m/n;
figure;stem(x,counts);
figure;imhist(f,32);
![](images/30.jpg)
- 图像均衡化
f = imread('../pic/lema.png');
f = rgb2gray(f);
figure;
subplot(121),imshow(f),title('原图');subplot(122),imhist(f);
ylim('auto')
g = histeq(f,256);
figure,subplot(121),imshow(g),title('直方图均衡化(256)');subplot(122),imhist(g);
ylim('auto')
g = histeq(f,128);
figure,subplot(121),imshow(g),title('直方图均衡化(128)');subplot(122),imhist(g);
ylim('auto')
g = histeq(f);
figure,subplot(121),imshow(g),title('直方图均衡化(64默认)');subplot(122),imhist(g);
ylim('auto')
g = histeq(f,8);
figure,subplot(121),imshow(g),title('直方图均衡化(8)');subplot(122),imhist(g);
ylim('auto')
![](images/29.jpg)
1.5. 形态学变换
I=imread('../pic/2.jpg');
se=translate(strel(1),[180 190]);
B=imdilate(I,se);
figure;subplot(1,2,1),subimage(I);title('原图像');
subplot(1,2,2),subimage(B);title('平移后图像');
![](images/31.jpg)
I=imread('../pic/2.jpg');
[w,h,c]=size(I);
tform = maketform('affine',[1,0,0;0,-1,0;0,h,1]);
I2 = imtransform(I,tform,'nearest');
tform= maketform('affine',[-1,0,0;0,1,0;w,0,1])
I3 = imtransform(I,tform,'nearest');
subplot(131);subimage(I);title('原图像');
subplot(132);subimage(I2);title('垂直翻转图像');
subplot(133);subimage(I3);title('水平翻转图像');
![](images/32.jpg)
- 图像转置
f = imread('../pic/1.jpg');
tform = maketform('affine',[0 1 0; 1 0 0;0 0 1]);
f1 = imtransform(f,tform,'nearest');
subplot(121);subimage(f);title('原图像');
subplot(122);subimage(f1);title('装置后的图像');
![](images/33.jpg)
- 图像中心旋转
f = imread('../pic/1.jpg');
f1 = imrotate(f,30,'nearest','crop');
subplot(121);subimage(f);title('原图像');
subplot(122);subimage(f1);title('旋转30度后的图像');
![](images/34.jpg)