Algorithm 1 -Recursive
1.Initialize your label image with no labels for all pixels.
2.Scan your binary image and find the first non-zero pixel, that has no label. Create a label for it in the label image.
3.Find all unmarked non-zero neighbors and mark them as visited with the same label id.
4.For each of these neighbors, go to step 3.
5.Repeat step 2 until you have scanned the entire image.
ALGORITHM version a (here labeling where blob /object point has thresholded value of 0 in the binary image)
oid compLabel(int i, int j,int m){
if(getPixel(i,j)==0){
setPixel(i,j,m);
compLabel(i-1,j-1,m);
compLabel(i-1,j,m);
compLabel(i-1,j+1,m);
compLabel(i,j-1,m);
compLabel(i,j+1,m);
compLabel(i+1,j-1,m);
compLabel(i+1,j,m);
compLabel(i+1,j+1,m);
}
}
void beginLabel(){
int m=0;
for(int y=0; y < size; y++)
for(int x=0; x < size; x++)
if(getPixel(x,y)==0)
compLabel(x,y,++m);
}
ALRGORITHM version b (here labeling where blob /object point has thresholded value of 1 in the binary image)
void setLabels(){
int m=2; //scr is a pseudo screen object
for(int y=0; y<scr.hgt; y++)
for(int x=0; x<scr.wid; x++)
if(scr.getPixel(x,y)==1) compLabel(x,y,m++);
}
void compLabel(int i, int j,int m){
if(scr.getPixel(i,j)==1){
scr.setPixel(i,j,m); //assign label
slow(); //thread delay
compLabel(i-1,j-1,m); compLabel(i-1,j,m);
compLabel(i-1,j+1,m); compLabel(i,j-1,m);
compLabel(i,j+1,m); compLabel(i+1,j-1,m);
compLabel(i+1,j,m); compLabel(i+1,j+1,m);
}
} |