Zhang-Suen thinning algorithm implementation not working as expected

Magera

New Member
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:
tn7oj.png
What I am able to achieve is:
xA0FH.png
\[code\] 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[j]; if (p == 1){ p1 = pixelList[i-1][j]; p2 = pixelList[i-1][j+1]; p3 = pixelList[j+1]; p4 = pixelList[i+1][j+1]; p5 = pixelList[i+1][j]; p6 = pixelList[i+1][j-1]; p7 = pixelList[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[j] == 1){ p = pixelList[j]; // ** Origin Pixel ** p1 = pixelList[i-1][j]; p2 = pixelList[i-1][j+1]; p3 = pixelList[j+1]; p4 = pixelList[i+1][j+1]; p5 = pixelList[i+1][j]; p6 = pixelList[i+1][j-1]; p7 = pixelList[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(); }}\[/code\]Where is the error in my logic that is causing me to get the incorrect results?
 
Top