This is an algorithm used to thin a black and white i.e. one bit per pixel images.

For example, with an input image of:

                                                           
 #################                   #############         
 ##################               ################         
 ###################            ##################         
 ########     #######          ###################         
   ######     #######         #######       ######         
   ######     #######        #######                       
   #################         #######                       
   ################          #######                       
   #################         #######                       
   ######     #######        #######                       
   ######     #######        #######                       
   ######     #######         #######       ######         
 ########     #######          ###################         
 ########     ####### ######    ################## ######  
 ########     ####### ######      ################ ######  
 ########     ####### ######         ############# ######  
                                                           
It produces the thinned output:

                                                           
                                                           
    # ##########                       #######             
     ##        #                   ####       #            
     #          #                 ##                       
     #          #                #                         
     #          #                #                         
     #          #                #                         
     ############               #                          
     #          #               #                          
     #          #                #                         
     #          #                #                         
     #          #                #                         
     #                            ##                       
     #                             ############            
                       ###                          ###    
                                                           
                                                           
Algorithm

Assume black pixels are one and white pixels zero, and that the input image is a rectangular N by M array of ones and zeroes.

The algorithm operates on all black pixels P1 that can have eight neighbours.

The neighbours are, in order, arranged as:


   P9      P2      P3   
   P8      P1      P4   
   P7      P6      P5   



Obviously the boundary pixels of the image cannot have the full eight neighbours.

 Define 



A
(
P
1
)


{\displaystyle A(P1)}

 = the number of transitions from white to black, (0 -> 1) in the sequence P2,P3,P4,P5,P6,P7,P8,P9,P2. (Note the extra P2 at the end - it is circular).
 Define 



B
(
P
1
)


{\displaystyle B(P1)}

 = The number of black pixel neighbours of P1. ( = sum(P2 .. P9) )


Step 1

All pixels are tested and pixels satisfying all the following conditions (simultaneously) are just noted at this stage.

 (0) The pixel is black and has eight neighbours
 (1) 



2
<=
B
(
P
1
)
<=
6


{\displaystyle 2<=B(P1)<=6}


 (2) A(P1) = 1
 (3) At least one of P2 and P4 and P6 is white
 (4) At least one of P4 and P6 and P8 is white
After iterating over the image and collecting all the pixels satisfying all step 1 conditions, all these condition satisfying pixels are set to white.



Step 2

All pixels are again tested and pixels satisfying all the following conditions are just noted at this stage.

 (0) The pixel is black and has eight neighbours
 (1) 



2
<=
B
(
P
1
)
<=
6


{\displaystyle 2<=B(P1)<=6}


 (2) A(P1) = 1
 (3) At least one of P2 and P4 and P8 is white
 (4) At least one of P2 and P6 and P8 is white
After iterating over the image and collecting all the pixels satisfying all step 2 conditions, all these condition satisfying pixels are again set to white.



Iteration

If any pixels were set in this round of either step 1 or step 2 then all steps are repeated until no image pixels are so changed.



Task

 Write a routine to perform Zhang-Suen thinning on an image matrix of ones and zeroes.
 Use the routine to thin the following image and show the output here on this page as either a matrix of ones and zeroes, an image, or an ASCII-art image of space/non-space characters. 
          00000000000000000000000000000000
          01111111110000000111111110000000
          01110001111000001111001111000000
          01110000111000001110000111000000
          01110001111000001110000000000000
          01111111110000001110000000000000
          01110111100000001110000111000000
          01110011110011101111001111011100
          01110001111011100111111110011100
          00000000000000000000000000000000



Reference

 Zhang-Suen Thinning Algorithm, Java Implementation by Nayef Reza.
 "Character Recognition Systems: A Guide for Students and Practitioners" By Mohamed Cheriet, Nawwaf Kharma, Cheng-Lin Liu, Ching Suen


-- Params:
-- List of lists (rows) of "pixel" values.
-- Record indicating the values representing black and white.
on ZhangSuen(matrix, {black:black, white:white})
    script o
        property matrix : missing value
        property changePixels : missing value
 
        on A(neighbours) -- Count transitions from white to black.
            set sum to 0
            repeat with i from 1 to 8
                if ((neighbours's item i is white) and (neighbours's item (i mod 8 + 1) is black)) then set sum to sum + 1
            end repeat
            return sum
        end A
 
        on B(neighbours) -- Count neighbouring black pixels.
            set sum to 0
            repeat with p in neighbours
                if (p's contents is black) then set sum to sum + 1
            end repeat
            return sum
        end B
    end script
 
    set o's matrix to matrix
    set rowCount to (count o's matrix)
    set columnCount to (count o's matrix's beginning) -- Assumed to be the same for every row.
    repeat until (o's changePixels is {})
        repeat with step from 1 to 2
            set o's changePixels to {}
            repeat with r from 2 to (rowCount - 1)
                repeat with c from 2 to (columnCount - 1)
                    if (o's matrix's item r's item c is black) then
                        tell (a reference to o's matrix) to ¬
                            set neighbours to {item (r - 1)'s item c, item (r - 1)'s item (c + 1), ¬
                                item r's item (c + 1), item (r + 1)'s item (c + 1), item (r + 1)'s item c, ¬
                                item (r + 1)'s item (c - 1), item r's item (c - 1), item (r - 1)'s item (c - 1)}
                        set blackCount to o's B(neighbours)
                        if ((blackCount > 1) and (blackCount < 7) and (o's A(neighbours) is 1)) then
                            set {P2, x, P4, x, P6, x, P8} to neighbours
                            if (step is 1) then
                                set toChange to ((P4 is white) or (P6 is white) or ((P2 is white) and (P8 is white)))
                            else
                                set toChange to ((P2 is white) or (P8 is white) or ((P4 is white) and (P6 is white)))
                            end if
                            if (toChange) then set end of o's changePixels to {r, c}
                        end if
                    end if
                end repeat
            end repeat
            if (o's changePixels is {}) then exit repeat
            repeat with pixel in o's changePixels
                set {r, c} to pixel
                set o's matrix's item r's item c to white
            end repeat
        end repeat
    end repeat
 
    return o's matrix -- or: return matrix -- The input has been edited in place.
end ZhangSuen
 
on join(lst, delim)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    set txt to lst as text
    set AppleScript's text item delimiters to astid
    return txt
end join
 
on demo()
    set pattern to "00000000000000000000000000000000
01111111110000000111111110000000
01110001111000001111001111000000
01110000111000001110000111000000
01110001111000001110000000000000
01111111110000001110000000000000
01110111100000001110000111000000
01110011110011101111001111011100
01110001111011100111111110011100
00000000000000000000000000000000"
    set matrix to pattern's paragraphs
    repeat with thisRow in matrix
        set thisRow's contents to thisRow's characters
    end repeat
    ZhangSuen(matrix, {black:"1", white:"0"})
    repeat with thisRow in matrix
        set thisRow's contents to join(thisRow, "")
    end repeat
    return join(matrix, linefeed)
end demo
return demo()