freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/rosetta-code/zhang-suen-thinning-algorit...

5.2 KiB
Raw Blame History

title id challengeType videoUrl localeTitle
Zhang-Suen thinning algorithm 594810f028c0303b75339ad7 5 张素细化算法

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 <= BP1<= 6 $ 2$ AP1= 1 $ 3P2和P4和P6中的至少一个是白色 4P4和P6和P8中的至少一个是白色 在迭代图像并收集满足所有步骤1条件的所有像素之后将满足像素的所有这些条件设置为白色。

第2步

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

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

Instructions

Tests

tests:
  - text: <code>thinImage</code>必须是一个函数
    testString: 'assert.equal(typeof thinImage, "function", "<code>thinImage</code> must be a function");'
  - text: <code>thinImage</code>必须返回一个数组
    testString: 'assert(Array.isArray(result), "<code>thinImage</code> must return an array");'
  - text: <code>thinImage</code>必须返回一个字符串数组
    testString: 'assert.equal(typeof result[0], "string", "<code>thinImage</code> must return an array of strings");'
  - text: <code>thinImage</code>必须返回一个字符串数组
    testString: 'assert.deepEqual(result, expected, "<code>thinImage</code> must return an array of strings");'

Challenge Seed

const testImage = [
  '                                                          ',
  ' #################                   #############        ',
  ' ##################               ################        ',
  ' ###################            ##################        ',
  ' ########     #######          ###################        ',
  '   ######     #######         #######       ######        ',
  '   ######     #######        #######                      ',
  '   #################         #######                      ',
  '   ################          #######                      ',
  '   #################         #######                      ',
  '   ######     #######        #######                      ',
  '   ######     #######        #######                      ',
  '   ######     #######         #######       ######        ',
  ' ########     #######          ###################        ',
  ' ########     ####### ######    ################## ###### ',
  ' ########     ####### ######      ################ ###### ',
  ' ########     ####### ######         ############# ###### ',
  '                                                          '];

function thinImage(image) {
  // Good luck!
}

After Test

console.info('after the test');

Solution

// solution required