数字图像处理 - Matlab - 实验笔记二

第三章 灰度变换与空间滤波

1. 仿真实现一种灰度变换增强图像

程序代码:

% 灰度增强
clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=double(rgb2gray(X));
% 获取图像大小
[width,height]=size(X0);
% 显示原灰度图
subplot(2,3,1)
imshow(uint8(X0));
title('原灰度图')
% 预分配内存
X1=zeros(width,height);
X2=zeros(width,height);
X3=zeros(width,height);
X4=zeros(width,height);
% 线性变换
a=0.5;
b=0;
for i=1:width
    for j=1:height
        X1(i,j)=X0(i,j)*a+b;
    end
end
subplot(2,3,2)
imshow(uint8(X1));
title('线性变换')
% 对数形式变换
c1=35;
for i=1:width
    for j=1:height
        X2(i,j)=c1*log(X0(i,j)+1);
    end
end
subplot(2,3,3)
imshow(uint8(X2));
title('对数形式变换')
% 指数形式变换
c2=0.1;
r=1.5;
for i=1:width
    for j=1:height
        X3(i,j)=c2*(X0(i,j)).^r;
    end
end
subplot(2,3,5)
imshow(uint8(X3));
title('指数形式变换')
% 分段线性变换
k1=0.5;
b1=0;
k2=167.5/80;
b2=-191.25;
k3=0.5;
b3=127.5;
for i=1:width
    for j=1:height
        if X0(i,j)<=120
            X4(i,j)=k1*X0(i,j)+b1;
        elseif X0(i,j)<=200
            X4(i,j)=k2*X0(i,j)+b2;
        else
            X4(i,j)=k3*X0(i,j)+b3;
        end
    end
end
subplot(2,3,6)
imshow(uint8(X4));
title('分段线性变换')

输出结果:
enhance

2. 仿真实现一幅图像的直方图

程序代码:

function hist = gethistogram(X)
%gethistogram
%获取灰度图的直方图数据
% 获取图像大小
[width,height]=size(X);
% 获取直方图数据
hist=zeros(1,256);
for i=1:width
    for j=1:height
        hist(X(i,j)+1)=hist(X(i,j)+1)+1;
    end
end
end
% 直方图
clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=rgb2gray(X);
% 显示原灰度图
subplot(1,2,1)
imshow(uint8(X0));
title('灰度图')
% 获取直方图
hist=gethistogram(X0);
subplot(1,2,2)
stem((0:255),hist)
title('直方图')
grid on

输出结果:
histogram

3. 仿真实现图像的直方图均衡化处理

程序代码:

function cumhist = getCumulativehistogram(X)
%getCumulativehistogram
%获取灰度图的累计直方图数据
% 获取直方图数据
hist=gethistogram(X);
% 计算累计直方图
cumhist=zeros(1,256);
cumhist(1)=hist(1);
for n=2:256
    cumhist(n)=cumhist(n-1)+hist(n);
end
end
clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=rgb2gray(X);
% 获取图像大小
[width,height]=size(X0);
% 显示原灰度图
subplot(2,2,1)
imshow(uint8(X0));
title('原灰度图')
% 显示原图的直方图
hist=gethistogram(X0);
subplot(2,2,2)
stem((0:255),hist)
title('原直方图')
grid on
axis([0 255 0 2100])
% 直方图均衡化
X1=zeros(width,height);
cumhist=getCumulativehistogram(X0);
hisequal=zeros(1,256);
for n=1:256
    hisequal(n)=round(cumhist(n)/65536*255);
end
for i=1:width
    for j=1:height
        X1(i,j)=hisequal(X0(i,j)+1);
    end
end
% 显示直方图均衡化后的灰度图
subplot(2,2,3)
imshow(uint8(X1));
title('直方图均衡化后的灰度图')
% 显示直方图均衡化后的直方图
hist1=gethistogram(X1);
subplot(2,2,4)
stem((0:255),hist1)
title('直方图均衡化后的直方图')
grid on
axis([0 255 0 2100])

输出结果:
histequal

4. 仿真实现多幅图像平均去高斯白噪声

程序代码:

clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=double(rgb2gray(X));
% 获取图像大小
[width,height]=size(X0);
% 显示原灰度图
subplot(1,3,1)
imshow(uint8(X0));
title('原灰度图')
% 加高斯白噪声
wgnoise=wgn(width,height,25);
X1=X0+wgnoise;
% 显示加噪灰度图
subplot(1,3,2)
imshow(uint8(X1));
title('其中一张加噪灰度图')
% 多幅图像平均
Y=X1;
for i=1:29
   Xn=X0+wgn(width,height,25);
   Y=Y+Xn;
end
Y=Y/30;
% 显示多幅图像平均后的图像
subplot(1,3,3)
imshow(uint8(Y));
title('多幅平均图像')

输出结果:
multisum

5. 仿真实现均值滤波

自己写了个滤波运算的函数练了练手,实际可以用matlab原有的B=imfilter(A,h),更方便。

function output = imagefilter(input,kernel)
%imagefilter
%二维图像滤波运算--零延拓
% 获取输入图像和卷积核的尺寸
[width,height]=size(input);
[m,n]=size(kernel);
% 零延拓
a=ceil((m-1)/2);
b=ceil((n-1)/2);
X=zeros(width+a*2,height+b*2);
for i=1:width
    for j=1:height
        X(i+a,j+b)=input(i,j);
    end
end
% 滤波运算
output=zeros(width,height);
for i=1:width
    for j=1:height
        for u=0:(2*a)
            for v=0:(2*b)
                output(i,j)=output(i,j)+kernel(1+u,1+v)*X(i+u,j+v);
            end
        end
    end
end
end

程序代码:

clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=double(rgb2gray(X));
% 获取图像大小
[width,height]=size(X0);
% 显示原灰度图
subplot(1,3,1)
imshow(uint8(X0));
title('原灰度图')
% 加高斯白噪声
wgnoise=wgn(width,height,25);
X1=X0+wgnoise;
% 显示加噪灰度图
subplot(1,3,2)
imshow(uint8(X1));
title('加噪灰度图')
% 均值滤波
filter=ones(3,3)/9;
Y=imagefilter(X1,filter);
% 显示均值滤波去噪灰度图
subplot(1,3,3)
imshow(uint8(Y));
title('均值滤波去噪灰度图')

输出结果:
meanfilter

6. 仿真实现中值滤波去除脉冲噪声

程序代码:

clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=double(rgb2gray(X));
% 获取图像大小
[width,height]=size(X0);
% 显示原灰度图
subplot(1,3,1)
imshow(uint8(X0));
title('原灰度图')
% 加脉冲噪声
X1=X0;
for n=1:1000
    X1(ceil(rand*256),ceil(rand*256))=0;
end
for n=1:1000
    X1(ceil(rand*256),ceil(rand*256))=255;
end
% 显示加噪灰度图
subplot(1,3,2)
imshow(uint8(X1));
title('加脉冲噪声灰度图')
% 边界重复延拓
X2=zeros(width+2,height+2);
for i=1:width
    for j=1:height
        X2(i+1,j+1)=X1(i,j);
    end
end
for i=1:width
   X2(i+1,1)=X2(i,2);
   X2(i+1,height+2)=X2(i,height+1);
end
for j=1:height+2
   X2(1,j)=X2(2,j);
   X2(width+2,j)=X2(width+1,j);
end
% 中值滤波
Y=zeros(width,height);
for i=1:width
    for j=1:height
        S=[X2(i,j:(j+2)),X2(i+1,j:(j+2)),X2(i+2,j:(j+2))];
        S=sort(S);
        Y(i,j)=S(5);
    end
end
% 显示中值滤波去噪灰度图
subplot(1,3,3)
imshow(uint8(Y));
title('中值滤波去噪灰度图')

输出结果:
median

7. 分别用Laplacian算子和Sobel算子实现图像的锐化增强,并对比实验结果

程序代码:

clear
X=imread('pic.jpg');
% 把原图转为灰度图
X0=double(rgb2gray(X));
% 显示原灰度图
subplot(1,3,1)
imshow(uint8(X0));
title('原灰度图')
% 显示Laplacian算子锐化增强图
La=[0 -1 0;-1 5 -1;0 -1 0];
X1=imfilter(X0,La);
subplot(1,3,2)
imshow(uint8(X1));
title('Laplacian算子锐化增强图')
% 显示Laplacian算子锐化增强图
So=[-1 -2 -1;0 1 0;1 2 1];
% So=[-1 0 1;-2 1 2;-1 0 1];
X2=imfilter(X0,So);
subplot(1,3,3)
imshow(uint8(X2));
title('Sobel算子锐化增强图')

输出结果:
laso

发布了3 篇原创文章 · 获赞 0 · 访问量 93

猜你喜欢

转载自blog.csdn.net/qq_41016449/article/details/105242254