code wiki / (root) / nx_lbp_grid.nx

nx_lbp_grid.nx source

↩ module page · 58 lines · 4160 B

1// nx_lbp_grid.nx -- SOVEREIGN face-descriptor RUNG 3 GATE: spatial-grid LBP descriptor. Splits a normalized 2// image into g x g cells, each a 256-bin LBP histogram, packed into one descriptor -> keeps WHERE texture 3// occurs (eyes/nose/mouth regions), which a single global histogram throws away. Proven with a spatial- 4// LOCALITY KAT (a localized pixel change leaves a DISTANT cell's histogram bit-identical) and a real-image 5// resize->grid->distance check (self=0, distinct>0). SAME-vs-DIFFERENT-person recall (vs the insightface 6// benchmark) is the next rung. Build/run: ./_offc/nx_sov_build_run.elf nx_lbp_grid license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_lbp_core.nx" 9import "nx_lbp_imgio.nx" 10 11func fill_uniform(img: *u8, n: i64, val: i64) -> i64 { var i: i64=0; while i<n { img[i]=val as u8; i=i+1 } return 0 } 12func copybuf(dst: *u8, src: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[i]=src[i]; i=i+1 } return 0 } 13func zeroN(p: *i64, n: i64) -> i64 { var i: i64=0; while i<n { p[i]=0; i=i+1 } return 0 } 14func sumN(p: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { s=s+p[i]; i=i+1 } return s } 15 16func main() -> i64 { 17 var fail: i64 = 0 18 19 // ---- spatial-locality KAT: 16x16 uniform, g=4 ---- 20 let W: i64=16; let H: i64=16; let G: i64=4 21 let DN: i64 = G*G*256 22 let a: *u8 = sys_mmap(W*H); fill_uniform(a, W*H, 128) 23 let da: *i64 = sys_mmap(8*DN) as *i64; zeroN(da, DN); lbp_grid_descr(a, W, H, G, da) 24 let interior: i64 = (W-2)*(H-2) 25 lp("KAT grid descr sum="); ln(sumN(da, DN)); lp(" interior="); ln(interior); lp(" -> ") 26 if sumN(da, DN) == interior { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } 27 28 // localized change inside cell (1,1): pixels [4,8) x [4,8) 29 let b: *u8 = sys_mmap(W*H); copybuf(b, a, W*H) 30 var yy: i64=4; while yy<8 { var xx: i64=4; while xx<8 { b[yy*W+xx]=(((xx-4)+(yy-4))*30) as u8; xx=xx+1 } yy=yy+1 } 31 let db: *i64 = sys_mmap(8*DN) as *i64; zeroN(db, DN); lbp_grid_descr(b, W, H, G, db) 32 let cell_far: i64 = (3*G+3)*256 // cell (3,3) far corner -- must be untouched 33 let cell_chg: i64 = (1*G+1)*256 // cell (1,1) -- must change 34 let dfar: i64 = chi2_range(da, db, cell_far, 256, 1000) 35 let dchg: i64 = chi2_range(da, db, cell_chg, 256, 1000) 36 lp("LOCALITY far-cell chi2="); ln(dfar); lp(" (expect 0) changed-cell chi2="); ln(dchg); lp(" (expect >0) -> ") 37 if dfar==0 { if dchg>0 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } } else { lp("FAIL\n" as *u8); fail=1 } 38 39 // ---- real images: resize 128x128, g=8 spatial-grid descriptor ---- 40 let N: i64=128; let G2: i64=8; let DN2: i64=G2*G2*256 41 let pA: *u8 = "/mnt/c/Users/elder/elder-ai-platform/data-vault/images/2026-06-14/20260614_000018_310470d0_3787058372_laptop.png" as *u8 42 let pB: *u8 = "/mnt/c/Users/elder/elder-ai-platform/data-vault/images/2026-06-14/20260614_000036_cfd6442e_2031691026_laptop.png" as *u8 43 let ga: *u8 = sys_mmap(N*N); let gb: *u8 = sys_mmap(N*N) 44 if load_gray_fixed(pA, N, N, ga) != 0 { lp("RUNG3 RED: A decode\n" as *u8); sys_exit(1); return 1 } 45 if load_gray_fixed(pB, N, N, gb) != 0 { lp("RUNG3 RED: B decode\n" as *u8); sys_exit(1); return 1 } 46 let dA: *i64 = sys_mmap(8*DN2) as *i64; zeroN(dA, DN2); lbp_grid_descr(ga, N, N, G2, dA) 47 let dB: *i64 = sys_mmap(8*DN2) as *i64; zeroN(dB, DN2); lbp_grid_descr(gb, N, N, G2, dB) 48 let int2: i64 = (N-2)*(N-2) 49 lp("real grid(8x8) descr sum="); ln(sumN(dA, DN2)); lp(" interior="); ln(int2); lp(" -> ") 50 if sumN(dA, DN2)==int2 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } 51 let self2: i64 = grid_chi2(dA, dA, G2, 1000) 52 let ab2: i64 = grid_chi2(dA, dB, G2, 1000) 53 lp("grid chi2(A,A)="); ln(self2); lp(" (expect 0) chi2(A,B)="); ln(ab2); lp(" (expect >0) -> ") 54 if self2==0 { if ab2>0 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } } else { lp("FAIL\n" as *u8); fail=1 } 55 56 if fail==0 { lp("nx_lbp_grid RUNG3 GREEN (spatial-grid descriptor: locality KAT + real-image self=0/distinct>0)\n" as *u8); sys_exit(0); return 0 } 57 lp("nx_lbp_grid RUNG3 RED\n" as *u8); sys_exit(1); return 1 58}