首页 > 代码库 > 《数字图像处理原理与实践(MATLAB版)》一书之代码Part3
《数字图像处理原理与实践(MATLAB版)》一书之代码Part3
本文系《数字图像处理原理与实践(MATLAB版)》一书之代码系列的Part3(P81~135),代码执行结果请参见原书配图,建议下载代码前阅读下文:
关于《数字图像处理原理与实践(MATLAB版)》一书代码发布的说明
http://blog.csdn.net/baimafujinji/article/details/40987807
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 = http://www.mamicode.com/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);
(代码发布未完,请待后续...)
《数字图像处理原理与实践(MATLAB版)》一书之代码Part3
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。