您的位置:首页 > 其它

像素标记法统计图像中连通域的个数

2015-07-28 09:01 405 查看
clear;
clc;
I=imread('rice.png');
s=strel('disk',2);
I=imerode(I,s);
I=im2bw(I);
I=I*255;
[m,n]=size(I);
%利用队列结构
front=1;
rear=1;%指明下一个像素入队的位置
Queue=zeros(m*n,2);
flag=0;
for i=1:m
for j=1:n
if I(i,j)==255
Queue(rear,1)=i;
Queue(rear,2)=j;
flag=flag+1;
I(i,j)=flag;
rear=rear+1;
end
while rear~=front
temp_i=Queue(front,1);
temp_j=Queue(front,2);
front=front+1;

cond1=(temp_i-1)*(temp_j-1);
if (cond1&I(temp_i-1,temp_j-1)==255)     %1
Queue(rear,1)=temp_i-1;
Queue(rear,2)=temp_j-1;
I(temp_i-1,temp_j-1)=flag;
rear=rear+1;
end

cond2=(temp_i-1)*(temp_j);
if (cond2&I(temp_i-1,temp_j)==255)      %2
Queue(rear,1)=temp_i-1;
Queue(rear,2)=temp_j;
I(temp_i-1,temp_j)=flag;
rear=rear+1;
end

cond3=(temp_i-1)*(temp_j-n);
if (cond3&I(temp_i-1,temp_j+1)==255)     %3
Queue(rear,1)=temp_i-1;
Queue(rear,2)=temp_j+1;
I(temp_i-1,temp_j+1)=flag;
rear=rear+1;
end

cond4=(temp_i)*(temp_j-n);
if (cond4&I(temp_i,temp_j+1)==255)       %4
Queue(rear,1)=temp_i;
Queue(rear,2)=temp_j+1;
I(temp_i,temp_j+1)=flag;
rear=rear+1;
end

cond5=(temp_i-m)*(temp_j-n);
if (cond5&I(temp_i+1,temp_j+1)==255)     %5
Queue(rear,1)=temp_i+1;
Queue(rear,2)=temp_j+1;
I(temp_i+1,temp_j+1)=flag;
rear=rear+1;
end

cond6=(temp_i-m)*(temp_j);
if (cond6&I(temp_i+1,temp_j)==255)       %6
Queue(rear,1)=temp_i+1;
Queue(rear,2)=temp_j;
I(temp_i+1,temp_j)=flag;
rear=rear+1;
end

cond7=(temp_i-m)*(temp_j-1);
if (cond7&I(temp_i+1,temp_j-1)==255)     %7
Queue(rear,1)=temp_i+1;
Queue(rear,2)=temp_j-1;
I(temp_i+1,temp_j-1)=flag;
rear=rear+1;
end

cond8=(temp_i)*(temp_j-1);
if (cond8&I(temp_i,temp_j-1)==255)       %8
Queue(rear,1)=temp_i;
Queue(rear,2)=temp_j-1;
I(temp_i,temp_j-1)=flag;
rear=rear+1;
end
end
end
end

flag


初到CBIB,导师就布置了几个练习,下面是我编写的利用像素标记法实现的图像中连通域个数的统计(8邻域法)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: