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

来源:互联网 发布:问卷调查自动填写软件 编辑:程序博客网 时间:2024/04/29 04:31
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    endendflag

初到CBIB,导师就布置了几个练习,下面是我编写的利用像素标记法实现的图像中连通域个数的统计(8邻域法)
0 0
原创粉丝点击