Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3425879
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: C/C++

2014-11-21 18:57:19

  1. //连通区域标C++记程序   
  2. //作者:胡柳   
  3.    
  4. //本程序使用了opencv中图像数据结构:IplImage   
  5. //算法原理:首先进行第一次扫描,标记连通区域;   
  6. //然后对等价标记进行合并,   
  7. //最后进行第二次扫描,对等价标记进行重新标记。   
  8.    
  9. //输入:mask为标记模板指针,img1为待标记二值图像指针   
  10. //返回值为连通区域数   
  11. int label( WORD *mask,IplImage *img1)   
  12. {   
  13.     BYTE *pb1=(BYTE*)img1->imageData;   
  14.     int height=img1->height;   
  15.     int width=img1->width;   
  16.     int k,j;   
  17.     
  18.     for( k=0; k
  19.         mask[k]=0;   
  20.    
  21.     int cw=100;//初始区域数目   
  22. loop:      
  23.     bool *col=new bool[cw*cw];   
  24.     for(k=0; k
  25.         col[k]=false;   
  26.        
  27. //第一次扫描   
  28.     int lab=1;   
  29.     for(k=1;k
  30.         for( j=1;j
  31.             if(pb1[k*width+j]==255)   
  32.                 if((mask[k*width+j-1]+mask[(k-1)*width+j-1]+mask[(k-1)*width+j]+mask[(k-1)*width+j+1])==0)   
  33.                 {   
  34.                     mask[k*width+j]=lab;   
  35.                     lab=lab+1;   
  36.                     if(lab>cw)   
  37.                     {   
  38.                         delete col;   
  39.                         cw+=200;   
  40.                         goto loop;   
  41.                     }   
  42.                 }   
  43.                 else   
  44.                 {   
  45.                     if(mask[k*width+j-1]!=0)   
  46.                         mask[k*width+j]=mask[k*width+j-1];   
  47.                     
  48.                     if(mask[(k-1)*width+j-1]!=0)   
  49.                         if(mask[k*width+j]==0)   
  50.                             mask[k*width+j]=mask[(k-1)*width+j-1];   
  51.                         else   
  52.                         {   
  53.                             col[mask[k*width+j]*cw+mask[(k-1)*width+j-1]]=true;   
  54.                             col[mask[(k-1)*width+j-1]*cw+mask[k*width+j]]=true;   
  55.                         }   
  56.                     
  57.                     if(mask[(k-1)*width+j]!=0)   
  58.                         if(mask[k*width+j]==0)   
  59.                             mask[k*width+j]=mask[(k-1)*width+j];   
  60.                         else   
  61.                         {   
  62.                             col[mask[k*width+j]*cw+mask[(k-1)*width+j]]=true;   
  63.                             col[mask[(k-1)*width+j]*cw+mask[k*width+j]]=true;   
  64.                         }   
  65.                    
  66.                     if(mask[(k-1)*width+j+1]!=0)   
  67.                         if(mask[k*width+j]==0)   
  68.                             mask[k*width+j]=mask[(k-1)*width+j+1];   
  69.                         else   
  70.                         {   
  71.                             col[mask[k*width+j]*cw+mask[(k-1)*width+j+1]]=true;   
  72.                             col[mask[(k-1)*width+j+1]*cw+mask[k*width+j]]=true;   
  73.                         }   
  74.                 }   
  75.        
  76.     if(lab==1)   
  77.         return 0;   
  78.    
  79. //等价关系合并   
  80.     bool *col2=new bool[lab*lab];   
  81.     for(k=0; k
  82.         col2[k]=false;   
  83.     for(k=1;k
  84.         for(j=1;j
  85.             if(col[k*cw+j]==true||k==j)   
  86.                 col2[k*lab+j]=true;   
  87.     delete[] col;   
  88.        
  89.     for(k=1;k
  90.         for(j=1;j
  91.             if(col2[j*lab+k]==true)   
  92.                 for(int i=1;i
  93.                     col2[j*lab+i]=col2[j*lab+i]||col2[k*lab+i];   
  94.        
  95.    
  96.     WORD *col3=new WORD[lab];   
  97.     WORD *col3_s=new WORD[lab];   
  98.     for(k=1;k
  99.     {   
  100.         int c1, c2;   
  101.         for(j=1;j
  102.             if(col2[k*lab+j]==true)   
  103.             {   
  104.                 c1=j;   
  105.                 break;   
  106.             }   
  107.         for(j=1;j
  108.             if(col2[j*lab+k]==true)   
  109.             {   
  110.                 c2=j;   
  111.                 break;   
  112.             }   
  113.         col3[k]=min(c1,c2);   
  114.         col3_s[k]=col3[k];   
  115.     }   
  116.    
  117.            
  118.     for(k=1;k
  119.     {   
  120.         WORD t;   
  121.         for(j=1;j
  122.             if(col3_s[j]>col3_s[j+1])   
  123.             {   
  124.                 t=col3_s[j];   
  125.                 col3_s[j]=col3_s[j+1];   
  126.                 col3_s[j+1]=t;   
  127.             }   
  128.     }   
  129.    
  130.     WORD *ind=new WORD[lab];   
  131.     ind[col3_s[1]]=1;   
  132.     int c=2;   
  133.     for(k=1;k
  134.         if(col3_s[k+1]!=col3_s[k])   
  135.         {   
  136.             ind[col3_s[k+1]]=c;   
  137.             c++;   
  138.         }   
  139.    
  140. //第2次扫描   
  141.     for(k=1;k
  142.         for( j=1;j
  143.             if(mask[k*width+j]!=0)   
  144.                 mask[k*width+j]=ind[col3[mask[k*width+j]]];   
  145.                
  146.    delete[] col2;   
  147.    delete[] col3;   
  148.    delete[] col3_s;   
  149.    delete[] ind;   
  150.    
  151.     return(c-1);//返回区域数目   
  152.  }  
阅读(3930) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~