Chang-Suen thinning algorithm implementation does not work properly

I am trying to use the thinning algorithm Zhang-Suen . I tried to implement it in Java. But the problem is that it detects that the edges are not a line of width of one pixel. The first time I use this algorithm, and I do not know what is wrong with my logic.

What I want to achieve:

enter image description here

I can achieve:

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 causes me the wrong results?

+5
source share
3 answers

connectivity = 0;

.

+1

You should loop until there are no changes to the bitmap. You have completed only one iteration of the algorithm.

+1
source

All Articles