langde
2008-05-31, 11:33
:cry: 代码的功能是找到二值图象中灰度值=255的孤立点 并且将其灰度值设为0
大概的思路是:
先扫描到一点 然后以这个点作为起始点为基点 扫描其周围的8个点 当扫描到其周围8个点中有灰度值=255的点时 再以这个新点为基点,重复上面的操作。。。。一直这样进行下去
当其周围8个点中没有灰度值=255的点,则认为该点是孤立点,将其灰度值设为0
代码:
%main.m
clc
clear all
bw1=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 255 0 0 0 0 0 0 0 0 0 0 0
0 0 255 255 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 255 0 0 255 0 0 0 0 0 0 0 0 0 0 0
0 0 0 255 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
figure,imshow(bw1);
title('数据图象');
[m,n]=size(bw1);
%链码计算,长度小于某个值的边缘滤出
bianjie_length=0;
first_point=zeros(1,2); %起始点
temp1_im=zeros(m,n); %临时存放检测到的点
end_im=zeros(m,n); %最终检测到的点
biaoji=zeros(1,2,2*(m+n)); %用于避免循环的出现
for i=2:20
for j=2:20
if bw1(i,j)==255
first_point=[i,j];
temp1_im(first_point(1,1),first_point(1,2))=255;
bianjie_length=1;
biaoji(:,:,1)=first_point(:,:);
%以i,j为起始点开始计算边缘长度
while ((first_point(1,1))~=1&&(first_point(1,2)~=1))
first_point=fun_find(bw1,biaoji,first_point(1,1),first_point(1,2),bianjie_length,1,1);
temp1_im(first_point(1,1),first_point(1,2))=255;
temp_bianjie_length=bianjie_length;
bianjie_length=temp_bianjie_length+1;
biaoji(:,:,bianjie_length)=first_point(:,:);
figure,imshow(temp1_im);
title('temp1_image');
disp(first_point);
end
disp(bianjie_length);
if bianjie_length>=2 % 人为设定的阈值
for p=1:m % 存储符合要求的边界
for q=1:n
if temp1_im(p,q)==255
end_im(p,q)=temp1_im(p,q);
end
end
end
bianjie_length=0;
else % 不满足条件,就将temp1_im清零,进入下一轮查找
bianjie_length=0;
end
end
end
end
figure,imshow(end_im);
title('检测结果');
下面的是我自己编写的子函数
%fun_find
function re_point=fun_find(bw,bji,a,b,len,c,d)
%//////////////////////////////////////
% bw为图象数据
% m,n为图象数据的维数
% bji为以搜索点记录
% i,j为搜索的起始点坐标
% len为已扫描的点数
%//////////////////////////////////////
re_point=[c,d];
for k=a+1:-1:a-1
for l=b+1:-1:b-1
if ((bw(k,l)==255)&&(bw(k,l)~=bw(a,b)))
for bj=1:len
if (bji(:,:,bj)~=[k,l])
re_point(:,:)=[k,l];
break;
end
end
end
end
end
end
%i-1 7 6 5
% i 8 O 4
% i+1 1 2 3
% j-1 j j+1
% 对应每一点,依次搜寻其左下方点、下方点,右下方点、右边点,即为逆时针方向查找
附件里也有代码 可以运行 但是不能得到预期的结果 很郁闷 哪位高人能帮我看看 改后烦请发到我的油箱吧 [email protected] 谢谢了
大概的思路是:
先扫描到一点 然后以这个点作为起始点为基点 扫描其周围的8个点 当扫描到其周围8个点中有灰度值=255的点时 再以这个新点为基点,重复上面的操作。。。。一直这样进行下去
当其周围8个点中没有灰度值=255的点,则认为该点是孤立点,将其灰度值设为0
代码:
%main.m
clc
clear all
bw1=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 255 0 0 0 0 0 0 0 0 0 0 0
0 0 255 255 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 255 0 0 255 0 0 0 0 0 0 0 0 0 0 0
0 0 0 255 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 255 0 0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 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
figure,imshow(bw1);
title('数据图象');
[m,n]=size(bw1);
%链码计算,长度小于某个值的边缘滤出
bianjie_length=0;
first_point=zeros(1,2); %起始点
temp1_im=zeros(m,n); %临时存放检测到的点
end_im=zeros(m,n); %最终检测到的点
biaoji=zeros(1,2,2*(m+n)); %用于避免循环的出现
for i=2:20
for j=2:20
if bw1(i,j)==255
first_point=[i,j];
temp1_im(first_point(1,1),first_point(1,2))=255;
bianjie_length=1;
biaoji(:,:,1)=first_point(:,:);
%以i,j为起始点开始计算边缘长度
while ((first_point(1,1))~=1&&(first_point(1,2)~=1))
first_point=fun_find(bw1,biaoji,first_point(1,1),first_point(1,2),bianjie_length,1,1);
temp1_im(first_point(1,1),first_point(1,2))=255;
temp_bianjie_length=bianjie_length;
bianjie_length=temp_bianjie_length+1;
biaoji(:,:,bianjie_length)=first_point(:,:);
figure,imshow(temp1_im);
title('temp1_image');
disp(first_point);
end
disp(bianjie_length);
if bianjie_length>=2 % 人为设定的阈值
for p=1:m % 存储符合要求的边界
for q=1:n
if temp1_im(p,q)==255
end_im(p,q)=temp1_im(p,q);
end
end
end
bianjie_length=0;
else % 不满足条件,就将temp1_im清零,进入下一轮查找
bianjie_length=0;
end
end
end
end
figure,imshow(end_im);
title('检测结果');
下面的是我自己编写的子函数
%fun_find
function re_point=fun_find(bw,bji,a,b,len,c,d)
%//////////////////////////////////////
% bw为图象数据
% m,n为图象数据的维数
% bji为以搜索点记录
% i,j为搜索的起始点坐标
% len为已扫描的点数
%//////////////////////////////////////
re_point=[c,d];
for k=a+1:-1:a-1
for l=b+1:-1:b-1
if ((bw(k,l)==255)&&(bw(k,l)~=bw(a,b)))
for bj=1:len
if (bji(:,:,bj)~=[k,l])
re_point(:,:)=[k,l];
break;
end
end
end
end
end
end
%i-1 7 6 5
% i 8 O 4
% i+1 1 2 3
% j-1 j j+1
% 对应每一点,依次搜寻其左下方点、下方点,右下方点、右边点,即为逆时针方向查找
附件里也有代码 可以运行 但是不能得到预期的结果 很郁闷 哪位高人能帮我看看 改后烦请发到我的油箱吧 [email protected] 谢谢了