code wiki / (root) / nx_lbp.nx

nx_lbp.nx source

↩ module page · 58 lines · 3546 B

1// nx_lbp.nx -- SOVEREIGN face-descriptor RUNG 1 GATE (hardware-up, integer-only). Proves the LBP operator + 2// histogram + chi-square distance (in nx_lbp_core.nx) with a BIT-EXACT KAT and a same<different neg-control. 3// LBP is weight-free + illumination-invariant by construction -> a capability we reproduce from first 4// principles. BENCHMARK (measuring-stick ONLY): insightface ArcFace 512-d embeddings. 5// Build/run: ./_offc/nx_sov_build_run.elf nx_lbp license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_lbp_core.nx" 8 9func 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 } 10func fill_gradient(img: *u8, w: i64, h: i64, step: i64) -> i64 { 11 var y: i64=0; while y<h { var x: i64=0; while x<w { img[y*w+x]=(x*step) as u8; x=x+1 } y=y+1 } return 0 12} 13 14func main() -> i64 { 15 var fail: i64 = 0 16 17 // ---- KAT1: one hand-computed 3x3 patch -> known code ---- 18 // center=50; neighbors TL,T,TR,R,BR,B,BL,L = 10,20,30,90,80,70,60,40 19 // >=50 ? TL0 T0 TR0 R1 BR1 B1 BL1 L0 -> 8+16+32+64 = 120 20 let p: *u8 = sys_mmap(9) 21 p[0]=10 as u8; p[1]=20 as u8; p[2]=30 as u8 22 p[3]=40 as u8; p[4]=50 as u8; p[5]=90 as u8 23 p[6]=60 as u8; p[7]=70 as u8; p[8]=80 as u8 24 let code1: i64 = lbp_code(p, 3, 1, 1) 25 lp("KAT1 lbp_code=" as *u8); ln(code1); lp(" expect=120 -> " as *u8) 26 if code1 == 120 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail = 1 } 27 28 // ---- KAT2: 6x6 uniform -> every interior pixel flat -> code 255 x16 ---- 29 let imgU: *u8 = sys_mmap(36); fill_uniform(imgU, 36, 128) 30 let hU: *i64 = sys_mmap(8*256) as *i64; zero256(hU); lbp_hist(imgU, 6, 6, hU) 31 lp("KAT2 uniform hist[255]=" as *u8); ln(hU[255]); lp(" sum=" as *u8); ln(hsum(hU)); lp(" expect 16/16 -> " as *u8) 32 if hU[255]==16 { if hsum(hU)==16 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } } else { lp("FAIL\n" as *u8); fail=1 } 33 34 // ---- intensity invariance: a brighter flat image -> identical descriptor (chi2==0) ---- 35 let imgU2: *u8 = sys_mmap(36); fill_uniform(imgU2, 36, 200) 36 let hU2: *i64 = sys_mmap(8*256) as *i64; zero256(hU2); lbp_hist(imgU2, 6, 6, hU2) 37 let dUU2: i64 = chi2(hU, hU2, 1000) 38 lp("INV chi2(flat128,flat200)=" as *u8); ln(dUU2); lp(" expect 0 -> " as *u8) 39 if dUU2==0 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } 40 41 // ---- structure distance: a gradient image differs from flat (chi2>0) ---- 42 let imgC: *u8 = sys_mmap(36); fill_gradient(imgC, 6, 6, 40) 43 let hC: *i64 = sys_mmap(8*256) as *i64; zero256(hC); lbp_hist(imgC, 6, 6, hC) 44 let dUC: i64 = chi2(hU, hC, 1000) 45 lp("STRUCT chi2(flat,gradient)=" as *u8); ln(dUC); lp(" expect >0 -> " as *u8) 46 if dUC > 0 { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } 47 48 // ---- ordering neg-control: a near-copy of the gradient is CLOSER than the flat image ---- 49 let imgC2: *u8 = sys_mmap(36); fill_gradient(imgC2, 6, 6, 40); imgC2[2*6+3]=255 as u8 50 let hC2: *i64 = sys_mmap(8*256) as *i64; zero256(hC2); lbp_hist(imgC2, 6, 6, hC2) 51 let dCC2: i64 = chi2(hC, hC2, 1000) 52 let dCU: i64 = chi2(hC, hU, 1000) 53 lp("ORDER chi2(grad,grad')=" as *u8); ln(dCC2); lp(" chi2(grad,flat)=" as *u8); ln(dCU); lp(" expect near<far -> " as *u8) 54 if dCC2 < dCU { lp("PASS\n" as *u8) } else { lp("FAIL\n" as *u8); fail=1 } 55 56 if fail==0 { lp("nx_lbp RUNG1 GREEN (KAT bit-exact + invariance + same<different)\n" as *u8); sys_exit(0); return 0 } 57 lp("nx_lbp RUNG1 RED\n" as *u8); sys_exit(1); return 1 58}