2.是J(K)=255,我运行了一次后出现白色加上淡黄色的原图像,你做的就是灰度级的处理,我建议你改为j(k)=0效果就是黑色的原图,但不是黑白二值图,是黑点构成的,我等级太低,没法给你发图,希望能帮到你!
这个网站有一些代码:http://www.matlabsky.com/?fromuid=4481你要的在http://www.matlabsky.com/viewthread.php?tid=248&highlight=%C8%F1%BB%AF的#9吧。
常用的一些图像处理Matlab源代码
都是一些简单的图像处理源代码,入门性的介绍
#1:数字图像矩阵数据的显示及其傅立叶变换
#2:二维离散余弦变换的图像压缩
#3:采用灰度变换的方法增强图像的对比度
#4:直方图均匀化
#5:模拟图像受高斯白噪声和椒盐噪声的影响
#6:采用二维中值滤波函数medfilt2对受椒盐噪声干扰的图像滤波
#7:采用MATLAB中的函数filter2对受噪声干扰的图像进行均值滤波
#8:图像的自适应魏纳滤波
#9:运用5种不同的梯度增强法进行图像锐化
#10:图像的高通滤波和掩模处理
#11:利用巴特沃斯(Butterworth)低通滤波器对受噪声干扰的图像进行平滑处理
%之前写的一个程序,这里面用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算子边缘检测并细化后的图像')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)