--- title: Zhang-Suen thinning algorithm id: 594810f028c0303b75339ad7 challengeType: 5 videoUrl: '' localeTitle: 张素细化算法 --- ## Description
这是用于稀释黑白的算法,即每像素一位图像。例如,输入图像为:
 ##############################
 ##################################
 #####################################
 ##################################
   ###### ####### ####### ######
   ###### ####### #######
   ########################
   #######################
   ########################
   ###### ####### #######
   ###### ####### #######
   ###### ####### ####### ######
 ##################################
 ######## #####################################
 ######## ###################################
 ######## ####### ###### ###################
                                                           
它产生稀疏的输出:
 # ########## ####### ## # #### # # # ## # # # # # # # # # ############ # # # # # # # # # # # # # # ## # ############ ### ### </pre> 

算法

假设黑色像素是一个并且白色像素为零,并且输入图像是矩形N乘M的1和0阵列。 该算法对可以具有八个邻居的所有黑色像素P1进行操作。邻居按顺序安排为:
P9 P2 P3
P8 P1 P4
P7 P6 P5
显然,图像的边界像素不能具有完整的八个邻居。
 Define $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(P1)$ = the number of black pixel neighbours of P1. ( = sum(P2 .. P9) ) 

步骤1:

测试所有像素,并且在此阶段仅注意满足以下所有条件(同时)的像素。 (0)像素是黑色的,有八个邻居 (1)$ 2 <= B(P1)<= 6 $ (2)$ A(P1)= 1 $ (3)P2和P4和P6中的至少一个是白色 (4)P4和P6和P8中的至少一个是白色 在迭代图像并收集满足所有步骤1条件的所有像​​素之后,将满足像素的所有这些条件设置为白色。

第2步:

再次测试所有像素,并且在此阶段仅注意满足以下所有条件的像素。 (0)像素是黑色的,有八个邻居 (1)$ 2 <= B(P1)<= 6 $ (2)$ A(P1)= 1 $ (3)P2和P4中的至少一个和“'P8”'是白色的 (4)“'P2”'和P6和P8中的至少一个是白色 在迭代图像并收集满足所有步骤2条件的所有像​​素之后,将满足像素的所有这些条件再次设置为白色。 迭代: 如果在该轮步骤1或步骤2中设置了任何像素,则重复所有步骤直到没有图像像素如此改变。

任务: 编写一个例程,在1和0的图像矩阵上执行Zhang-Suen细化。

## Instructions
## Tests
```yml tests: - text: thinImage必须是一个函数 testString: 'assert.equal(typeof thinImage, "function", "thinImage must be a function");' - text: thinImage必须返回一个数组 testString: 'assert(Array.isArray(result), "thinImage must return an array");' - text: thinImage必须返回一个字符串数组 testString: 'assert.equal(typeof result[0], "string", "thinImage must return an array of strings");' - text: thinImage必须返回一个字符串数组 testString: 'assert.deepEqual(result, expected, "thinImage must return an array of strings");' ```
## Challenge Seed
```js const testImage = [ ' ', ' ################# ############# ', ' ################## ################ ', ' ################### ################## ', ' ######## ####### ################### ', ' ###### ####### ####### ###### ', ' ###### ####### ####### ', ' ################# ####### ', ' ################ ####### ', ' ################# ####### ', ' ###### ####### ####### ', ' ###### ####### ####### ', ' ###### ####### ####### ###### ', ' ######## ####### ################### ', ' ######## ####### ###### ################## ###### ', ' ######## ####### ###### ################ ###### ', ' ######## ####### ###### ############# ###### ', ' ']; function thinImage(image) { // Good luck! } ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```