Seeded Region Growing Using Line Scan Algorithm - Stack Base Implementation
Seeded Region Growing Using Line Scan Algorithm - Stack Base Implementation
Pi19404
March 1, 2013
Contents
Contents
2 |
??
3 |
??
For the present application we assume that we need identify the region in the image to which seed pixels belong ie other region of the image are not required. For every seed points 4/8 connected neighbors of seed point are analyzed for similarity with the seed pixel using some predefined criteria. Thus all the neighborhood pixels are marked to be compared with seed pixel. If the connected neighbor satisfies the criteria then it is included labelled as desired region and neighbor is unmarked. While all the connected neighbors of the present neighbor are marked. This process continues till no marked pixels remains which results in segmentation of region to which seed pixel belongs. As the region grows the similarity comparision can be peformed with the mean value of region rather than just the seed pixels.
4 |
??
As algorithm progress all connected neighbors of seed pixels are used to initiate recursive calls .If these pixels satisfy the similarity criteria use recurive calls for their connected neighbors and so on. In this way all the connected pixel of seed pixel satisfying the similarity criteria are labelled in the output image and segmentation is achieved. The we can either consider 4/8 connected beighbors for analysis. In the present implementation we use a threshold of 20 . If euclidean distance between two pixels is less than 20 we consider the pixels to be similar. We can see that region growing algorithm stop at places when reflections of object fall on the water.Increasing the threshold is one option to overcome this problem.But it is not guranteed to work in all scenarios.This is shown below. The seed is choosen at location within the shown object. In
(a) Image
(b) thresh 20
(c) thresh 50
(d) input 2
(e) thresh 50
(f) thresh 80
the second image we can see than even with high threshold of 80,the white over exposed region will never be labelled as belonging
5 |
??
Image Segmentation - Region Growing Algorithm For UnderWater Image Segmentation to the object.
However this modification may not always provide desired sementation as shown in the images below.
6 |
??
(a) input 2
(b) thresh 80
(c) modified
(d) Image
(e) thresh 20
(f) thresh 50
color and connected to original object. Also we make one more modification of reducing the distance threshold to white pixels keeping it half of color threhold. In the above image the hand along with the object is de-
(a) Image
(b) thresh 20
tected since the color threshold is kept high.But the main point
7 |
??
Image Segmentation - Region Growing Algorithm For UnderWater Image Segmentation to note is that due to addition of edge map the region does not overflow significantly from the desired object boundary
float seed_fill :: ColorDist3 ( Scalar a , Scalar b ) { float a1 = a [2]; float a2 = a [1]; float a3 = a [0]; float b1 = b [2]; float b2 = b [1]; float b3 = b [0]; float dist =((( a1 - b1 ) *( a1 - b1 ) +( a2 - b2 ) *( a2 - b2 ) +( a3 - b3 ) *( a3 - b3 ) ) ) ; dist = sqrt ( dist /3) ; return dist ; }
1 2 3 4 5 6 7 8
Mat edge1 ; cv :: GaussianBlur ( input , input , Size (3 ,3) ,1) ; cv :: Laplacian ( input , edge1 , input . depth () ,1 ,1) ; vector <Mat > em ; cv :: split ( edge1 , em ) ; cv :: add ( em [0] , em [1] , edge ) ; cv :: add ( edge , em [2] , edge ) ; edge = edge /3;
8 |
??
The requirement is to obtain a normalized edge map that returns a value between 0-255. The tranformation function should give a value must be large for stronger edges and small for weak edges. The transformation function is monotonic. The transformation function should give a variation of edge map value must gradually increase/decrease according to the edge strength. A exponential transformation function satisfies these criteria. The transformation function used is
g
0
(x) =
exp(
I (x; y )=K )
K is parameter that determine how fast the edge strength falls Since value corresponding to the maximum edge intensity isg (x) = exp(1=K )
0
=1
(x)
(1)
1 2 3 4 5 6 7
double K =1; edge . convertTo ( edge , CV_32FC (3) , -1.0/(255.0* K ) ,0) ; cv :: exp ( edge , edge ) ; cv :: threshold ( edge , edge , std :: exp ( -1/ K ) +0.001 , 0 , THRESH_TOZERO ) ; edge =1 - edge ; edge =255* edge ; edge . convertTo ( edge , CV_8UC (1) ,1 ,0) ;
9 |
??
Image Segmentation - Region Growing Algorithm For UnderWater Image Segmentation The Euclidean distance of the current pixel with the seed pixel color and with mean pixel intesity value about small neighborhood or current pixel with the white pixel is computed We define a small neighborhood size over whih the mean is computed the default value is 5. The ROI is determined as follows
1 2 3 4 5
int s1 =x - bsize /2 >=0? x - bsize /2:0; int s2 =y - bsize /2 >=0? y - bsize /2:0; int s3 =( x - bsize /2+ bsize ) <= width ? bsize :x - bsize /2+ bsize - width -1; int s4 =( y - bsize /2+ bsize ) <= height ? bsize :y - bsize /2+ bsize - height -1; Rect r = Rect ( s1 , s2 , s3 , s4 ) ;
The mean value of the image pixels and edge pixels are computed in the defined ROI about the current pixel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Mat rx = edge ; uchar * ptr1 =( uchar *) input . data ; uchar * ptr =( uchar *) rx . data ; for ( int i1 =0; i1 < r . height ; i1 ++) { for ( int j1 =0; j1 < r . width ; j1 ++) { f1 = f1 + ptr [( i1 + r . y ) * rx . step + rx . channels () *( j1 + r . x ) ]; f4 = f4 + ptr1 [( i1 + r . y ) * input . step + input . channels () *( j1 + r . x ) ]; f5 = f5 + ptr1 [( i1 + r . y ) * input . step + input . channels () *( j1 + r . x ) +1]; f6 = f6 + ptr1 [( i1 + r . y ) * input . step + input . channels () *( j1 + r . x ) +2]; } } f1 = f1 /( r . width * r . height ) ; f4 = f4 /( r . width * r . height ) ; f5 = f5 /( r . width * r . height ) ; f6 = f6 /( r . width * r . height ) ;
Next we compute the distance of the mean intensity value with white color value
1 2
10 |
??
Image Segmentation - Region Growing Algorithm For UnderWater Image Segmentation The final step is to specify a threshold .If the distance dist2 to white color value plus mean edge intesity is less than specifed threshold then it belongs to object and we return a similarity value of 0 else we compare the color distane measure to seed pixel value with threshold and decide if the pixel belongs to object or not.
The input to scan line algorithm are a pair of seed points points lying on a horizontal line. The pair of points have different x coordinates and same y co-ordinates. and if line above/below current line is to be scanned next. The scan line algorithm consists of two step ,finding the seed points and next is to mark the pixels between the seed points.
11 |
??
0.8.1 Initialization
The Seed Points can be found by identifying the points at which the scan line intersects the edges of image. Initial pair of seed points are the same as input seed pixel point. From the current point we keep on checking all the pixels to the left and right of current pixel using similarity criteria. The points at which the similarity criteria is not satisfied are local edge points. Points A,B shown in the figure determine the seed points.
Thus for the above line we obtain the left edge as point A or point to left of A. and the right edge as point B or point to right of B. Let these new points be denoted as C and D. From point C we move towards left filling points which satisfy similarity criteria. If we reach the point D.Then Line has been successfully filled and scan can be performed for the line above and below the present line,
12 |
??
Image Segmentation - Region Growing Algorithm For UnderWater Image Segmentation From point C as we move towards left we may encounter a edge before we reach point D Line segment (E ; F ) and (G; H ) denotes such a case. Thus line segment (E ; F ) and (G; H ) are considered the scanned line and line above/below this line segment will be scanned. Similarity if we move from the point D and end before reaching point C, then this new line segment will be filled and lines above and below this line segment will be scanned next. In this way entire image will be parsed by using connected line segments.
0.9 Implementation
In present application we use a stack based implementation. The elements of stack contain iformation above the line to be scanned. A class Line_Element is used to define the entry.It will contain the right and left co-ordinates of current line segment,y co-ordinate line segment above/below
1 2 3 4 5 6 7
The first operation is to obtain the left and right edges of the line This is done by scanning left/right from the left and right pixel values.
1 2 3 4 5 6 7
for ( xa = p . left ; xa >= nMinX && check_color ( xa , p . y ) == true ; xa - -) ; if ( xa < p . left ) xa = xa +1; for ( xb = p . right ; xb < nMaxX && check_color ( xb , p . y ) == true ; xb ++) ; if ( xb > p . right ) xb = xb -1;
13 |
??
From left end of the line we determine the line segments encountered as we move towards the right and add pixel of line above and below these line segments to stack. In the below code segment.
xa
and
xb
14 |
??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
0.11 Code
The class seed_fill_stack defines a class for stack based scan line algorithm. The input to algorithm is input image and seed pixel location and output is labelled image. Code is available in repository https://github.com/pi19404/m19404/tree/master/RegionGrowing
15 |
??