nx_areola_localize_test.nx source
↩ module page · 70 lines · 2412 B
1import "nx_syscalls.nx"
2import "nx_image.nx"
3import "nx_areola_localize.nx"
4
5func paint_rect(rgb: *Image, x0: i64, y0: i64, x1: i64, y1: i64,
6 r: i64, g: i64, b: i64) -> i64 {
7 var y: i64 = y0
8 while y < y1 {
9 var x: i64 = x0
10 while x < x1 {
11 if x >= 0 { if x < rgb.width { if y >= 0 { if y < rgb.height {
12 nx_image_set(rgb, x, y, 0, r)
13 nx_image_set(rgb, x, y, 1, g)
14 nx_image_set(rgb, x, y, 2, b)
15 } } } }
16 x = x + 1
17 }
18 y = y + 1
19 }
20 return 0
21}
22
23func main() -> i64 {
24 let w: i64 = 200
25 let h: i64 = 200
26 let rgb: *Image = nx_image_alloc(w, h, 3)
27 // Breast bbox 50..150, 30..130 (100x100). Skin tone R=200 G=170 B=160 (luminance ~178).
28 paint_rect(rgb, 50, 30, 150, 130, 200, 170, 160)
29 // Areola at center, 88..112 x 70..90 (24x20). Darker pigment R=140 G=100 B=95 (luminance ~110).
30 paint_rect(rgb, 88, 70, 112, 90, 140, 100, 95)
31
32 let bbox: *i64 = (sys_mmap(4 * 8 + 16)) as *i64
33 bbox[0] = 50; bbox[1] = 30; bbox[2] = 149; bbox[3] = 129
34
35 let result: *i64 = (sys_mmap(NX_AREOLAL_RES_FIELDS * 8 + 16)) as *i64
36 nx_areola_localize(rgb, bbox, result)
37
38 // T1: must find darker region.
39 let area: i64 = result[NX_AREOLAL_RES_AREA]
40 if area < 200 { return 1 }
41 if area > 700 { return 2 } // should be ~480 px (24x20)
42
43 // T2: bbox should approximately match painted region.
44 let ax0: i64 = result[NX_AREOLAL_RES_X0]
45 let ax1: i64 = result[NX_AREOLAL_RES_X1]
46 if ax0 < 80 { return 10 }
47 if ax0 > 95 { return 11 }
48 if ax1 < 105 { return 12 }
49 if ax1 > 120 { return 13 }
50
51 // T3: centroid should be near (100, 80).
52 let cx: i64 = result[NX_AREOLAL_RES_CENTROID_X]
53 let cy: i64 = result[NX_AREOLAL_RES_CENTROID_Y]
54 if cx < 95 { return 20 }
55 if cx > 105 { return 21 }
56 if cy < 75 { return 22 }
57 if cy > 85 { return 23 }
58
59 // T4: build the areola mask via nx_areola_build_mask.
60 let mask: *Image = nx_image_alloc(w, h, 1)
61 nx_areola_build_mask(rgb, bbox, mask)
62 // Inside areola: mask should be 255 at (100, 80).
63 let m_inside: i64 = nx_image_get(mask, 100, 80, 0)
64 if m_inside != 255 { return 30 }
65 // Outside areola but within breast: mask should be 0 at (60, 40).
66 let m_outside: i64 = nx_image_get(mask, 60, 40, 0)
67 if m_outside != 0 { return 31 }
68
69 return 0
70}