本文系《数字图像处理原理与实践(MATLAB版)》(电子工业出版社)一书之代码系列的Part2(P43~P135),代码执行结果请参见原书配图。
----------------------------
P44
i = imread('theatre.jpg');
i = rgb2gray(i);
i = double(i);
out1 = log(1+i)/0.065;
out2 = log(1+i)/0.035;
out1(find(out1>255)) = 255;
out2(find(out2>255)) = 255;
out1 = uint8(out1);
out2 = uint8(out2);
subplot(221), imshow(out1), title('image, p = 0.065');
subplot(222), imhist(out1), title('histgram, p = 0.065');
subplot(223), imshow(out2), title('image, p = 0.035');
subplot(224), imhist(out2), title('histgram, p = 0.035');
P47
i = rgb2gray(imread('theatre.jpg'));
i = double(i);
y1 = 255*(i/255).^2.5;
y2 = 255*(i/255).^0.4;
y1 = uint8(y1);
y2 = uint8(y2);
subplot(221), imshow(y1), title('p =2.5');
subplot(222), imhist(y1), title('p =2.5');
subplot(223), imshow(y2), title('p =0.4');
subplot(224), imhist(y2), title('p =0.4');
P48
i = rgb2gray(imread('theatre.jpg'));
y1 = double(i);
y1 = 255*(y1/255).^2.5;
y2 = uint8(y1);
y3 = imadjust(y2, [ ], [ ], 0.4);
subplot(131), imshow(i), title('original image');
subplot(132), imshow(y2),title('power = 2.5');
subplot(133), imshow(y3),title('gamma = 0.4');
P49
i = imread('theatre.jpg');
i = rgb2gray(i);
i = double(i);
y1 = 1.5.^(i*0.070)-1;
y2 = 1.5.^(i*0.050)-1;
y1(find(y1>255)) = 255;
y2(find(y2>255)) = 255;
y1 = uint8(y1);
y2 = uint8(y2);
subplot(221), imshow(y1), title('c=0.070');
subplot(222), imhist(y1), title('c=0.070');
subplot(223), imshow(y2), title('c=0.050');
subplot(224), imhist(y2), title('c=0.050');
P52
i = imread('theatre.jpg');
i = rgb2gray(i);
L = imadjust(i,[ ],[50/255;150/255]);
J = imadjust(L,[50/255;150/255 ],[20/255;230/255]);
subplot(221), imshow(L), title('low contrast');
subplot(222), imhist(L), title('low contrast');
subplot(223), imshow(J), title('gray stretch');
subplot(224), imhist(J), title('gray stretch');
P54
i = rgb2gray(imread('theatre.jpg'));
LC = imadjust(i,[ ],[50/255;150/255]);
figure(1), subplot(221), imshow(LC);
title('low contrast');
figure(1),subplot(222), imhist(LC);
title('low contrast');
HE1 = histeq(LC);
figure(1), subplot(223), imshow(HE1);
title('histogram equalization');
figure(1),subplot(224), imhist(HE1);
title('histogram equalization');
P56
img = rgb2gray(imread('theatre.jpg'));
img_ref = rgb2gray(imread('rpic.jpg'));
[hgram, x] = imhist(img_ref);
J = histeq(img, hgram);
subplot(2,3,1), imshow(img), title('original image');
subplot(2,3,4), imhist(img), title('original image');
subplot(2,3,2), imshow(img_ref), title('reference image');
subplot(2,3,5), imhist(img_ref), title('reference image');
subplot(2,3,3), imshow(J), title('output image');
subplot(2,3,6), imhist(J), title('output image');
P64-1
I = imread('apostles.jpg');
I = double(I);
B = zeros(size(I));
H = size(I);
move_x = 100;
move_y = 150;
B(move_y + 1:H(1), move_x+1:H(2), 1:H(3))=...
I(1:H(1)-move_y, 1:H(2) - move_x, 1:H(3));
subplot(1,2,1),subimage(uint8(I))
title('原图像')
subplot(1,2,2),subimage(uint8(B))
title('平移变换');
P64-2
I = imread('apostles.jpg');
se=translate(strel(1),[150 100]);
B = imdilate(I,se);
figure;
subplot(1,2,1),subimage(I);
title('原图像');
subplot(1,2,2),subimage(B);
title('平移变换');
P66
I = imread('apostles.jpg');
[height, width, dim]=size(I);
%水平镜像变换
tform = maketform('affine',[-1 0 0;0 1 0; width 0 1]);
B=imtransform(I, tform, 'nearest');
%垂直镜像变换
tform2 = maketform('affine', [1 0 0; 0 -1 0; 0 height 1]);
C=imtransform(I, tform2, 'nearest');
subplot(1,3,1),imshow(I);
title('原图像');
subplot(1,3,2),imshow(B);
title('水平图像');
subplot(1,3,3),imshow(C);
title('垂直图像');
P67
A = imread('apostles.jpg');
A = double(A);
figure(1), imshow(uint8(A));
H = size(A);
figure(2),B(1:H(1),1:H(2),1:H(3))=A(H(1):-1:1,1:H(2),1:H(3));%垂直镜像
imshow(uint8(B));
figure(3),C(1:H(1),1:H(2),1:H(3))=A(1:H(1),H(2):-1:1,1:H(3));%水平镜像
imshow(uint8(C));
P69
I = imread('apostles.jpg');
tform = maketform('affine',[0 1 0; 1 0 0; 0 0 1]);%定义转置矩阵
B = imtransform(I, tform, 'nearest');
subplot(1,2,1),imshow(I)
title('原图像');
subplot(1,2,2),imshow(B)
title('转置图像');
P74
I = imread('C:\apostles.jpg');
A = imresize(I, 1.5, 'nearest');
B = imresize(I, 1.5, 'bilinear');
C = imresize(I, 1.5, 'bicubic');
subplot(2,2,1), imshow(I), title('original');
subplot(2,2,2), imshow(A), title('nearest');
subplot(2,2,3), imshow(B), title('bilinear');
subplot(2,2,4), imshow(C), title('bicubic');
P80
I = imread('apostles.jpg');
A = imrotate(I, 30, 'nearest');%旋转30度,最邻近插值
figure(1),imshow(A)
B = imrotate(I, 45, 'bilinear','loose');%旋转45度,二次线性插值
figure(2),imshow(B)
P92
i = imread('Hepburn.jpg');
%注意w和h1这两个模板是等价的
w = [1 1 1;1 1 1;1 1 1]/9;
h1 = fspecial('average', [3 3]);
h2 = fspecial('average', [5 5]);
h3 = fspecial('average', [7 7]);
%执行图像的简单平滑
g1 = imfilter(i, w, 'conv', 'replicate');
g2 = imfilter(i, h2, 'conv', 'replicate');
g3 = imfilter(i, h3, 'conv', 'replicate');
P98
i = imread('baboon.jpg');
h = fspecial('gaussian', 7, 2);
g = imfilter(i, h,'conv');
subplot(121), imshow(i), title('original image');
subplot(122), imshow(g), title('gaussian smooth');
P103
i = rgb2gray(imread('lena.jpg'));
i_noise = imnoise(i, 'salt & pepper');
w1 = [1 2 1; 2 4 2; 1 2 1]/16;
output1 = imfilter(i_noise, w1, 'conv', 'replicate');
w2 = [1 1 1; 1 1 1; 1 1 1]/9;
output2 = imfilter(i_noise, w2, 'conv', 'replicate');
output3 = medfilt2(i_noise, [3, 3]);
P106
function B = bfilter2(A,w,sigma)
% 针对灰度图像或彩色图像选择应用不同的处理函数
if size(A,3) == 1
B = bfltGray(A,w,sigma(1),sigma(2));
else
B = bfltColor(A,w,sigma(1),sigma(2));
end
% 对灰度图像进行双边滤波处理的函数
function B = bfltGray(A,w,sigma_d,sigma_r)
% 计算高斯模板
[X,Y] = meshgrid(-w:w,-w:w);
G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));
% 进行双边滤波
dim = size(A);
B = zeros(dim);
for i = 1:dim(1)
for j = 1:dim(2)
% 抽取一块局部区域,这与值域核的大小相对应
iMin = max(i-w,1);
iMax = min(i+w,dim(1));
jMin = max(j-w,1);
jMax = min(j+w,dim(2));
I = A(iMin:iMax,jMin:jMax);
% 计算值域核,也就是灰度值的权值模板
H = exp(-(I-A(i,j)).^2/(2*sigma_r^2));
% 计算双边滤波响应
F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
B(i,j) = sum(F(:).*I(:))/sum(F(:));
end
end
% 对彩色图像进行双边滤波处理的函数
function B = bfltColor(A,w,sigma_d,sigma_r)
% 将输入的RGB图像转换到CIE颜色空间中
if exist('applycform','file')
A = applycform(A,makecform('srgb2lab'));
else
A = colorspace('Lab<-RGB',A);
end
[X,Y] = meshgrid(-w:w,-w:w);
G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));
sigma_r = 100*sigma_r;
% 进行滤波处理
dim = size(A);
B = zeros(dim);
for i = 1:dim(1)
for j = 1:dim(2)
iMin = max(i-w,1);
iMax = min(i+w,dim(1));
jMin = max(j-w,1);
jMax = min(j+w,dim(2));
I = A(iMin:iMax,jMin:jMax,:);
dL = I(:,:,1)-A(i,j,1);
da = I(:,:,2)-A(i,j,2);
db = I(:,:,3)-A(i,j,3);
H = exp(-(dL.^2+da.^2+db.^2)/(2*sigma_r^2));
F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
norm_F = sum(F(:));
B(i,j,1) = sum(sum(F.*I(:,:,1)))/norm_F;
B(i,j,2) = sum(sum(F.*I(:,:,2)))/norm_F;
B(i,j,3) = sum(sum(F.*I(:,:,3)))/norm_F;
end
end
% 将滤波结果转换回RGB色彩空间
if exist('applycform','file')
B = applycform(B,makecform('lab2srgb'));
else
B = colorspace('RGB<-Lab',B);
end
P108
I = imread('cat.gif');
I = double(I)/255;
w = 5;
sigma = [3 0.1];
B = bfilter2(I,w,sigma);
P111
I = imread('cameraman.tif');
H = fspecial('unsharp');
sharpened = imfilter(I,H,'replicate');
subplot(121), imshow(I), title('Original Image')
subplot(122), imshow(sharpened); title('Sharpened Image')
P112
I = imread('cameraman.tif');
Laplace=[0 -1 0;-1 4 -1; 0 -1 0 ];
Data = double(I);
LaplaceImage=conv2(Data,Laplace,'same');
%上面这句也可以写作下面这种形式,作用是等同的
%LaplaceImage=imfilter(Data,Laplace,'conv','same');
subplot(1,2,1); imshow(uint8(LaplaceImage)); title('Laplace图像');
%原图像与拉普拉斯图像叠加
DataLap=imadd(Data,LaplaceImage);
subplot(1,2,2),imshow(uint8(DataLap));
title('锐化增强后的图像');
P124
I = imread('fruits.jpg');
SE = strel('rectangle',[10 10]);
I2 = imerode(I, SE);
figure(2),imshow(I2)
P128
I = imread('fruits.jpg');
SE = strel('rectangle',[10 10]);
I3 = imdilate(I, SE);
figure(3),imshow(I3)
P133
I = imread('character.jpg');
figure, imshow(I);
SE = strel('square',3);
Ie = imerode(I, SE);
I2 = I - Ie; %计算内边界
figure(2), imshow(I2);
Id = imdilate(I, SE);
I3 = Id - I; %计算外边界
figure(3), imshow(I3);
P134
I = imread('character.jpg');
SE = strel('square',3);
Ie = imerode(I, SE);
Iee = imerode(Ie, SE);
Id = imdilate(I, SE);
Idd = imdilate(Id, SE);
I1 = Ie - Iee;
I2 = Idd - Id;
I3 = I1 + I2;
figure(3), imshow(I3);
(代码发布未完,请待后续...)