您的位置:首页 > 其它

基于神经模糊算法的边缘检测算法

2014-11-17 10:36 381 查看
本文参照赵小川编著的《MATLAB图像处理——能力提高与应用案例》,个人觉得该算法具有较好的复杂度,且效果并不比Canny优越,但是可以学习一下。

function edge_detection_based_on_neural_network
%选择图像
[file path] = uigetfile('*.bmp;*.jpg;*png','选择一副图片');
if file==0
warndlg('用户必须选择一个输入的图片');
else
a=imread(fullfile(path,file));
end
figure;
subplot(131),imshow(a,[]);
%方向检测
I = double(a);
[R C P]=size(a);
for i=2:R-1
for j=2:C-1
D1(i-1,j-1)=abs(I(i-1,j-1)-I(i,j))+abs(I(i+1,j+1)-I(i,j));
D2(i-1,j-1)=abs(I(i-1,j)-I(i,j))+abs(I(i+1,j)-I(i,j));
D3(i-1,j-1)=abs(I(i-1,j+1)-I(i,j))+abs(I(i+1,j-1)-I(i,j));
D4(i-1,j-1)=abs(I(i,j-1)-I(i,j))+abs(I(i,j+1)-I(i,j));
end
end
%边缘分类
[R C P] = size(D1);
for i=1:R
for j=1:C
if((D1(i,j)<=35)&&(D2(i,j)<=35)&&(D3(i,j)<=35)&&(D4(i,j)<=35))
New_im(i,j)=0;%BACKGROUND CLASS
elseif ((D1(i,j)<=35)&&(D2(i,j)>35)&&(D3(i,j)>5)&&(D4(i,j)>35))
New_im(i,j)=1;
elseif ((D1(i,j)>35)&&(D2(i,j)<=35)&&(D3(i,j)>5)&&(D4(i,j)>35))
New_im(i,j)=2;
elseif ((D1(i,j)>35)&&(D2(i,j)>35)&&(D3(i,j)<=5)&&(D4(i,j)>35))
New_im(i,j)=3;
elseif ((D1(i,j)>35)&&(D2(i,j)>35)&&(D3(i,j)>5)&&(D4(i,j)<=35))
New_im(i,j)=4;
elseif ((D1(i,j)>35)&&(D2(i,j)>35)&&(D3(i,j)>5)&&(D4(i,j)>35))
New_im(i,j)=5;%SPECKLE CLASS
end
end
end
%自组织
C_0 = [5 5 5 5];
C_1 = [5 35 35 35];
C_2 = [35 5 35 35];
C_3 = [35 35 5 35];
C_4 = [35 35 35 5];
C_5 = [35 35 35 35];
[r c]=size(D1);
n1 = 1;
for i=1:r
for j=1:c
X = [D1(i,j) D2(i,j) D3(i,j) D4(i,j)];
U_0(i,j) = 1-(sum(abs(X-C_0)))/451;
U_1(i,j) = 1-(sum(abs(X-C_1)))/451;
U_2(i,j) = 1-(sum(abs(X-C_2)))/451;
U_3(i,j) = 1-(sum(abs(X-C_3)))/451;
U_4(i,j) = 1-(sum(abs(X-C_4)))/451;
U_5(i,j) = 1-(sum(abs(X-C_5)))/451;
[Value ind]=sort([U_0(i,j) U_1(i,j) U_2(i,j) U_3(i,j) U_4(i,j) U_5(i,j)],'descend');
if ind(1)==1
New_edgeim(i,j)=0;
else
New_edgeim(i,j)=1;
end
end
end

subplot(132),imshow(New_edgeim,[]);

New_im = New_edgeim;
[r c]=size(New_im);
for i=2:r-1
for j=2:c-1
switch New_im(i,j)
case 0
New_edge(i-1,j-1)=0;
case 1
if(D3(i,j)>=D3(i+1,j-1))&&(D3(i,j)>D3(i-1,j+1))
New_edge(i-1,j-1)=1;
else
New_edge(i-1,j-1)=0;
end
case 2
if(D4(i,j)>=D4(i+1,j-1))&&(D4(i,j)>=D4(i-1,j+1))
New_edge(i-1,j-1)=1;
else
New_edge(i-1,j-1)=0;
end
case 3
if(D1(i,j)>=D1(i+1,j-1)&&(D1(i,j)>=D1(i-1,j+1)))
New_edge(i-1,j-1)=1;
else
New_edge(i-1,j-1)=0;
end
case 4
if(D2(i,j)>=D2(i+1,j-1)&&(D2(i,j)>=D2(i-1,j+1)))
New_edge(i-1,j-1)=1;
else
New_edge(i-1,j-1)=0;
end
case 5
New_edge(i-1,j-1)=1;
end
end
end
subplot(133),imshow(New_edge);

程序结果演示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息