%之前写的一个程序,这里面用Sobel算子和Prewitt算子的部分就是对图像锐化得到边缘的了。也可以直接用matlab自带的函数S = edge(I, 'sobel')进行锐化。
clcclose all
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%读入图像
f=imread('Pictures/4_m03.tif')
%f=imread('Pictures/5_m01.tif')
%f=imread('Pictures/10_m02.tif')
%f=imread('Pictures/22_m03.tif')
f=imresize(f,0.25)
f=im2double(f)
[m,n]=size(f)
subplot(2,3,1),imshow(f)
title('原始图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%生成高斯平滑滤波模板
hg=zeros(3,3) %设定模板大小3*3
delta=0.5
for x=1:1:3
for y=1:1:3
u=x-2
v=y-2
谨带 hg(x,y)=exp(-(u^2+v^2)/(2*pi*delta^2))
end
end
h=hg/sum(hg(:))
%高斯滤波
ftemp=zeros(m,n)
rowhigh=m-1
colhigh=n-1
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1) f(x,y-1) f(x,y) f(x,y+1)f(x+1,y-1) f(x+1,y) f(x+1,y+1)]
竖迹 A=h.*mod
ftemp(x,y)=sum(A(:))
end
end
f=ftemp
subplot(2,3,4),imshow(f)
title('高斯滤波后的图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用Sobel算子进行边缘检测
sx=[-1 0 1-2 0 2-1 0 1]
sy=[-1 -2 -10 0 01 2 1]
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1) 祥纤芦f(x,y-1) f(x,y) f(x,y+1)f(x+1,y-1) f(x+1,y) f(x+1,y+1)]
fsx=sx.*mod
fsy=sy.*mod
ftemp(x,y)=max((abs(sum(fsx(:)))),(abs(sum(fsy(:)))))
end
end
fs=im2uint8(ftemp)
subplot(2,3,2),imshow(fs)
title('Sobel算子进行边缘检测的原始图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%阈值分割
TH1=140 %设定阈值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH1)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200
elseif(fs(x,y)>=TH1)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200
else fs(x,y)=50
end
end
end
subplot(2,3,5),imshow(fs)
title('Sobel算子边缘检测并细化后的图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用Prewitt算子进行边缘检测
sx=[1 0 -11 0 -11 0 -1]
sy=[-1 -1 -10 0 01 1 1]
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1) f(x,y-1) f(x,y) f(x,y+1)f(x+1,y-1) f(x+1,y) f(x+1,y+1)]
fsx=sx.*mod
fsy=sy.*mod
ftemp(x,y)=max((abs(sum(fsx(:)))),(abs(sum(fsy(:)))))
end
end
fs=im2uint8(ftemp)
subplot(2,3,3),imshow(fs)
title('Prewitt算子进行边缘检测的原始图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%阈值分割
TH1=100 %设定阈值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH1)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200
elseif(fs(x,y)>=TH1)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200
else fs(x,y)=50
end
end
end
subplot(2,3,6),imshow(fs)
title('Prewitt算子边缘检测并细化后的图像')
%%%目测大岩含你的是拉普拉斯算子 下面的程序也是一滚笑样的 你试试 图片改成你的图片A=imread('lena.bmp')
figure(1)
subplot(1,2,1)
imshow(A)
title('原图')
I=double(A)
h=[-1 -1 -1-1 9 -1-1 -1 -1]
J=conv2(I,h,'same')
K=uint8(J)
subplot(1,2,2)
imshow(K)
title('使用拉普拉斯算子锐化枣陪处理后的图')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)