Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 工程数学软件 > MATLAB论坛
MATLAB论坛 一切MATLAB相关问题在此讨论。
回复
 
主题工具 显示模式
旧 2008-05-31, 11:33   #1
langde
初级会员
 
注册日期: 2008-03-10
年龄: 45
帖子: 2
声望力: 0
langde 正向着好的方向发展
默认 [求助代码问题出在哪?大人帮帮我!!谢谢

: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 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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 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] 谢谢了
上传的附件
文件类型: rar shiyan.rar (1.7 KB, 4 次查看)
langde 当前离线   回复时引用此帖
旧 2008-08-20, 18:39   #2
pslpsl
普通会员
 
注册日期: 2008-08-17
年龄: 36
帖子: 70
声望力: 20
pslpsl 是一个将要出名的人pslpsl 是一个将要出名的人
默认 回复: [求助代码问题出在哪?大人帮帮我!!谢谢

不知道这段代码的最终目的是什么?

程序调好了 运行结果中3个孤立点被消除了

但如果只是为了消除孤立点完全没必要写得这么复杂的



代码:
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 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0
0 0 255 255 0 0 0 0 0 0 255 0 0 0 0 0 0 255 255 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0
0 0 255 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 255 0 0 0 0
0 0 255 255 255 255 255 255 255 255 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 255 0 255 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 255 255 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 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 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 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 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 255 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 255 255 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 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);
%  EDIT BY PSL 2008 08 20
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));             %用于避免循环的出现
figure;
title('temp1_image');
for i=1:m
    for j=1:n
        if bw1(i,j)==255
            first_point=[i,j];
            bianjie_length=bianjie_length+1;
            biaoji(:,:,bianjie_length)=first_point(:,:);
            %以i,j为起始点开始计算边缘长度
            disp(first_point);
            while  ((first_point(1,1))~=0&(first_point(1,2)~=0))
                   %first_point
                   %biaoji(:,:,bianjie_length)
                   first_point=fun_find(bw1,biaoji,first_point(1,1),first_point(1,2),bianjie_length,0,0);
                   
                   if(first_point(1,1))~=0&(first_point(1,2)~=0)
                   tempPos=biaoji(:,:,bianjie_length);
                   temp1_im(tempPos(1),tempPos(2))=255;
                   bianjie_length=bianjie_length+1;
                   biaoji(:,:,bianjie_length)=first_point(:,:);
                   pause(0.01);
                   imshow(temp1_im);
                   disp(first_point);
                   end
                   
            end
            disp(bianjie_length)
            bianjie_length=0;
        end
    end
end
 
 end_im=temp1_im;
 figure,imshow(end_im);
 title('检测结果');
代码:
function re_point=fun_find(bw,bji,a,b,len,c,d)
%//////////////////////////////////////
%  bw为图象数据
%  m,n为图象数据的维数
%  bji为已搜索点记录
%  a,b为搜索的起始点坐标
%  len为已扫描的点数
%  EDIT BY PSL 2008 08 20
%//////////////////////////////////////
    re_point=[c,d];
    [m,n]=size(bw);
    bool=0;
    if(a==m)
        a1=m;
    else
        a1=a+1;
    end
    if(a==0)
        a2=0;
    else
        a2=a-1;
    end
    if(b==n)
        b1=n;
    else
        b1=b+1;
    end
    if(b==0)
        b2=0;
    else
        b2=b-1;
    end
    for k=a1:-1:a2
        for l=b1:-1:b2
            if (bw(k,l)==255&(k~=a|l~=b))
               for bj=1:len
                   if (bji(:,:,bj)==[k,l])
                       bool=1;
                   end
               end
               if (bool==0);
                       re_point(:,:)=[k,l];
                       break;
               end
            end
        end  
    end
end
pslpsl 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码



所有时间均为北京时间。现在的时间是 10:51


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.