code wiki / (root) / nx_region_test.nx

nx_region_test.nx source

↩ module page · 64 lines · 2241 B

1// nx_region_test.nx -- smoke for typed body-region segmentation. 2 3import "nx_syscalls.nx" 4import "nx_image.nx" 5import "nx_region.nx" 6 7func fill_rect_skin(img: *Image, x0: i64, y0: i64, x1: i64, y1: i64) -> i64 { 8 var y: i64 = y0 9 while y < y1 { 10 var x: i64 = x0 11 while x < x1 { 12 nx_image_set(img, x, y, 0, 200) 13 nx_image_set(img, x, y, 1, 150) 14 nx_image_set(img, x, y, 2, 130) 15 x = x + 1 16 } 17 y = y + 1 18 } 19 return 0 20} 21 22func fill_black(img: *Image) -> i64 { 23 var y: i64 = 0 24 while y < img.height { 25 var x: i64 = 0 26 while x < img.width { 27 nx_image_set(img, x, y, 0, 0); nx_image_set(img, x, y, 1, 0); nx_image_set(img, x, y, 2, 0) 28 x = x + 1 29 } 30 y = y + 1 31 } 32 return 0 33} 34 35func main() -> i64 { 36 // T1: empty -> only BACKGROUND 37 let img1: *Image = nx_image_alloc(64, 64, 3) 38 fill_black(img1) 39 let s1: *RegionSet = nx_region_segment(img1) 40 if nx_region_count_kind(s1, NX_REGION_TORSO) != 0 { return 1 } 41 if nx_region_count_kind(s1, NX_REGION_BACKGROUND) != 1 { return 2 } 42 43 // T2: 25x25 torso -> TORSO + FACE + BACKGROUND 44 let img2: *Image = nx_image_alloc(64, 64, 3) 45 fill_black(img2) 46 fill_rect_skin(img2, 20, 20, 45, 45) 47 let s2: *RegionSet = nx_region_segment(img2) 48 if nx_region_count_kind(s2, NX_REGION_TORSO) != 1 { return 10 } 49 if nx_region_count_kind(s2, NX_REGION_FACE) != 1 { return 11 } 50 if nx_region_count_kind(s2, NX_REGION_BACKGROUND) != 1 { return 12 } 51 let t_idx: i64 = nx_region_find_kind(s2, NX_REGION_TORSO) 52 if nx_region_get_field(s2, t_idx, NX_REGION_F_AREA_PX) < 600 { return 13 } 53 54 // T3: torso + attached limb -> TORSO + FACE + LIMB + BACKGROUND 55 let img3: *Image = nx_image_alloc(64, 64, 3) 56 fill_black(img3) 57 fill_rect_skin(img3, 20, 20, 45, 45) 58 fill_rect_skin(img3, 46, 20, 58, 29) 59 let s3: *RegionSet = nx_region_segment(img3) 60 if nx_region_count_kind(s3, NX_REGION_TORSO) != 1 { return 20 } 61 if nx_region_count_kind(s3, NX_REGION_LIMB) != 1 { return 21 } 62 63 return 0 64}