Matlab边缘检测问题

Matlab边缘检测问题,第1张

用mesh语句似乎可以,具体也不了解你的情况,感觉怪怪的,发一段我以前些的程序,用罗伯特算子写的,把算子一改就是sobel了。两种边缘检测近似算法奉上:

clc

close all

clear all

%%%生成高斯平滑滤波模板%%%

%%%%%%%%%%%%%%%%%%%%%%%%%

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(:))

%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%读入图像%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%

f = imread('1111.tif')% 读入图像文件

f=rgb2gray(im2double(f))

imshow(f)

title('原始图像')

[m,n]=size(f)

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

figure,imshow(f)

title('通过高斯滤波器后的图像')

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% %%%利用roberts算子进行边缘检测%%%

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sx=[-1 -2 -10 0 01 2 1]

sy=[-1 0 1-2 0 2-1 0 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)=sqrt((sum(fsx(:)))^2+(sum(fsy(:)))^2)

end

end

fr=im2uint8(ftemp)

figure,imshow(fr)

title('用roberts算子边缘检测的原始图像')

%%%域值分割%%%

TH1=60 %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fr(x,y)>=TH1)&((fr(x,y-1) <= fr(x,y)) &(fr(x,y) >fr(x,y+1)) )

fr(x,y)=200

elseif(fr(x,y)>=TH1)&( (fr(x-1,y) <=fr(x,y)) &(fr(x,y) >fr(x+1,y)))

fr(x,y)=200

else fr(x,y)=50

end

end

end

figure,imshow(fr)

title('用roberts算子边缘检测并细化后的图像')

%%%%%%%%%%%%%%%%%%%%%%%%%%

利用第一种近似算法进行边缘检测%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%3*3的sobel算子%%%%%%%%

sx=[-1 -2 -10 0 01 2 1]

sy=[-1 0 1-2 0 2-1 0 1]

%sx=[0 1 2-1 0 1-2 -1 0]

%sy=[-2 -1 0-1 0 10 1 2]

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)=abs(sum(fsx(:)))+abs(sum(fsy(:)))

end

end

fs=im2uint8(ftemp)

figure,imshow(fs)

title('用第一种近似算法进行边缘检测的原始图像')

%%%域值分割%%%

TH2=200 %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) &(fs(x,y) >fs(x,y+1)) )

fs(x,y)=200

elseif(fs(x,y)>=TH2)&( (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

figure,imshow(fs)

title('采用第一种近似算法进行边缘检测后的图像')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%利用第二种近似算法进行边缘检测%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%3*3的sobel算子%%%%%%%%

sx=[-1 -2 -10 0 01 2 1]

sy=[-1 0 1-2 0 2-1 0 1]

%sx=[0 1 2-1 0 1-2 -1 0]

%sy=[-2 -1 0-1 0 10 1 2]

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)

figure,imshow(fs)

title('用第二种近似算法进行边缘检测的原始图像')

%%%域值分割%%%

TH2=200 %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) &(fs(x,y) >fs(x,y+1)) )

fs(x,y)=200

elseif(fs(x,y)>=TH2)&( (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

figure,imshow(fs)

title('采用第二种近似算法进行边缘检测后的图像')

public class Rectangle{ private int width private int height public Rectangle(){ this.width = 10this.height = 10 } public Rectangle(int width, int height){this.width = width this.height = height }public int area(){ return width * height } //省略getter/setter }


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11454830.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存