GeekVan

导航

matlab 区域生长算法生成二值图像边界区域

function ret=growseed()
%=====区域生长算法========
%input :二值图像数据
%output:边界点二值图像数据
global I;global M;
global N;global Y;global T;global newy;global newx;%==边界点x,y坐标
%获取多张图片的二值数据
tempI=draw();   %tempI 36*45*numbertotal
count=length(tempI(1,1,:));
wide=length(tempI(:,1,1));%36
high=length(tempI(1,:,1));%45
result=zeros(wide,high,count);
for k=1:count
I=tempI(:,:,k);
% figure,imshow(I),title('原始图像')
I=double(I);
[M,N]=size(I);
[seedx,seedy]=searchseed(I);
Y=zeros(M,N);          %作一个全零与原图像等大的图像矩阵Y,作为输出图像矩阵
T=zeros(M,N);%标记点。。。
newx=0;newy=0;
calseed(seedx,seedy);
figure,imshow(Y);
% if k>1&&k<count
% result(:,:,k)=Y;
%  figure,imshow(Y);
% else
%     result(:,:,k)=T;
% %     imshow(T);
% %     figure,imshow(T);
% end
end



ret=result;
end

function calseed(seedx,seedy)
global M;
global N;
global Y;
global I;
global newx;
global newy;

global T;
for i = -1 : 1
        for j = -1 : 1
            newseedx = seedx + i;
            newseedy = seedy+ j;
            %生长准则:判断生长点8邻域内像素的各自灰度值是否与生长点所在像素灰度值相等
            if newseedx> 0 && newseedx <= M && newseedy > 0 && newseedy <= N &&T(newseedx,newseedy)==0&&I(newseedx,newseedy)==1
                T(newseedx,newseedy)=1;
                if isequal(newseedx,newseedy)==1
                    calseed(newseedx,newseedy);
                else
                    Y(newx,newy)=1;%set边界点值为1
                end  
            end
        end
end
     
end

function ret=isequal(x,y)%====判断点的领域是否相等
%===输入1个点的x,y坐标
%==返回1 相等
global I;
global newx;
global newy;
global M;
global N;
ret=1;
tag=0;
for i = -1 : 1
    if tag==0
        for j = -1 : 1
         if x+i> 0 && x+i <= M && y+j > 0 && y+j <= N
          if I( x+i,y+j)==0
              ret=0;
              tag=1;
              newx=x+i;
              newy=y+j;
          end
         end
        end
    end
end


end

function [x,y]=searchseed(I)
%=========寻找种子点=====
%Input:一张二值数据的图片
%Output:返回种子点的x,y坐标

%======求数据为1的质心====
[M,N]=size(I);
corx=0;
cory=0;
count=0;
for i=1:M
    for j=1:N
        if I(i,j)==1
            corx=corx+i;
            cory=cory+j;
            count=count+1;
        end
    end
end
x=round(corx/count);
y=round(cory/count);

end

  

处理结果

posted on 2018-03-28 13:42  GeekVan  阅读(2476)  评论(0编辑  收藏  举报