Vous êtes sur la page 1sur 5

Single Pass Connected Component Labelling Algorithm

Single Pass Connected Component Labelling Algorithm

In the Single Pass connected component labelling algorithm each pixel is visited approximately once. The algorithm can be applied to color as well as binary images. The algorithm uses a distance measure to compare if the pixels belong to same component or not. The algorithm uses a decision criteria to decide if the connected component analysis if to be performed for the given pixel. The algorithm outputs a labelled matrix indicating connected components. For binary image the distance function returns a 1 if pixel intensities are same and a 0 if pixel intensities are dierent. For binary image the decision criteria is to check if pixel intensity is 255 and perform connected component analysis The distance function is also used to check if pixel is a pixel of interest by comparing it with a predened foreground color.Only if the pixel Algorithm is as follows :
check if pixel is labelled or not . if the pixel is not labelled check if decision criteria is satised. Assign a new label to pixel . Push the current pixel in the stack. pop pixel from top of stack. For each pixel in the stack check its 4/8 neighbours which are not

labelled and satisfy the distance criteria. botton stack.

If neighbour satises distance criteria push neighbourhood pixels to the Continue this opration till stack is empty. Loop through all rows and columns.

Single Pass Connected Component Labelling Algorithm Using this method each pixel is visited approximately once.

For color image we can specify the decision criteria or decision threshold according to the threshold.Also the algorithm can be used on any color space.

Sample Code

Sample Code

s t r i n g winName2 ="binary image " ; s t r i n g winName3 =" l a b e l l e d image " ; i n t d i s t 3 ( Vec3b a , Vec3b b , i n t r a d i u s 1 ) { i f ( b . v a l [0]==255) return 1; else return 0; } i n t d i s t 4 ( Vec3b a , Vec3b b , i n t r a d i u s 1 ) { i f ( ( abs ( b . v a l [0] a . v a l [0]))==0 ) return 1; else return 0; } void connected (Mat dst ) { Mat l a b e l ; dst . copyTo ( l a b e l ) ; l a b e l . convertTo ( l a b e l , CV_32FC3, 1 , 0 ) ; l a b e l . setTo ( cv : : S c a l a r : : a l l ( 0 ) ) ;

Vec3b v0 ; v0 . v a l [ 0 ] = 0 ;

Sample Code

v0 . v a l [ 1 ] = 0 ; v0 . v a l [ 2 ] = 2 5 5 ; f l o a t l a b e l i =0; i n t l l =100; i n t r a d i u s 1=s p a t i a l R a d ; i n t k=0; i n t neighbor [ 1 6 ] = { 1, 1, 0 , 1 , 1 , 1 , 1 ,0 , 1 ,0 , 1 ,1 , 0 , 1 , 1 , 1 } ; i n t neighbor1 [ 8 ] = { 1 ,0 , 0 , 1 , 1 , 0 , 0 , 1 } ; i n t r a d i u s =3; f o r ( i n t i =1; i <dst . c o l s 1; i ++) { f o r ( i n t j =1; j<dst . rows 1; j++) { Vec3b v1=dst . at<cv : : Vec3b>(j , i ) ; Vec3f l=l a b e l . at<Vec3f >(j , i ) ; i f ( d i s t 3 ( v0 , v1 , r a d i u s 1 )==1 && ( f l o a t ) l . v a l [0]==0) { stack <CvPoint> p o i n t s ; p o i n t s . push ( cvPoint ( i , j ) ) ; l l = l l +10; l a b e l i=rand ()%255; Vec3f l ; l . v a l [0]= l a b e l i ; l . val [1]=0; l . val [2]=0; l a b e l . at<Vec3f >(j , i )= l ;

w h i l e ( ! p o i n t s . empty ( ) )

CvPoint p = p o i n t s . top ( ) ; // lPt . push_back ( p ) ; p o i n t s . pop ( ) ; f o r ( k=0;k<8;k++) { i n t x1=p . x+neighbor [ 2 k ] ;

Sample Code

i n t y1=p . y+neighbor [ 2 k +1]; Vec3b v2=dst . at<cv : : Vec3b>(y1 , x1 ) ; v1=dst . at<cv : : Vec3b>(j , i ) ; Vec3f l 2=l a b e l . at<Vec3f >(y1 , x1 ) ; i f ( x1>1 && y1 > 1 && x1 < dst . c o l s 1 && y1 <dst . rows 1 && { p o i n t s . push ( cvPoint ( x1 , y1 ) ) ; Vec3f l ; l . v a l [0]= l a b e l i ; l . val [1]=0; l . val [2]=0; l a b e l . at<Vec3f >(y1 , x1)= l ; l=l a b e l . at<Vec3f >(y1 , x1 ) ; } } } } } }

l a b e l . convertTo ( l a b e l ,CV_8UC3, 1 . 0 , 0 ) ; imshow ( winName3 , l a b e l ) ; waitKey ( 0 ) ;

Vous aimerez peut-être aussi