## 2 Materials and Methods
### Spectra Processing  
During each acquisition, three datasets are generated by the system: (i) the raw data cube (RD) with the intensities of measured light from the scanned target; (ii) a dark frame (D) captured without illumination to remove the sensor noise (temperature and integration time dependent) and bad pixels (e.g., hot pixels); (iii) a white (W) reference frame, captured from the scanned area of the white reference (WR) tile, to take into account the spectrum of the illumination source for reflectance computation. The white frame not only contains information on the signal of the source but also on the response of the camera system (optics, sensor, electronics) and how it affects the measured spectrum. In theory, the WR tile has a reflectance of 100% without any spectral features. However, in practice, the presence of small deviations from this ideal value will introduce perturbations in the processing of  
Figure 4: Simplified version of the hypercubes generated during the HSI acquisition. Each layer (XY plane) corresponds to spatial data for a specific band, characterized by its central wavelength (\(\lambda\)). A hyper-pixel, illustrated as red squares, is a vector whose components represent the reflectance values associated with each band, forming a spectrum.  
Figure 3: (a) Wheat flour and (b) BSF larvae meal, with the (c) white reference disposed on a Teflon® sheet, during HSI acquisition in simultaneous mode. The white reference is used by the acquisition system to convert the raw measurements into reflectance (a spectral range calibration check with a valid result is required).  
the raw data cube. The reflectance computation is performed according to the following equation [20]:  
\[R=\frac{RD(t_{1})-D(t_{1})}{W(t_{2})-D(t_{2})}\times\frac{t_{2}}{t_{1}} \tag{1}\]  
where \(t_{k}\) (\(k=1\), 2) refers to the integration times to acquire the data cubes. During this study, the WR tile was always present during the data acquisition (simultaneous mode), and both times are equal.  
HSI acquisition was conducted using samples from the same batch of wheat flour and BSF larvae meal as those used for the analytical characterization in order to have comparable results. To define the representative spectra of each sample, a python script was created in order to perform all the computations. The process involves the following steps:  
1. Normalization of the bands to allow pixel intensity comparison between different hypercubes and definition of a Region Of Interest (ROI) within the sample (Figure 5);
2. Computation of the mean spectra of the pixels inside the ROI, for noise reduction and increase representativeness (Figure 6).  
Figure 5: Band 200 (\(\lambda=994.31\) nm) of the wheat flour (**a**) and BSF meal (**b**), after normalization, with a 40 \(\times\) 30 pixels ROI (cyan rectangle) used for mean spectrum computation.  
Figure 6: Mean spectrum inside the ROI computed with 1200 pixels (pale blue lines), for the wheat flour (**a**) and the BSF larvae flour (**b**) samples.  
3. Mean spectrum pre-processing (Figure 7): 1. Computation of the Standard Normal Variate to reduce variability introduced by particle size, surface scattering effects, and correct baseline shifts [23]; 2. Determination of quantitative similarity metrics for the SNV-transformed spectra of each sample: Spectral Angle Mapper (SAM) [24], and Pearson Coefficient of Correlation; 3. Application of the Savitzky-Golay filter with a 7-points window and a 2 nd order polynomial, for noise reduction, and 2 nd derivative computation (SGd2) to highlight spectral features, namely, absorption peaks not visible in the raw spectrum [25].