Matlab图像做图像空域滤波及边缘检测。
题目示例1
- 对一幅给定的图像加噪后分别用邻域平均和中值滤波器滤波之,并通过改变加噪程度,判断两种方法的差异及处理效果。
(注:图像加噪函数imnoise(p1,‘salt & pepper’,p2)使用方法请查询HELP)
这里的我们以下图为例演示。
题目示例1代码
x1=imread('lena256.bmp');
subplot(2,2,1)
imshow(x1)
title('原图')
x2=imnoise(x1,'salt & pepper',0.05);
subplot(2,2,2)
imshow(x2)
title('原图加噪后')
x2=double(x2);
[L1,L2]=size(x1);
for i=1:L1
for j=1:L2
% if(x2(i,j))
if(i==1&j==1)
x3(i,j)=round((x2(i,j)+x2(i+1,j)+x2(i,j+1))/3);
elseif(i==1&j==256)
x3(i,j)=round((x2(i,j)+x2(i+1,j)+x2(i,j-1))/3);
elseif(i==256&j==1)
x3(i,j)=round((x2(i,j)+x2(i-1,j)+x2(i,j+1))/3);
elseif(i==256&j==256)
x3(i,j)=round((x2(i,j)+x2(i-1,j)+x2(i,j-1))/3);
elseif(i==1)
x3(i,j)=round((x2(i,j)+x2(i+1,j)+x2(i,j+1)+x2(i,j-1))/4);
elseif(i==256)
x3(i,j)=round((x2(i,j)+x2(i-1,j)+x2(i,j+1)+x2(i,j-1))/4);
elseif(j==1)
x3(i,j)=round((x2(i,j)+x2(i+1,j)+x2(i,j+1)+x2(i-1,j))/4);
elseif(j==256)
x3(i,j)=round((x2(i,j)+x2(i+1,j)+x2(i,j-1)+x2(i-1,j))/4);
else
x3(i,j)=round((x2(i,j)+x2(i-1,j)+x2(i,j+1)+x2(i,j-1)+x2(i+1,j))/5);
end
end
end
subplot(2,2,3)
imshow(uint8(x3))
title('邻域平均法')
for i=1:L1
for j=1:L2
if(i==1&j==1)
ord=sort([x2(i,j),x2(i+1,j),x2(i,j+1),x2(i+1,j+1)]);
x4(i,j)=ord(2);
elseif(i==1&j==256)
ord=sort([x2(i,j),x2(i+1,j),x2(i,j-1),x2(i+1,j-1)]);
x4(i,j)=ord(2);
elseif(i==256&j==1)
ord=sort([x2(i,j),x2(i-1,j),x2(i,j+1),x2(i-1,j+1)]);
x4(i,j)=ord(2);
elseif(i==256&j==256)
ord=sort([x2(i,j),x2(i-1,j),x2(i,j-1),x2(i-1,j-1)]);
x4(i,j)=ord(2);
elseif(i==1)
ord=sort([x2(i,j),x2(i+1,j),x2(i,j+1),x2(i+1,j+1),x2(i,j-1),x2(i+1,j-1)]);
x4(i,j)=ord(3);
elseif(i==256)
ord=sort([x2(i,j),x2(i-1,j),x2(i,j+1),x2(i-1,j+1),x2(i,j-1),x2(i-1,j-1)]);
x4(i,j)=ord(3);
elseif(j==1)
ord=sort([x2(i,j),x2(i+1,j),x2(i-1,j),x2(i+1,j+1),x2(i-1,j+1),x2(i,j+1)]);
x4(i,j)=ord(3);
elseif(j==256)
ord=sort([x2(i,j),x2(i+1,j),x2(i-1,j),x2(i+1,j-1),x2(i-1,j-1),x2(i,j-1)]);
x4(i,j)=ord(3);
else
ord=sort([x2(i,j),x2(i+1,j),x2(i-1,j),x2(i+1,j+1),x2(i-1,j+1),x2(i,j+1),x2(i+1,j-1),x2(i,j-1),x2(i-1,j-1)]);
x4(i,j)=ord(5);
end
end
end
%x4=medfilt2(x2,[3,3]);
%x4=filter2(fspecial('average',3),x2);
subplot(2,2,4)
imshow(uint8(x4))
title('中值滤波法')
运行结果为
题目示例2
- 寻找或设计一幅适合分割技术应用的图像
1)分别利用Priwitt及Sobel的两个微分算子进行边缘检测; 2) 比较Priwitt及Sobel各自两个微分算子边缘检测结果的差异; 3)比较Priwitt与Sobel微分算子边缘检测结果的差异。
题目示例2代码
p1=[1 1 1;
0 0 0;
-1 -1 -1]/3;
p2=[1 0 -1;
1 0 -1;
1 0 -1;]/3;
s1=[1 2 1;
0 0 0;
-1 -2 -1]/4;
s2=[1 0 -1;
2 0 -2;
1 0 -1]/4;
x1=imread('lena256.bmp');
[L1,L2]=size(x1);
x1=double(x1);
for i=1:L1
for j=1:L2
if(i==1||i==256||j==1||j==256)
xp1(i,j)=x1(i,j);
xp2(i,j)=x1(i,j);
xs1(i,j)=x1(i,j);
xs2(i,j)=x1(i,j);
else
window=[x1(i-1,j-1),x1(i-1,j),x1(i-1,j+1);
x1(i,j-1),x1(i,j),x1(i,j+1);
x1(i+1,j-1),x1(i+1,j),x1(i+1,j+1);];
xp1(i,j)=sum(sum(window.*p1));
xp2(i,j)=sum(sum(window.*p2));
xs1(i,j)=sum(sum(window.*s1));
xs2(i,j)=sum(sum(window.*s2));
end
end
end
subplot(2,3,1)
imshow(uint8(x1))
title('原图')
subplot(2,3,2)
imshow(uint8(abs(xp1)))
title('Priwitt提取水平边缘')
subplot(2,3,3)
imshow(uint8(abs(xp2)))
title('Priwitt提取竖直边缘')
subplot(2,3,4)
imshow(uint8(x1))
title('原图')
subplot(2,3,5)
imshow(uint8(abs(xs1)))
title('Sobel提取水平边缘')
subplot(2,3,6)
imshow(uint8(abs(xs2)))
title('Sobel提取竖直边缘')
运行结果为
|