nx_image_feature_extract.nx source
↩ module page · 161 lines · 7574 B
1// nx_image_feature_extract.nx -- fixed-length feature vector emitter.
2//
3// Composes existing image-quality detectors (nx_aesthetics +
4// nx_self_similarity + nx_symmetry_group) into a 16-element Q10
5// feature vector. Pair with nx_cosine_similarity for image-to-image
6// comparison, concept-embedding lookup, clustering, recommendation.
7//
8// FOUNDATION FOR THE "AI GROWS BY INGESTING IMAGE" ROADMAP:
9// user uploads N images of a new outfit
10// -> nx_image_feature_extract on each -> N feature vectors
11// -> average them (centroid) -> "outfit_X" concept embedding
12// -> store as content-addressed manifest in nishi-library
13// later prompt mentions "outfit_X"
14// -> blend stored embedding into prompt embedding (IP-adapter path)
15// -> render conditioned on the new concept
16// No LoRA training, no base-model fine-tune. The substrate GROWS.
17//
18// Idea-provenance (patent-clean):
19// CLIP image-encoder pattern (Radford 2021)
20// IP-Adapter (Ye 2023)
21// Textual Inversion (Gal 2022)
22// The architecture absorbs the IDEA -- fixed-length embedding per
23// image -- without copying any third-party code. Today's
24// features are substrate-native (aesthetics + fractal +
25// multi-axis symmetry) rather than CLIP-encoder weights.
26//
27// VECTOR LAYOUT (each Q10):
28// [ 0] COLOR_HARMONY hue distribution narrowness
29// [ 1] LIGHTING exposure / range / clipping composite
30// [ 2] COMPOSITION thirds + symmetry + balance composite
31// [ 3] CLARITY salience peakedness
32// [ 4] SHARPNESS mean Sobel magnitude
33// [ 5] AESTHETIC_COMPOSITE mean of [0..4]
34// [ 6] FRACTAL_DIM box-counting Q10 of (D-1) for D in [1, 2]
35// [ 7] FRACTAL_AESTHETIC Spehar 2003 D~1.4 peak-fit
36// [ 8] EDGE_DENSITY Sobel-thresh fraction
37// [ 9] SYM_COMPOSITE max across 7 symmetry axes
38// [10] SYM_H horizontal bilateral
39// [11] SYM_V vertical bilateral
40// [12] SYM_DIAG diagonal mirror
41// [13] SYM_ROT_180 180-deg rotational
42// [14] SYM_N_AXES_PRESENT how many symmetry axes >= 70% threshold
43// [15] SYM_TRANS_PERIOD translational periodic best-period
44//
45// Substrate cardinal: this vector is the IMAGE's signature. Two
46// images with similar substrate signatures should look "alike" in
47// the broad-aesthetic sense; nx_cosine_similarity over these
48// vectors operationalizes "looks like" mathematically.
49//
50// genealogy_id: salton_1971_smart + radford_2021_clip +
51// gal_2022_textual_inversion + ye_2023_ip_adapter +
52// birkhoff_1933_aesthetic_measure +
53// spehar_2003_universal_aesthetic_fractals
54// lineage_id: image_feature_vector_q10_composite
55
56// nx_safety_envelope:
57// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
58// sil_target: SIL1
59// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
60// verdict: NOT_YET_EVALUATED
61
62import "nx_syscalls.nx"
63import "nx_tier.nx"
64import "nx_image.nx"
65import "nx_aesthetics.nx"
66import "nx_self_similarity.nx"
67import "nx_symmetry_group.nx"
68
69// Feature vector length. Power of 2 for downstream-friendly layouts
70// (cache lines, SIMD width when nx_simd lands, etc.).
71const NX_IMAGE_FEATURE_LEN: nx_int = 16
72
73// Named indices into the feature vector for downstream clarity.
74const NX_IMG_FEAT_COLOR_HARMONY: nx_int = 0
75const NX_IMG_FEAT_LIGHTING: nx_int = 1
76const NX_IMG_FEAT_COMPOSITION: nx_int = 2
77const NX_IMG_FEAT_CLARITY: nx_int = 3
78const NX_IMG_FEAT_SHARPNESS: nx_int = 4
79const NX_IMG_FEAT_AESTHETIC_COMPOSITE: nx_int = 5
80const NX_IMG_FEAT_FRACTAL_DIM: nx_int = 6
81const NX_IMG_FEAT_FRACTAL_AESTHETIC: nx_int = 7
82const NX_IMG_FEAT_EDGE_DENSITY: nx_int = 8
83const NX_IMG_FEAT_SYM_COMPOSITE: nx_int = 9
84const NX_IMG_FEAT_SYM_H: nx_int = 10
85const NX_IMG_FEAT_SYM_V: nx_int = 11
86const NX_IMG_FEAT_SYM_DIAG: nx_int = 12
87const NX_IMG_FEAT_SYM_ROT_180: nx_int = 13
88const NX_IMG_FEAT_SYM_N_AXES_PRESENT: nx_int = 14
89const NX_IMG_FEAT_SYM_TRANS_PERIOD: nx_int = 15
90
91// ===== Public emitter =================================================
92//
93// rgb 3-channel image (color features)
94// gray 1-channel image (composition / sharpness / fractal)
95// out caller-allocated nx_int array of length NX_IMAGE_FEATURE_LEN
96//
97// Returns: overall fidelity Q10 (mean of upstream detector fidelities
98// where available; 0 = totally untrustworthy, Q = full confidence).
99// Caller refuses to act on the feature vector below a chosen
100// fidelity floor (honest-perf-verdict cardinal).
101
102func nx_image_feature_extract(rgb: *Image, gray: *Image, out: *nx_int) -> nx_int {
103 // ---- Aesthetics composite (5 axes + composite) ----
104 let aest: *AestheticsReport = (sys_mmap(8 * NX_SIZEOF_NX_INT)) as *AestheticsReport
105 nx_aesthetics_compute(rgb, gray, aest)
106 out[NX_IMG_FEAT_COLOR_HARMONY] = aest.color_harmony
107 out[NX_IMG_FEAT_LIGHTING] = aest.lighting_quality
108 out[NX_IMG_FEAT_COMPOSITION] = aest.composition
109 out[NX_IMG_FEAT_CLARITY] = aest.subject_clarity
110 out[NX_IMG_FEAT_SHARPNESS] = aest.sharpness
111 out[NX_IMG_FEAT_AESTHETIC_COMPOSITE] = aest.composite
112
113 // ---- Self-similarity (fractal dim + Spehar fit + edge density) ----
114 let ss: *SelfSimilarityReport = (sys_mmap(4 * NX_SIZEOF_NX_INT)) as *SelfSimilarityReport
115 nx_self_similarity_compute(gray, ss)
116 out[NX_IMG_FEAT_FRACTAL_DIM] = ss.fractal_dim_q10
117 out[NX_IMG_FEAT_FRACTAL_AESTHETIC] = ss.aesthetic_q10
118 out[NX_IMG_FEAT_EDGE_DENSITY] = ss.edge_density_q10
119
120 // ---- Symmetry (multi-axis + composite) ----
121 let sym: *SymmetryReport = (sys_mmap(12 * NX_SIZEOF_NX_INT)) as *SymmetryReport
122 nx_symmetry_compute(gray, sym)
123 out[NX_IMG_FEAT_SYM_COMPOSITE] = sym.composite_q10
124 out[NX_IMG_FEAT_SYM_H] = sym.h_bilateral_q10
125 out[NX_IMG_FEAT_SYM_V] = sym.v_bilateral_q10
126 out[NX_IMG_FEAT_SYM_DIAG] = sym.diagonal_q10
127 out[NX_IMG_FEAT_SYM_ROT_180] = sym.rot_180_q10
128 out[NX_IMG_FEAT_SYM_N_AXES_PRESENT] = sym.n_axes_present
129 out[NX_IMG_FEAT_SYM_TRANS_PERIOD] = sym.translation_period
130
131 // ---- Fidelity (substrate-side honest signal) ----
132 // Self-similarity has explicit fidelity_q10; aesthetics and
133 // symmetry don't carry one yet so we conservatively gate by
134 // the self-similarity reading. When upstream detectors widen
135 // their reports, this composes the mean.
136 return ss.fidelity_q10
137}
138
139// ===== Convenience: feature-vector centroid for N images =============
140//
141// Computes the per-axis mean of N feature vectors into out_centroid.
142// Used to derive a "concept embedding" from N example images: average
143// their features, normalize, store as the concept's canonical
144// signature.
145
146func nx_image_feature_centroid(vectors: *nx_int, n_vectors: nx_int,
147 out_centroid: *nx_int) -> nx_int {
148 if n_vectors <= 0 { return 0 }
149 var axis: nx_int = 0
150 while axis < NX_IMAGE_FEATURE_LEN {
151 var sum: nx_int = 0
152 var i: nx_int = 0
153 while i < n_vectors {
154 sum = sum + vectors[i * NX_IMAGE_FEATURE_LEN + axis]
155 i = i + 1
156 }
157 out_centroid[axis] = sum / n_vectors
158 axis = axis + 1
159 }
160 return 0
161}