像素标记法统计图像中连通域的个数
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邻域法)
相关文章推荐
- 线程和正则表达式
- 【ZOJ】3886 Nico Number【线段树】
- 【POJ 3041】Asteroids
- 不加好友就能微信聊天?微信企业号实现了!
- android AIDL RPC 机制
- Java 编程下 Eclipse/myeclipse 如何设置单行代码显示的最大宽度
- 全方面了解PHP框架
- 【串和序列处理 8】最长平台问题
- 【排序结构7】 基数排序
- zoj 3882 Help Bob(zoj 2015年7月月赛)
- 使用jQuery基本过滤选择器
- 基于jQuery鼠标滚轮滑动到页面节点部分
- QT QSS 使用
- JavaScript正则表达式知识汇总
- 不停止MySQL服务增加从库的两种方式
- org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 解决方法
- C# inline-asm / 嵌入x86汇编
- php 加载字体 并保存成图片
- 10个用于Web开发的最好 Python 框架
- HDU-1317 XYZZY