二、空域滤波
1.滤波
clear;
close all;
img=rgb2gray(imread("1.png"));
figure;imshow(img);
img=double(imnoise(img,'salt & pepper',0.1));
figure,imshow(img,[]);
N=5;
h=fspecial('average',N);
I=filter2(h,img);
figure,imshow(I,[]);
I=medfilt2(img,[N N]);
figure,imshow(I,[]);
I=ordfilt2(img,N*N,true(N));
figure,imshow(I,[]);
I=ordfilt2(img,1,true(N));
figure,imshow(I,[]);
a=rgb2gray(imread('1.png'));
figure;
subplot(2,3,1);
imshow(a);
title('原图')
a1=double(imnoise(a,'salt & pepper',0.1));
subplot(2,3,2);
imshow(a1,[]);
title('含噪图像');
N=5;
b=fspecial('average',N);
b1=filter2(b,a1);
subplot(2,3,3);
imshow(b1,[]);
title('均值滤波');
c=medfilt2(a1,[N N]);
subplot(2,3,4);
imshow(c,[]);
title('中值滤波');
d=ordfilt2(a1,N*N,true(N));
subplot(2,3,5);
imshow(d,[]);
title('最大值滤波');
e=ordfilt2(a1,1,true(N));
subplot(2,3,6);
imshow(e,[]);
title('最小值滤波');
2.边缘算子
clear;
close all;
%0.原图
II=imread('橙子.png');
I=double(II);
figure,imshow(II)
title('原始图片');
IR=I(:,:,1);
IG=I(:,:,2);
IB=I(:,:,3);
%1.均值低通滤波
H=fspecial('average',5);
F1{1}=double(filter2(H,IR));
F2{1}=double(filter2(H,IG));
F3{1}=double(filter2(H,IB));
x_filtered1=cat(3,F1{1},F2{1},F3{1});
%2.gaussian低通滤波
H=fspecial('gaussian',7,3);
F1{2}=double(filter2(H,IR));
F2{2}=double(filter2(H,IG));
F3{2}=double(filter2(H,IB));
x_filtered2=cat(3,F1{2},F2{2},F3{2});
%3.增强图像=原图-均值低通滤波
F1{3}=2*I(:,:,1)-F1{1};
F2{3}=2*I(:,:,2)-F2{1};
F3{3}=2*I(:,:,3)-F3{1};
x_filtered3=cat(3,F1{3},F2{3},F3{3});
%4.增强图像=原图-高斯低通滤波
F1{4}=2*I(:,:,1)-F1{2};
F2{4}=2*I(:,:,2)-F2{2};
F3{4}=2*I(:,:,3)-F3{2};
x_filtered4=cat(3,F1{4},F2{4},F3{4});
%5.'prewitt'边缘算子增强
H=fspecial('prewitt');
F1{5}=uint8(I(:,:,1)+filter2(H,IR));
F2{5}=uint8(I(:,:,2)+filter2(H,IG));
F3{5}=uint8(I(:,:,3)+filter2(H,IB));
x_filtered5=cat(3,F1{5},F2{5},F3{5});
%6.'sobel'边缘算子增强
H=fspecial('sobel');
F1{6}=uint8(I(:,:,1)+filter2(H,IR));
F2{6}=uint8(I(:,:,2)+filter2(H,IG));
F3{6}=uint8(I(:,:,3)+filter2(H,IB));
x_filtered6=cat(3,F1{6},F2{6},F3{6});
%画图
figure
subplot(2,3,1);
imshow(uint8(x_filtered1),[]);
title('均值低通滤波');
subplot(2,3,2);
imshow(uint8(x_filtered2),[]);
title('gaussian低通滤波');
subplot(2,3,3);
imshow(uint8(x_filtered3),[]);
title('增强图像=原图-均值低通滤波');
subplot(2,3,4);
imshow(uint8(x_filtered4),[]);
title('增强图像=原图-高斯低通滤波');
subplot(2,3,5);
imshow(uint8(x_filtered5),[]);
title('prewitt边缘算子增强');
subplot(2,3,6);
imshow(uint8(x_filtered6),[]);
title('sobel边缘算子增强');
subtitle('滤波图像');