diff --git a/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/zhang-suen-thinning-algorithm.md b/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/zhang-suen-thinning-algorithm.md index 64d4e8b5d4..72ab1cf2ee 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/zhang-suen-thinning-algorithm.md +++ b/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/zhang-suen-thinning-algorithm.md @@ -10,77 +10,61 @@ dashedName: zhang-suen-thinning-algorithm 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: - - -
- #################                   #############
- ##################               ################
- ###################            ##################
- ########     #######          ###################
-   ######     #######         #######       ######
-   ######     #######        #######
-   #################         #######
-   ################          #######
-   #################         #######
-   ######     #######        #######
-   ######     #######        #######
-   ######     #######         #######       ######
- ########     #######          ###################
- ########     ####### ######    ################## ######
- ########     ####### ######      ################ ######
- ########     ####### ######         ############# ######
-
+```js +const testImage1 = [ + ' ', + '######### ######## ', + '### #### #### #### ', + '### ### ### ### ', + '### #### ### ', + '######### ### ', + '### #### ### ### ', + '### #### ### #### #### ### ', + '### #### ### ######## ### ', + ' ' +]; +``` It produces the thinned output: -
+```js
+[ '                               ',
+  '########         ######        ',
+  '#      #        ##             ',
+  '#       #       #              ',
+  '#      #        #              ',
+  '###### #        #              ',
+  '#     ##        #              ',
+  '#      #    #   ##    ##   #   ',
+  '#       #         ####         ',
+  '                               ' ];
+```
 
-    # ##########                       #######
-     ##        #                   ####       #
-     #          #                 ##
-     #          #                #
-     #          #                #
-     #          #                #
-     ############               #
-     #          #               #
-     #          #                #
-     #          #                #
-     #          #                #
-     #                            ##
-     #                             ############
-                       ###                          ###
-
-
- -

Algorithm

+## 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: - - - - -
P9P2P3
P8P1P4
P7P6P5
+$$\begin{array}{|c|c|c|} + \\hline + P9 & P2 & P3\\\\ \\hline + P8 & \boldsymbol{P1} & P4\\\\ \\hline + P7 & P6 & P5\\\\ \\hline +\end{array}$$ Obviously the boundary pixels of the image cannot have the full eight neighbours. - +- Define $A(P1)$ = the number of transitions from white to black, ($0 \to 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(P1)$ = the number of black pixel neighbours of P1. ($= \\sum(P2 \ldots P9)$) **Step 1:** All pixels are tested and pixels satisfying all the following conditions (simultaneously) are just noted at this stage. -
    -
  1. The pixel is black and has eight neighbours
  2. -
  3. $2 <= B(P1) <= 6$
  4. -
  5. $A(P1) = 1$
  6. -
  7. At least one of P2, P4 and P6 is white
  8. -
  9. At least one of P4, P6 and P8 is white
  10. -
+1. The pixel is black and has eight neighbours +2. $2 \le B(P1) \le 6$ +3. $A(P1) = 1$ +4. At least one of $P2$, $P4$ and $P6$ is white +5. At least one of $P4$, $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. @@ -88,14 +72,12 @@ After iterating over the image and collecting all the pixels satisfying all step All pixels are again tested and pixels satisfying all the following conditions are just noted at this stage. -
    -
  1. The pixel is black and has eight neighbours
  2. -
  3. $2 <= B(P1) <= 6$
  4. -
  5. $A(P1) = 1$
  6. -
  7. At least one of P2, P4 and P8 is white
  8. -
  9. At least one of P2, P6 and P8 is white
  10. -
- +1. The pixel is black and has eight neighbours +2. $2 \le B(P1) \le 6$ +3. $A(P1) = 1$ +4. At least one of $P2$, $P4$ and $P8$ is white +5. At least one of $P2$, $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:** @@ -104,7 +86,7 @@ If any pixels were set in this round of either step 1 or step 2 then all steps a # --instructions-- -Write a routine to perform Zhang-Suen thinning on the provided image matrix. +Write a routine to perform Zhang-Suen thinning on the provided `image`, an array of strings, where each string represents single line of the image. In the string, `#` represents black pixel, and whitespace represents white pixel. Function should return thinned image, using the same representation. # --hints-- @@ -117,19 +99,25 @@ assert.equal(typeof thinImage, 'function'); `thinImage` should return an array. ```js -assert(Array.isArray(result)); +assert(Array.isArray(thinImage(_testImage1))); ``` `thinImage` should return an array of strings. ```js -assert.equal(typeof result[0], 'string'); +assert.equal(typeof thinImage(_testImage1)[0], 'string'); ``` -`thinImage` should return an array of strings. +`thinImage(testImage1)` should return a thinned image as in the example. ```js -assert.deepEqual(result, expected); +assert.deepEqual(thinImage(_testImage1), expected1); +``` + +`thinImage(testImage2)` should return a thinned image. + +```js +assert.deepEqual(thinImage(_testImage2), expected2); ``` # --seed-- @@ -137,7 +125,31 @@ assert.deepEqual(result, expected); ## --after-user-code-- ```js -const imageForTests = [ +const _testImage1 = [ + ' ', + '######### ######## ', + '### #### #### #### ', + '### ### ### ### ', + '### #### ### ', + '######### ### ', + '### #### ### ### ', + '### #### ### #### #### ### ', + '### #### ### ######## ### ', + ' ' +]; +const expected1 = [ + ' ', + '######## ###### ', + '# # ## ', + '# # # ', + '# # # ', + '###### # # ', + '# ## # ', + '# # # ## ## # ', + '# # #### ', + ' ' +]; +const _testImage2 = [ ' ', ' ################# ############# ', ' ################## ################ ', @@ -156,7 +168,7 @@ const imageForTests = [ ' ######## ####### ###### ################ ###### ', ' ######## ####### ###### ############# ###### ', ' ']; -const expected = [ +const expected2 = [ ' ', ' ', ' # ########## ####### ', @@ -176,35 +188,27 @@ const expected = [ ' ', ' ' ]; -const result = thinImage(imageForTests); ``` ## --seed-contents-- ```js -const testImage = [ - ' ', - ' ################# ############# ', - ' ################## ################ ', - ' ################### ################## ', - ' ######## ####### ################### ', - ' ###### ####### ####### ###### ', - ' ###### ####### ####### ', - ' ################# ####### ', - ' ################ ####### ', - ' ################# ####### ', - ' ###### ####### ####### ', - ' ###### ####### ####### ', - ' ###### ####### ####### ###### ', - ' ######## ####### ################### ', - ' ######## ####### ###### ################## ###### ', - ' ######## ####### ###### ################ ###### ', - ' ######## ####### ###### ############# ###### ', - ' ']; - function thinImage(image) { } + +const testImage1 = [ + ' ', + '######### ######## ', + '### #### #### #### ', + '### ### ### ### ', + '### #### ### ', + '######### ### ', + '### #### ### ### ', + '### #### ### #### #### ### ', + '### #### ### ######## ### ', + ' ' +]; ``` # --solutions--