Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zhang-Suen thinning algorithm implementation not working as expected

Tags:

java

algorithm

I am trying to use Zhang-Suen thinning algorithm . I have attempted to implement it in Java. But the problem is it finds me edges not as a one pixel width line. First time I am using this algorithm and I don't know what is wrong with my logic.

What I want to achieve is:

enter image description here

What I am able to achieve is:

enter image description here

 public void thinStepI(){

     delList.clear();
    neighbor = 0;
    connectivity = 0;

     for(int i=4;i<width-4;i++)
        for(int j=4;j<height-4;j++){
            p = pixelList[i][j];
            if (p == 1){
                p1 = pixelList[i-1][j]; 
                p2 = pixelList[i-1][j+1]; 
                p3 = pixelList[i][j+1]; 
                p4 = pixelList[i+1][j+1]; 
                p5 = pixelList[i+1][j];
                p6 = pixelList[i+1][j-1]; 
                p7 = pixelList[i][j-1]; 
                p8 = pixelList[i-1][j-1];  


                neighbor = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;

                if (p1 == 0 && p2 == 1)
                    connectivity ++;
                if (p2 == 0 && p3 == 1)
                    connectivity ++;
                if (p3 == 0 && p4 == 1)
                    connectivity ++;
                if (p4 == 0 && p5 == 1)
                    connectivity ++;
                if (p5 == 0 && p6 == 1)
                    connectivity ++;
                if (p6 == 0 && p7 == 1)
                    connectivity ++;
                if (p7 == 0 && p8 == 1)
                    connectivity ++;
                if (p8 == 0 && p1 == 1)
                    connectivity ++;

                if ( connectivity == 1 && (neighbor >= 2 && neighbor <= 6) &&
                     (p1 * p3 * p5 == 0) && (p3 * p5 * p7 == 0) ){
                    delList.add(i);
                    delList.add(j);
                }



            }      
        }

     int length = delList.size();
     if (length != 0){
        for(int i =0; i < (length - 1); i+=2){
            pixelList[delList.get(i)][delList.get(i+1)] = 0;
            System.out.println("oldu");
        }
        thinStepI();
     }


}

   public void thinStepII(){
    delList.clear();
    neighbor = 0;
    connectivity = 0;

     for(int i=4;i<width-4;i++)
        for(int j=4;j<height-4;j++){
            if (pixelList[i][j] == 1){
                p  = pixelList[i][j]; // ** Origin Pixel ** 
                p1 = pixelList[i-1][j]; 
                p2 = pixelList[i-1][j+1]; 
                p3 = pixelList[i][j+1]; 
                p4 = pixelList[i+1][j+1]; 
                p5 = pixelList[i+1][j];
                p6 = pixelList[i+1][j-1]; 
                p7 = pixelList[i][j-1]; 
                p8 = pixelList[i-1][j-1];                    

                neighbor = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;

                if (p1 == 0 && p2 == 1)
                    connectivity ++;
                if (p2 == 0 && p3 == 1)
                    connectivity ++;
                if (p3 == 0 && p4 == 1)
                    connectivity ++;
                if (p4 == 0 && p5 == 1)
                    connectivity ++;
                if (p5 == 0 && p6 == 1)
                    connectivity ++;
                if (p6 == 0 && p7 == 1)
                    connectivity ++;
                if (p7 == 0 && p8 == 1)
                    connectivity ++;
                if (p8 == 0 && p1 == 1)
                    connectivity ++;

                if ( connectivity == 1 && (neighbor >= 2 && neighbor <= 6) &&
                     (p1 * p3 * p7 == 0) && (p1 * p5 * p7 == 0) ){
                    delList.add(i);
                    delList.add(j);
                }



            }     
        }

     int length = delList.size();
     if (length != 0){
        for(int i =0; i < (length - 1); i+=2){
            pixelList[delList.get(i)][delList.get(i+1)] = 0;
            System.out.println("oldu2");
        }
        thinStepII();
     }

}

Where is the error in my logic that is causing me to get the incorrect results?

like image 984
Ecrin Avatar asked Oct 08 '22 09:10

Ecrin


2 Answers

I think the link you are using for the description of the ZS algorithm is wrong. A necessary requirement for the deletion of a foreground pixel is that its crossing number is 1. The "crossing number" is the number of times foreground pixels change to background pixels as you traverse the 8 neighbouring pixels in clockwise order. This seems to be missing from the description in your link. See: http://www.uel.br/pessoal/josealexandre/stuff/thinning/ftp/lam-lee-survey.pdf for a better discussion.

like image 105
Alasdair Avatar answered Oct 13 '22 10:10

Alasdair


The statement

connectivity = 0;

should be placed inside the two for loops in each method.

like image 21
Nguyễn Minh Vũ Avatar answered Oct 13 '22 12:10

Nguyễn Minh Vũ