第二章 灰度变换与空间滤波
例2.1 使用函数imadjust
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\1.tif');
g1 = imadjust(f,[0 1],[1 0]);
g = imcomplement(f);
g2 = imadjust(f,[0.5 0.75],[0 1]);
g3 = imadjust(f,[ ],[ ],2);
g4 = imadjust(f,stretchlim(f),[ ]);
g5 = imadjust(f,stretchlim(f),[1 0]);
figure,
subplot(171),imshow(f);
subplot(172),imshow(g1);
subplot(173),imshow(g);
subplot(174),imshow(g2);
subplot(175);imshow(g3);
subplot(176);imshow(g4);
subplot(177);imshow(g5);
例2.2 利用对数变换减小动态范围
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\2.tif');
g = im2uint8(mat2gray(log(1 + double(f))));
figure,
subplot(121),imshow(f);
subplot(122),imshow(g);
例2.3 函数intrans说明
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\0.tif');
g = intrans(f,'stretch',mean2(tofloat(f)),0.9);
figure,
subplot(121),imshow(f);
subplot(122),imshow(g);
例2.4 计算并绘制图像直方图
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\1.tif');
g = imhist(f);
figure,imshow(g);
h = imhist(f,25);
horz = linspace(0,255,25);
bar(horz,h);
axis([0 255 0 60000]);
set(gca,'xtick',0:50:255);
set(gca,'ytick',0:20000:60000);
h1 = imhist(f,25);
horz = linspace(0,255,25);
stem(horz,h,'fill');
axis([0 255 0 60000]);
set(gca,'xtick',0:50:255);
set(gca,'ytick',0:20000:60000);
hc = imhist(f);
plot(hc);
axis([0 255 0 15000]);
set(gca,'xtick',0:50:255);
set(gca,'ytick',0:2000:15000);
例2.5 直方图
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\9.tif');
figure,subplot(141),imshow(f);
subplot(142),imhist(f)
ylim('auto');
g = histeq(f,256);
subplot(143),imshow(g);
subplot(144),imhist(g);
ylim('auto');
hnorm = imhist(f)./numel(f);
cdf = cumsum(hnorm);
x = linspace(0,1,256);
plot(x,cdf);
axis([0 1 0 1]);
set(gca,'xtick',0:.2:1);
set(gca,'ytick',0:.2:1);
xlabel('Input intensity values','fontsize',9);
ylabel('Output intensity value','fontsize',9);
2.6 直方图匹配
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\10.tif');
figure,subplot(141),imshow(f);
subplot(142),imhist(f)
ylim('auto');
g = histeq(f,256);
subplot(143),imshow(g);
subplot(144),imhist(g);
ylim('auto');
例2.7 函数adapthisteq的使用
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\10.tif');
g1 = adapthisteq(f);
g2 = adapthisteq(f,'NumTiles',[25 25]);
g3 = adapthisteq(f,'NumTiles',[25 25],'ClipLimit',0.05);
figure,subplot(141),imshow(f);
subplot(142),imshow(g1);
subplot(143),imshow(g2);
subplot(144),imshow(g3);
2.8 函数imfilter的应用
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\11.tif');
w = ones(31);
gd = imfilter(f,w);
gr = imfilter(f,w,'replicate');
gs = imfilter(f,w,'symmetric');
gc = imfilter(f,w,'circular');
f8 = im2uint8(f);
g8r = imfilter(f8,w,'replicate');
figure,subplot(171),imshow(f);
subplot(172),imshow(gd,[ ]);
subplot(173),imshow(gr,[ ]);
subplot(174),imshow(gs,[ ]);
subplot(175),imshow(gc,[ ]);
subplot(176),imshow(g8r,[ ]);
2.9 使用函数cofilt实现非线性空间滤波
matlab实现:
function v = gmean(A)
% 这里的 A 代表的是 colfilt 生成的一个 mn x MN 的矩阵;
mn = size(A, 1);
v = prod(A, 1).^(1/mn);
f = padarray(f,[m n],'replicate');
% 为了消减边界效应,使用padarray中的’replicate’选项来填充输入图像;
g = colfilt(f,[m n],'sliding',@gmean);
% 调用colfilt
>> a = [1 2; 3 4]
a =
1 2
3 4
% 若 a 是一个向量返回元素的乘积,若 A 是一个矩阵则 prod(A)将列作为向量处理,并返回每列的积
>> b = prod(a)
b =
3 8
% prod(a,dim) 计算A中有dim指定方向的乘积,dim为1 代表返回行计算列
>> b = prod(a,1)
b =
3 8
% dim 为 2 代表返回列,计算行
>> b = prod(a,2)
b =
2
12
>> a = [1 2;3 4;5 6]
a =
1 2
3 4
5 6
% size(a) 返回一个行向量,包括每一维的长度
>> size(a)
ans =
3 2
% size(a,dim) dim为1 返回有多少行,dim 为 2 返回有多少列
>> size(a,1)
ans =
3
>> size(a,2)
ans =
2
2.10 使用函数imfilter实现拉普拉斯滤波器:
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\12.tif');
w = fspecial('laplacian',0);
g1 = imfilter(f,w,'replicate');
f2 = tofloat(f);
g2 = imfilter(f2,w,'replicate');
g = f2 - g2;
figure,subplot(141),imshow(f);
subplot(142),imshow(g1,[ ]);
subplot(143),imshow(g2,[ ]);
subplot(144),imshow(g);
2.11 手工指定滤波器及增强技术的比较:
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\12.tif');
w4 = fspecial('laplacian',0);
w8 = [1 1 1;1 -8 1;1 1 1 ];
f = tofloat(f);
g4 = f - imfilter(f ,w4,'replicate');
g8 = f - imfilter(f,w8,'replicate');
figure,subplot(131),imshow(f);
subplot(132),imshow(g4);
subplot(133),imshow(g8);
2.12 利用函数medfilt2进行中值滤波:
matlab实现:
f = imread('E:\图像处理\冈萨雷斯《数字图像处理》(MATLAB版)电子版、图源、代码\DIP3E_Original_Images_CH03\13.tif');
fn = imnoise(f,'salt & pepper',0.2);
gm = medfilt2(fn);
gms = medfilt2(fn,'symmetric');
figure,subplot(141),imshow(f);
subplot(142),imshow(fn);
subplot(143),imshow(gm);
subplot(144),imshow(gms);