Rapid detection method and system for food pesticide residues based on multispectral imaging

By employing multispectral imaging technology correction, variational autoencoder, Mahalanobis distance filtering, and collaborative attention tensor processing, the problems of matrix background interference and weak residual signals in multispectral imaging detection were solved, achieving high sensitivity and high accuracy detection of pesticide residues in food.

CN121595543BActive Publication Date: 2026-04-07TIANJIN INST OF FOOD SAFETY TESTING TECH
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-01-30
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

Existing multispectral imaging detection methods for detecting pesticide residues in food suffer from problems such as strong matrix background interference, weak residue signals, insufficient detection sensitivity, low positioning accuracy, and poor robustness.

Method used

By acquiring and correcting multispectral images, a latent feature vector is extracted using a variational autoencoder. The candidate regions for pesticide residues are then screened using Mahalanobis distance, generating spatial and spectral attention maps. A collaborative attention tensor is constructed, and element-wise multiplication is performed to enhance image features. The resulting data is then input into a regression network to predict pesticide residue concentrations.

Benefits of technology

It improves the accuracy and generalization ability of pesticide residue detection, significantly enhances the sensitivity of detection and its adaptability to different food varieties, and ensures the accuracy and reliability of detection results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121595543B_ABST
    Figure CN121595543B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of spectral analysis detection, and discloses a food pesticide residue rapid detection method and system based on multispectral imaging. The method comprises the following steps: collecting a multispectral image and correcting to obtain a reflectivity image cube; inputting a variational autoencoder to encode to obtain a latent feature vector; calculating Mahalanobis distance after normalization to screen a residue candidate area, generating spatial and spectral attention weights and performing outer product to construct a collaborative attention tensor; multiplying the reflectivity image element by element to obtain an enhanced image, and extracting features to input a regression network to predict a pesticide residue concentration. The application improves the accuracy and generalization ability of pesticide residue detection under different food varieties.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of spectral analysis detection, in particular to a food pesticide residue rapid detection method and system based on multi-spectral imaging. BACKGROUND

[0002] Food pesticide residue detection is an important link to ensure food safety. Traditional pesticide residue detection methods mainly include gas chromatography, liquid chromatography-mass spectrometry and other chemical analysis techniques. Although these methods have high accuracy, they have problems such as complex sample pretreatment, long detection period and high cost, which are difficult to meet the needs of on-site rapid detection. In recent years, non-destructive detection methods based on spectral imaging technology have gradually become a research hotspot. Multi-spectral imaging technology acquires the reflection or transmission images of food samples at multiple specific wavebands, and establishes the quantitative relationship between spectral characteristics and pesticide residue concentration by combining chemometrics methods, which has the advantages of non-destructive, rapid and non-contact, and shows good application prospects in food safety detection field.

[0003] However, the existing multi-spectral imaging detection method still has many deficiencies. First, the multi-spectral imaging system is affected by dark current noise and light source non-uniformity during acquisition, resulting in system errors in the original image. If not corrected effectively, it will directly affect the accuracy of subsequent analysis. Second, the food matrix composition is complex and diverse, and the spectral characteristics of different foods are significantly different. The spectral signal of pesticide residue is often overwhelmed by strong matrix background signal, resulting in insufficient detection sensitivity. In addition, the existing method usually uses global feature extraction strategy, which cannot effectively distinguish the residual area and matrix background area on the food surface, resulting in a large amount of redundant information participating in modeling, reducing the robustness and generalization ability of the model. SUMMARY

[0004] The present application provides a food pesticide residue rapid detection method and system based on multi-spectral imaging, which solves the problems of insufficient detection sensitivity caused by strong matrix background interference and weak residual signal in multi-spectral imaging food pesticide residue detection, low positioning accuracy caused by insufficient use of spatial and spectral information, and poor robustness caused by unreasonable multi-scale feature fusion, and improves the accuracy and generalization ability of pesticide residue detection under different food varieties.

[0005] In a first aspect, the present application provides a food pesticide residue rapid detection method based on multi-spectral imaging, which comprises:

[0006] Step S1: acquiring multi-spectral images of the food sample to be detected and performing correction processing to obtain a reflectance image cube;

[0007] Step S2: input the reflectance image cube into a variational autoencoder for encoding to obtain a latent feature vector;

[0008] Step S3: after normalization processing of the latent feature vector, calculate Mahalanobis distance with a global matrix feature center, screen a residual candidate region from pixel positions with Mahalanobis distance exceeding a preset threshold, generate a spatial attention map based on spatial distribution of the residual candidate region, generate spectral attention weights based on band responses of the reflectance image cube, and construct a collaborative attention tensor through outer product operation of the spatial attention map and the spectral attention weights;

[0009] Step S4: perform element-wise multiplication operation on the reflectance image cube and the collaborative attention tensor to obtain an enhanced image, extract features of the enhanced image and input into a regression network to obtain a pesticide residue concentration prediction value.

[0010] In a second aspect, the present application provides a food pesticide residue rapid detection system based on multispectral imaging, which comprises:

[0011] A correction module is configured to acquire a multispectral image of a food sample to be detected and perform correction processing to obtain a reflectance image cube;

[0012] An encoding module is configured to input the reflectance image cube into a variational autoencoder for encoding to obtain a latent feature vector;

[0013] A screening module is configured to perform normalization processing on the latent feature vector, calculate Mahalanobis distance with a global matrix feature center, screen a residual candidate region from pixel positions with Mahalanobis distance exceeding a preset threshold, generate a spatial attention map based on spatial distribution of the residual candidate region, generate spectral attention weights based on band responses of the reflectance image cube, and construct a collaborative attention tensor through outer product operation of the spatial attention map and the spectral attention weights;

[0014] A prediction module is configured to perform element-wise multiplication operation on the reflectance image cube and the collaborative attention tensor to obtain an enhanced image, extract features of the enhanced image and input into a regression network to obtain a pesticide residue concentration prediction value.

[0015] In the technical scheme provided in the application, the reflectance image cube is obtained by collecting the multispectral image of the food sample to be detected and performing correction processing, the influence of dark current noise of the imaging system and non-uniformity of the light source on the image quality is effectively eliminated, the original gray value depending on the specific imaging condition is converted into a standardized reflectance value, a reliable data foundation is provided for subsequent analysis, and detection result fluctuation caused by system error is avoided. The reflectance image cube is input into a variational autoencoder to obtain a latent feature vector, the high-dimensional spectral data is compressed to a low-dimensional latent space through nonlinear transformation of the encoder network, while the key spectral characteristics for distinguishing different food matrix types and pesticide residues are retained, the data dimension is reduced, and the separability of the features is enhanced, thereby providing compact and discriminative feature representation for subsequent anomaly detection.

[0016] The Mahalanobis distance from the global matrix feature center is calculated after the latent feature vector is normalized, the correlation and variance difference between the feature dimensions are fully considered, the standardization measurement of the feature space is realized through inverse transformation of the covariance matrix, the Mahalanobis distance can accurately reflect the real degree of deviation of the sample feature from the matrix feature center, the residual candidate region is obtained by screening the pixel positions with the Mahalanobis distance exceeding a preset threshold, accurate anomaly detection based on statistical characteristics is realized, and the pesticide residue area and the matrix background area are effectively distinguished. The spatial attention map is generated based on the spatial distribution of the residual candidate region, the texture and edge information of the residual area is extracted through a multi-scale convolution kernel, the spatial distribution characteristics of the pesticide residue on the food surface are accurately captured, the spectral attention weight is generated based on the band response of the reflectance image cube, the distinguishing ability of each band for pesticide residue detection is learned through the spectral attention network, the weight of the pesticide feature absorption band is adaptively strengthened, the synergistic attention tensor is constructed through the outer product operation of the spatial attention map and the spectral attention weight, the joint weighting of the spatial position importance and the band importance is realized, and each band of each pixel is assigned a refined weight value, both the spatial locality of the residue and the band selectivity of the spectrum are utilized, and the enhancement effect of the residue signal and the background suppression ability are significantly improved. BRIEF DESCRIPTION OF DRAWINGS

[0017] In order to more clearly illustrate the technical scheme of the embodiments of the application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are some embodiments of the application, and other drawings can be obtained by those skilled in the art without creative labor.

[0018] Figure 1 An embodiment schematic diagram of the food pesticide residue rapid detection method based on multispectral imaging in the embodiments of the application;

[0019] Figure 2This is a schematic diagram illustrating the residual candidate region screening based on Mahalanobis distance in an embodiment of this application;

[0020] Figure 3 This is a schematic diagram of the spectral attention weight distribution in an embodiment of this application. Detailed Implementation

[0021] This application provides a method and system for rapid detection of pesticide residues in food based on multispectral imaging. The terms "first," "second," "third," "fourth," etc. (if present) in the specification, claims, and accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments described herein can be implemented in a sequence other than that illustrated or described herein. Furthermore, the terms "comprising" or "having" and any variations thereof are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0022] For ease of understanding, the specific process of the embodiments of this application is described below. Please refer to [link / reference]. Figure 1 One embodiment of the rapid detection method for pesticide residues in food based on multispectral imaging in this application includes:

[0023] Step S1: Acquire multispectral images of the food sample to be tested and perform correction processing to obtain a reflectance image cube;

[0024] Specifically, the correction process addresses the image distortion issues caused by dark current noise and light source inhomogeneity during the acquisition process of multispectral imaging systems. Dark current correction eliminates thermal noise signals generated by CCD cameras under no-light conditions, while whiteboard calibration eliminates the effects of uneven light source intensity distribution and differences in optical system response. The original grayscale values ​​are converted into standardized reflectance values. The value of each pixel in the reflectance image cube in each band represents the proportion of light reflected at that location for that wavelength, with a value range between 0 and 1, thus eliminating systematic errors under different imaging conditions.

[0025] Step S2: Input the reflectance image cube into the variational autoencoder for encoding to obtain the latent feature vector;

[0026] The variational autoencoder compresses high-dimensional spectral vectors into a low-dimensional latent space through an encoder network. The latent mean vector and latent log-variance vector output by the encoder describe the probability distribution characteristics of spectral features in the latent space. The reparameterization operation converts the mean vector and log-variance vector into standard deviations, multiplies them with the standard normal distribution sample values, and adds the mean vector to obtain the latent feature vector. The dimension of the latent feature vector is much lower than that of the original spectral vector, but it retains the key spectral characteristics that distinguish different food matrix types. During training, the variational autoencoder uses reconstruction loss to ensure that the latent features can restore the original spectrum, and KL divergence constraints ensure that the latent space conforms to the standard normal distribution assumption.

[0027] Step S3: After normalizing the latent feature vector, calculate the Mahalanobis distance with the global matrix feature center. Select residual candidate regions by filtering the pixel positions where the Mahalanobis distance exceeds a preset threshold. Generate a spatial attention map based on the spatial distribution of the residual candidate regions. Generate spectral attention weights based on the band response of the reflectance image cube. Construct a co-attention tensor by performing an outer product operation between the spatial attention map and the spectral attention weights.

[0028] The normalization network performs cross-variety feature space transformation on the latent feature vectors. The global matrix feature center vector represents the average distribution position of matrix features in all training samples. The Mahalanobis distance calculation considers the correlation and variance differences between the dimensions of matrix features. The feature space is linearly transformed by the inverse of the covariance matrix before calculating the Euclidean distance. A large distance value indicates that the spectral features of the pixel are far from the matrix feature center. The spatial attention map extracts the texture and edge information of the residue candidate region through the convolution kernel. The spectral attention weight learns the ability of each band to distinguish pesticide residue detection through the fully connected network. The outer product operation multiplies the importance of spatial position with the importance of band to obtain the comprehensive weight value of each pixel for each band.

[0029] Step S4: Perform element-wise multiplication of the reflectance image cube and the collaborative attention tensor to obtain the enhanced image. Extract features from the enhanced image and input them into the regression network to obtain the predicted pesticide residue concentration.

[0030] In this process, image enhancement applies the weights of the collaborative attention tensor to the reflectance image cube through element-wise multiplication. Positions and bands with weights close to 1 retain the original signal intensity, while positions and bands with weights close to 0 are suppressed. Multi-scale feature extraction captures local detail features and global context features respectively. The signal-to-noise ratio (SNR) is calculated by dividing the mean of the feature maps by the standard deviation, where the mean represents the signal intensity and the standard deviation represents the noise fluctuation amplitude. Softmax normalization converts the SNR at each scale into normalized fusion weights. The three fully connected layers of the regression network progressively reduce the dimensionality and fit the nonlinear mapping relationship between features and concentration. The last layer outputs a single numerical value, which is the predicted pesticide residue concentration.

[0031] In this application, an enhanced image is obtained by performing element-wise multiplication of a reflectance image cube and a collaborative attention tensor. This enhanced image is then directly applied to the original spectral data through weight modulation, preserving the spectral signal intensity of the pesticide residue region, suppressing interference information from the matrix background region, and improving the signal-to-noise ratio. Features are extracted from the enhanced image and input into a regression network to obtain the predicted pesticide residue concentration. Multi-scale feature extraction captures local detail features and global context features, calculates the signal-to-noise ratio of the feature maps at each scale, and obtains the fusion weights through softmax normalization. Adaptive weighted fusion is performed based on the quality of features at each scale, avoiding noise interference that may be introduced by fixed-weight fusion and ensuring the rationality of feature fusion. Global weighted average pooling uses spatial attention maps as weights to aggregate multi-scale fused features, further strengthening the contribution of residue region features. The regression network gradually reduces dimensionality through three fully connected layers and fits the nonlinear mapping relationship between features and concentration, finally outputting a single value as the predicted pesticide residue concentration. This realizes an end-to-end detection process from the original multispectral image to quantitative concentration prediction. This application constructs a complete pesticide residue detection technology solution by organically combining the algorithmic features of probabilistic latent space modeling of variational autoencoders, statistical anomaly detection using Mahalanobis distance, spatial-spectral joint weighting using collaborative attention mechanisms, and multi-scale feature fusion driven by signal-to-noise ratio. In the field of rapid food safety detection, this solution fully leverages the feature extraction capabilities of deep learning models and the selective enhancement capabilities of attention mechanisms, enabling the detection system to accurately identify and quantitatively analyze trace pesticide residues in complex food matrix backgrounds, significantly improving the accuracy, sensitivity, and generalization ability to different food varieties.

[0032] In one specific embodiment, step S1 includes:

[0033] A multispectral imaging system was used to acquire images of the food samples to be tested, obtaining raw images of multiple bands with wavelengths ranging from 400 nanometers to 1000 nanometers.

[0034] Dark current correction is performed on the original multi-band images to obtain dark current corrected images;

[0035] Acquire a whiteboard reference image and a dark current image. Subtract the gray value of the dark current image from the gray value of the dark current correction image to obtain the correction difference. Subtract the gray value of the dark current image from the gray value of the whiteboard reference image to obtain the reference difference.

[0036] Divide the correction difference by the reference difference to obtain the reflectance value of each pixel position in each band, and construct a reflectance image cube from the reflectance values.

[0037] Specifically, the process of acquiring the dark current corrected image involves turning off the light source or blocking the lens while acquiring the multi-band original image, and acquiring the dark current image under completely dark conditions. The dark current image records the inherent noise grayscale value of each pixel position due to thermal effect when there is no light signal input from the CCD camera. The dark current correction is completed by subtracting the corresponding dark current grayscale value from the grayscale value of each pixel in the multi-band original image. The corrected image eliminates system noise unrelated to illumination. The acquisition of the whiteboard reference image uses a standard diffuse reflection whiteboard as the object under test. The whiteboard has a near 100% and uniform reflectivity for all wavelengths of light. The whiteboard reference image records the grayscale response of an ideal total internal reflection object under the current light source and imaging system conditions.

[0038] The calculation of reflectance value is essentially the normalization of the corrected grayscale response of the sample to the corrected grayscale response of the white board. The correction difference in the numerator represents the true optical response intensity of the sample after eliminating dark current, and the reference difference in the denominator represents the ideal total internal reflection response intensity of the white board after eliminating dark current. The ratio obtained by dividing the two is the reflectance of the sample relative to the ideal total internal reflection. The reflectance value is independent of the gain setting, exposure time, light source intensity and other parameters of the imaging system. The three dimensions of the reflectance image cube correspond to the image height, image width and number of spectral bands, respectively. The position index of any element in the cube uniquely determines the spatial position and wavelength, and the value of the element is the reflectance at that position and wavelength. The cube structure completely preserves the correspondence between spectral information and spatial information.

[0039] In one specific embodiment, step S2 includes:

[0040] Construct a variational autoencoder, which includes an encoder network and a decoder network;

[0041] Extract the spectral reflectance values ​​of each pixel location in the reflectance image cube to obtain the spectral vector;

[0042] The spectral vector is input into the encoder network, which includes an input layer, a first hidden layer, a second hidden layer and an output layer connected in sequence. The output layer outputs the latent mean vector and the latent log-variance vector.

[0043] Based on the latent mean vector and the latent log-variance vector, latent feature vectors are generated through reparameterization operations.

[0044] Specifically, the extraction of spectral vectors involves unfolding the reflectance image cube pixel by pixel in spatial dimension. The reflectance values ​​of each pixel location across all bands are arranged in wavelength order to form a one-dimensional vector. If the number of bands is 61, then the dimension of each spectral vector is 61. The spectral vector completely describes the spectral characteristic curve of that pixel location. The input layer of the encoder network receives the 61-dimensional spectral vector. The first and second hidden layers undergo nonlinear transformations through weight matrices and activation functions to gradually extract abstract features from the spectral curve. The output layer is divided into two branches, which output the latent mean vector and the latent log-variance vector, respectively. Both vectors have a dimension of 16. The latent mean vector represents the center position of the spectral feature in the latent space, and the latent log-variance vector represents the degree of uncertainty in the distribution of the feature.

[0045] The reparameterization operation first exponentially calculates the variance value by taking the square root of each element of the latent log-variance vector. Then, it takes the square root of the variance value to obtain the standard deviation vector. A random noise vector with the same dimension as the standard deviation vector is sampled from the standard normal distribution. The standard deviation vector and the random noise vector are multiplied element-wise and then added element-wise to the latent mean vector to obtain the latent feature vector. The randomness introduced by the reparameterization operation makes the latent feature vector generated each time slightly different for the same spectral input, but the distribution characteristics remain consistent. The 16 dimensions of the latent feature vector constitute the compressed spectral feature representation. Compared with the original 61-dimensional spectral vector, the dimensionality is reduced, but the key information for distinguishing different matrix types is retained. The variational autoencoder learns through training to map the spectra of different food matrices to different regional distributions in the latent space.

[0046] In one specific embodiment, step S3, after normalizing the latent feature vector, calculates the Mahalanobis distance to the global matrix feature center, including:

[0047] The latent feature vector is input into the normalization network for feature transformation. The normalization network includes a first fully connected layer and a second fully connected layer. The first fully connected layer is followed by a batch normalization layer and an activation layer. The second fully connected layer outputs a normalized feature vector.

[0048] Obtain the global matrix feature center vector and matrix feature covariance matrix obtained from pre-training;

[0049] Calculate the difference vector between the normalized feature vector and the global matrix feature center vector;

[0050] The Mahalanobis distance is obtained by performing matrix multiplication on the difference vector and the inverse of the matrix feature covariance matrix, and then multiplying it with the transpose of the difference vector.

[0051] Specifically, the first fully connected layer of the normalization network receives a 16-dimensional latent feature vector and maps it to a higher-dimensional feature space through a weight matrix. The batch normalization layer calculates the mean and variance of the output of the first fully connected layer in the batch dimension, standardizes the feature values ​​by subtracting the mean and dividing by the standard deviation, and then performs a linear transformation through learnable scaling and offset parameters. The activation layer uses a non-linear activation function to enhance the network's expressive power. The second fully connected layer maps the batch-normalized and activated features back to the 16-dimensional space and outputs a normalized feature vector. The normalization network obtains cross-variety feature transformation capability through meta-learning training, mapping the latent features of different food varieties to a unified standardized feature space. The global matrix feature center vector is the arithmetic mean of the normalized feature vectors of all matrix samples in the training set. The matrix feature covariance matrix is ​​a 16x16 symmetric matrix, where each element represents the covariance between two dimensions in the normalized feature vector.

[0052] The difference vector is obtained by subtracting the element corresponding to the global matrix feature center vector from each element of the normalized feature vector. The difference vector describes the direction and magnitude of the deviation of the current sample feature from the matrix feature center. The calculation of the inverse matrix of the covariance matrix involves matrix inversion. The existence of the inverse matrix requires that the covariance matrix be a full-rank non-singular matrix. The difference vector is regarded as a column vector and matrix multiplied with the inverse matrix to obtain a 16-dimensional intermediate vector. The difference vector is then transposed into a row vector and matrix multiplied with the intermediate vector to obtain a scalar value, which is the Mahalanobis distance. The Mahalanobis distance considers the variance difference of each dimension of the feature and the correlation between dimensions. The feature space is linearly transformed by the inverse covariance matrix, transforming the ellipsoidal contour lines in the feature space into spherical contour lines. The transformed Euclidean distance is the Mahalanobis distance. The larger the Mahalanobis distance value, the more the sample feature deviates from the typical distribution range of the matrix feature.

[0053] In one specific embodiment, step S3, which involves filtering residual candidate regions based on pixel positions whose Mahalanobis distance exceeds a preset threshold, includes:

[0054] Traverse each pixel position in the reflectance image cube and compare the relationship between the Mahalanobis distance corresponding to each pixel position and the preset threshold.

[0055] Pixels with a Mahalanobis distance greater than a preset threshold are marked as potential pesticide residue pixels;

[0056] The set of spatial locations of potential pesticide residue pixels constitutes the residue candidate region.

[0057] Specifically, the traversal process visits each pixel position of the reflectance image cube in the order from left to right and from top to bottom. Each pixel position corresponds to a calculated Mahalanobis distance value. The preset threshold is determined based on the statistical characteristics of the Mahalanobis distance distribution of the matrix samples in the training set. Usually, the percentile of the Mahalanobis distance distribution is selected as the threshold. The comparison operation determines whether the Mahalanobis distance of the current pixel is greater than the preset threshold. Pixels with a distance greater than the threshold have spectral features that deviate significantly from the normal distribution range of matrix features, indicating that there may be non-matrix substances such as pesticide residues at this position. Pixels with a distance less than or equal to the threshold are considered to be normal food matrix background.

[0058] The labeling of pixels with potential pesticide residues is achieved by recording the two-dimensional spatial coordinates of the pixels. The labeling process generates a binary mask image with the same cubic spatial size as the reflectance image. In the mask image, the pixel positions marked as potential pesticide residues are assigned a value of 1, while the matrix background pixel positions are assigned a value of 0. The residue candidate region consists of all pixel positions assigned a value of 1. These positions may be spatially discrete or clustered into connected regions. The spatial distribution characteristics of the residue candidate region reflect the adhesion pattern of pesticides on the food surface, providing prior information on location for the subsequent generation of spatial attention maps. Anomaly detection based on statistical characteristics is achieved by screening with Mahalanobis distance threshold, separating pixels with significantly abnormal spectral features from the normal matrix background.

[0059] Figure 2 This is a schematic diagram illustrating the residual candidate region screening based on Mahalanobis distance in an embodiment of this application. Figure 2 As shown, the distribution characteristics of matrix samples and residue samples in Mahalanobis distance space are illustrated. The horizontal axis represents the Mahalanobis distance between the normalized feature vector and the global matrix feature center, while the vertical axis represents the corresponding number of pixels. The light-colored diagonal line-filled area represents the Mahalanobis distance distribution of matrix samples, mainly concentrated in the range of 0 to 6, exhibiting typical gamma distribution characteristics. The dark-colored grid-filled area represents the Mahalanobis distance distribution of residue samples, with peak values ​​between 8 and 12, significantly deviating from the matrix feature center. The dashed line indicating the preset threshold of 6.5 effectively distinguishes the matrix background from the pesticide residue area. Pixels with a Mahalanobis distance greater than this threshold are marked as potential pesticide residue pixels, forming residue candidate regions, thus achieving anomaly detection based on statistical characteristics.

[0060] In one specific embodiment, step S3 generates a spatial attention map based on the spatial distribution of residual candidate regions, generates spectral attention weights based on the band response of the reflectance image cube, and constructs a collaborative attention tensor through the outer product operation of the spatial attention map and the spectral attention weights, including:

[0061] Extract the spatial images of the reflectance image cube in each band, and apply the first convolution kernel and the second convolution kernel to the spatial images of each band to extract multi-scale spatial features. The size of the first convolution kernel is smaller than the size of the second convolution kernel.

[0062] Multi-scale spatial features are concatenated into channels and then reduced to a single-channel feature map through a dimensionality-reducing convolutional layer. The single-channel feature map is then processed by the sigmoid activation function to obtain a spatial attention map.

[0063] Global average pooling is performed on the reflectance image cube in the spatial dimension to obtain the spectral statistical vectors of each band;

[0064] The spectral statistical vector is input into the spectral attention network, which includes a first fully connected layer and a second fully connected layer connected in sequence. After the first fully connected layer, an activation layer and a dropout layer are connected. After the second fully connected layer, a sigmoid activation layer is connected and the spectral attention weights are output.

[0065] The spatial attention map and the spectral attention weights are multiplied by an outer product to obtain the co-attention tensor.

[0066] Specifically, the spatial image extraction of the reflectance image cube in each band involves slicing the cube along the band dimension, with each band corresponding to a two-dimensional spatial image. The first convolutional kernel is 3x3 in size and is used to capture local detail features such as edges and textures. The second convolutional kernel is 5x5 in size and is used to capture spatial context features over a larger range. The two convolutional kernels perform convolution operations on the spatial image of each band respectively. The convolution operation extracts features while keeping the spatial dimensions unchanged. The feature map output by the first convolutional kernel has 32 channels, and the feature map output by the second convolutional kernel has 16 channels. The channel concatenation operation merges the feature maps of the two scales along the channel dimension to obtain a 48-channel multi-scale spatial feature. The dimensionality reduction convolutional layer uses a 1x1 convolutional kernel to reduce the 48-channel feature to 1 channel. The sigmoid activation function maps the values ​​of the single-channel feature map to the interval between 0 and 1. The value at each position in the output spatial attention map represents the spatial importance weight of that position.

[0067] Global average pooling calculates the average reflectance value of all pixel locations for each band of the reflectance image cube, resulting in a scalar representing the average response intensity of that band across the entire image region. The average values ​​of the 61 bands form a 61-dimensional spectral statistical vector. The first fully connected layer of the spectral attention network receives the 61-dimensional spectral statistical vector and maps it to a smaller dimension. The activation layer introduces nonlinear transformation capabilities, and the dropout layer randomly sets the output of some neurons to zero during training to prevent overfitting. The second fully connected layer maps the features back to the 61-dimensional space, and the sigmoid activation layer restricts the output value to the range of 0 to 1. The output spectral attention weights are a 61-dimensional vector, where each element represents the importance weight of the corresponding band for pesticide residue detection. Bands with weights close to 1 contribute more to detection, while bands with weights close to 0 contribute less.

[0068] The outer product operation treats the spatial attention map as a two-dimensional matrix with height multiplied by width, and the spectral attention weights as 61-dimensional column vectors. Each element of the two-dimensional matrix is ​​multiplied by the 61-dimensional vector to generate a three-dimensional co-attention tensor with height multiplied by width multiplied by 61. The value of any element in the tensor is equal to the spatial attention value of the corresponding spatial location multiplied by the spectral attention value of the corresponding band. The dimension of the co-attention tensor is completely consistent with that of the reflectance image cube. The weight values ​​in the tensor comprehensively consider the importance of spatial location and band, realizing fine-grained weight allocation for each pixel and each band. The outer product operation is essentially broadcasting the one-dimensional spectral weights to the entire spatial region, while simultaneously broadcasting the two-dimensional spatial weights to all bands. The multiplication of the two weights yields a three-dimensional joint weight distribution.

[0069] Figure 3 This is a schematic diagram of the spectral attention weight distribution in an embodiment of this application. Figure 3 As shown, the spectral attention weight distribution for each band within the wavelength range of 400nm to 1000nm is illustrated. The horizontal axis represents the spectral wavelength, and the vertical axis represents the attention weight values ​​normalized by the sigmoid activation function, with weights ranging from 0 to 1. The figure reveals a distinct bimodal distribution, with two significant peaks near wavelengths of 680nm and 850nm. These bands correspond to the characteristic absorption regions of pesticide residues and are therefore assigned higher weights by the spectral attention network. Dark-filled bars represent key feature bands with stronger detection contributions. Through this weight distribution, the system can adaptively enhance spectral bands sensitive to pesticide residues, suppress redundant or noisy bands, improve the specificity and accuracy of detection, and provide guidance on the importance of band dimensions for the subsequent construction of the collaborative attention tensor.

[0070] In one specific embodiment, step S4 includes:

[0071] The enhanced image is obtained by performing element-wise multiplication between the reflectance image cube and the corresponding elements of the collaborative attention tensor.

[0072] The enhanced image is subjected to original resolution feature extraction, half-downsampled feature extraction, and quarter-downsampled feature extraction to obtain the first scale feature map, the second scale feature map, and the third scale feature map.

[0073] The ratio of the mean to the standard deviation of the first-scale feature map, the second-scale feature map, and the third-scale feature map in the spatial dimension is calculated to obtain the signal-to-noise ratio (SNR) of each scale feature map. Based on the SNR, the fusion weight of each scale is calculated by softmax normalization.

[0074] The second-scale feature map and the third-scale feature map are upsampled to the spatial size of the first-scale feature map through bilinear interpolation. The upsampled feature maps of each scale are then weighted and summed according to the fusion weight to obtain the multi-scale fused feature.

[0075] Global weighted average pooling is performed on the multi-scale fused features to obtain a global feature vector. The global feature vector is then input into a regression network, which consists of three fully connected layers connected in sequence. The last fully connected layer outputs the predicted value of pesticide residue concentration.

[0076] Specifically, element-wise multiplication multiplies the reflectance image cube with the elements of the co-attention tensor at the same spatial location and in the same band. Multiplying the reflectance value by a weight close to 1 keeps the value essentially unchanged, while multiplying the reflectance value by a weight close to 0 significantly suppresses the value. The enhanced image maintains the same three-dimensional size as the reflectance image cube. The spectral signal of the pesticide residue region in the enhanced image is preserved or amplified, while the spectral signal of the matrix background region is weakened. The original resolution feature extraction directly applies a residual convolution block to the enhanced image to obtain the first-scale feature map. Half-sampling reduces the spatial size of the enhanced image by half through max pooling before applying a residual convolution block to obtain the second-scale feature map. Quarter-sampling performs two max pooling operations on the enhanced image to reduce the spatial size to one-quarter of the original size before applying a residual convolution block to obtain the third-scale feature map. The number of channels in the feature maps of the three scales is 256, but the spatial size decreases sequentially.

[0077] The signal-to-noise ratio (SNR) calculation first calculates the mean across all pixel positions and channels in the spatial dimension of each scale feature map. The mean represents the average activation intensity of the feature, i.e., the signal strength. Then, the standard deviation is calculated to represent the fluctuation range of the feature value, i.e., the noise level. Dividing the mean by the standard deviation yields the SNR value for that scale. A higher SNR value indicates that the signal component of the feature is stronger relative to the noise component. Softmax normalization first multiplies the SNR values ​​of the three scales by a temperature coefficient and then performs an exponential operation. The sum of the three exponential values ​​is then used as a normalization factor. Dividing the exponential value of each scale by the normalization factor yields the fusion weight for that scale. The sum of the three fusion weights equals 1 and is positive. Scales with higher SNR receive larger fusion weights. Bilinear interpolation upsampling spatially enlarges each channel of the second and third scale feature maps. The upsampled pixel value is obtained by calculating the weighted average of the four nearest neighbor pixels around the target position. After upsampling, the spatial dimensions of the three scale feature maps are unified to the size of the first scale feature map.

[0078] Weighted summation multiplies the feature values ​​at corresponding positions of the feature maps at the three scales by their respective fusion weights and then sums them to obtain the feature value of the multi-scale fused feature at that position. The fused feature combines fine-grained local features and coarse-grained global features. Global weighted average pooling uses the spatial attention map generated in step three as weights, multiplying the feature vector of the multi-scale fused feature at each spatial position by the corresponding spatial attention weight value. The sum of the weighted feature vectors at all spatial positions is then divided by the sum of the spatial attention weights to obtain a 256-dimensional global feature vector. The first fully connected layer of the regression network maps the 256-dimensional global feature vector to 128 dimensions, the second layer to 64 dimensions, and the third layer to 1 dimension, outputting a single numerical value, which is the predicted value of pesticide residue concentration in milligrams per kilogram. The regression network learns the nonlinear mapping relationship between the global feature vector and the pesticide residue concentration through training.

[0079] The above describes the rapid detection method for pesticide residues in food based on multispectral imaging in the embodiments of this application. The following describes the rapid detection system for pesticide residues in food based on multispectral imaging in the embodiments of this application. One embodiment of the rapid detection system for pesticide residues in food based on multispectral imaging in the embodiments of this application includes:

[0080] The calibration module is used to acquire multispectral images of the food sample to be tested and perform calibration processing to obtain a reflectance image cube;

[0081] The encoding module is used to input the reflectance image cube into the variational autoencoder for encoding to obtain the latent feature vector;

[0082] The filtering module is used to normalize the potential feature vector and calculate the Mahalanobis distance to the global matrix feature center. Residual candidate regions are obtained by filtering the pixel positions where the Mahalanobis distance exceeds a preset threshold. A spatial attention map is generated based on the spatial distribution of the residual candidate regions. Spectral attention weights are generated based on the band response of the reflectance image cube. A co-attention tensor is constructed by the outer product operation of the spatial attention map and the spectral attention weights.

[0083] The prediction module is used to perform element-wise multiplication of the reflectance image cube and the collaborative attention tensor to obtain an enhanced image, extract features from the enhanced image and input them into a regression network to obtain a predicted value of pesticide residue concentration.

[0084] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A rapid detection method for pesticide residues in food based on multispectral imaging, characterized in that, The method includes: Step S1: Acquire multispectral images of the food sample to be tested and perform correction processing to obtain a reflectance image cube; Step S2: Input the reflectance image cube into a variational autoencoder for encoding to obtain a latent feature vector; Step S3: After normalizing the latent feature vector, calculate the Mahalanobis distance to the global matrix feature center. Select residual candidate regions by filtering the pixel positions where the Mahalanobis distance exceeds a preset threshold. Generate a spatial attention map based on the spatial distribution of the residual candidate regions. Generate spectral attention weights based on the band response of the reflectance image cube. Construct a co-attention tensor by performing an outer product operation between the spatial attention map and the spectral attention weights. Step S4: Perform element-wise multiplication of the reflectance image cube and the collaborative attention tensor to obtain an enhanced image. Extract features from the enhanced image and input them into a regression network to obtain the predicted pesticide residue concentration.

2. The rapid detection method for pesticide residues in food based on multispectral imaging according to claim 1, characterized in that, Step S1 includes: A multispectral imaging system was used to acquire images of the food samples to be tested, obtaining raw images of multiple bands with wavelengths ranging from 400 nanometers to 1000 nanometers. Dark current correction is performed on the original multi-band image to obtain a dark current corrected image; Acquire a whiteboard reference image and a dark current image. Subtract the gray value of the dark current image from the gray value of the dark current correction image to obtain a correction difference value. Subtract the gray value of the dark current image from the gray value of the whiteboard reference image to obtain a reference difference value. Divide the correction difference by the reference difference to obtain the reflectance value of each pixel position in each band, and construct the reflectance image cube from the reflectance values.

3. The rapid detection method for pesticide residues in food based on multispectral imaging according to claim 1, characterized in that, Step S2 includes: Construct a variational autoencoder, which includes an encoder network and a decoder network; Extract the spectral reflectance values ​​of each pixel location in the reflectance image cube to obtain the spectral vector; The spectral vector is input into the encoder network, which includes an input layer, a first hidden layer, a second hidden layer, and an output layer connected in sequence. The output layer outputs a latent mean vector and a latent log-variance vector. The latent feature vector is generated through reparameterization operation based on the latent mean vector and the latent log-variance vector.

4. The rapid detection method for pesticide residues in food based on multispectral imaging according to claim 1, characterized in that, Step S3, which involves normalizing the latent feature vector and then calculating the Mahalanobis distance to the global matrix feature center, includes: The latent feature vector is input into a normalization network for feature transformation. The normalization network includes a first fully connected layer and a second fully connected layer. The first fully connected layer is followed by a batch normalization layer and an activation layer. The second fully connected layer outputs a normalized feature vector. Obtain the global matrix feature center vector and matrix feature covariance matrix obtained from pre-training; Calculate the difference vector between the normalized feature vector and the global matrix feature center vector; The difference vector is multiplied by the inverse of the matrix feature covariance matrix, and then multiplied by the transpose of the difference vector to obtain the Mahalanobis distance.

5. The rapid detection method for pesticide residues in food based on multispectral imaging according to claim 4, characterized in that, Step S3, which involves filtering residual candidate regions based on pixel positions whose Mahalanobis distance exceeds a preset threshold, includes: Traverse each pixel position in the reflectance image cube and compare the relationship between the Mahalanobis distance corresponding to each pixel position and a preset threshold. Pixels with a Mahalanobis distance greater than the preset threshold are marked as potential pesticide residue pixels; The set of spatial locations of the potential pesticide residue pixels constitutes the residue candidate region.

6. The rapid detection method for pesticide residues in food based on multispectral imaging according to claim 5, characterized in that, In step S3, a spatial attention map is generated based on the spatial distribution of the residual candidate regions, spectral attention weights are generated based on the band response of the reflectance image cube, and a collaborative attention tensor is constructed through the outer product operation of the spatial attention map and the spectral attention weights, including: Extract the spatial images of the reflectance image cube in each band, and apply a first convolution kernel and a second convolution kernel to the spatial images of each band to extract multi-scale spatial features. The size of the first convolution kernel is smaller than the size of the second convolution kernel. The multi-scale spatial features are concatenated by channels and then reduced to a single-channel feature map by a dimensionality-reducing convolutional layer. The single-channel feature map is then processed by the sigmoid activation function to obtain the spatial attention map. Global average pooling is performed on the reflectance image cube in the spatial dimension to obtain the spectral statistical vectors of each band; The spectral statistical vector is input into the spectral attention network, which includes a first fully connected layer and a second fully connected layer connected in sequence. After the first fully connected layer, an activation layer and a dropout layer are connected. After the second fully connected layer, a sigmoid activation layer is connected and the spectral attention weights are output. The spatial attention map and the spectral attention weights are multiplied by an outer product to obtain the collaborative attention tensor.

7. The rapid detection method for pesticide residues in food based on multispectral imaging according to claim 1, characterized in that, Step S4 includes: The enhanced image is obtained by performing element-wise multiplication between the reflectance image cube and the corresponding elements of the collaborative attention tensor. The enhanced image is subjected to original resolution feature extraction, half-downsampled feature extraction, and quarter-downsampled feature extraction to obtain a first-scale feature map, a second-scale feature map, and a third-scale feature map. The ratio of the mean to the standard deviation of the first-scale feature map, the second-scale feature map, and the third-scale feature map in the spatial dimension is calculated to obtain the signal-to-noise ratio of each scale feature map. Based on the signal-to-noise ratio, the fusion weight of each scale is calculated by softmax normalization. The second-scale feature map and the third-scale feature map are respectively upsampled to the spatial size of the first-scale feature map through bilinear interpolation. The upsampled feature maps of each scale are then weighted and summed according to the fusion weight to obtain the multi-scale fusion feature. The multi-scale fused features are subjected to global weighted average pooling to obtain a global feature vector. The global feature vector is then input into a regression network, which includes three fully connected layers connected in sequence. The last fully connected layer outputs the predicted value of pesticide residue concentration.

8. A rapid detection system for pesticide residues in food based on multispectral imaging, characterized in that, For implementing the rapid detection method for pesticide residues in food based on multispectral imaging as described in any one of claims 1-7, the rapid detection system for pesticide residues in food based on multispectral imaging comprises: The calibration module is used to acquire multispectral images of the food sample to be tested and perform calibration processing to obtain a reflectance image cube; The encoding module is used to input the reflectance image cube into the variational autoencoder for encoding to obtain the latent feature vector; The filtering module is used to normalize the potential feature vector and calculate the Mahalanobis distance to the global matrix feature center. Residual candidate regions are obtained by filtering the pixel positions where the Mahalanobis distance exceeds a preset threshold. A spatial attention map is generated based on the spatial distribution of the residual candidate regions. Spectral attention weights are generated based on the band response of the reflectance image cube. A co-attention tensor is constructed by the outer product operation of the spatial attention map and the spectral attention weights. The prediction module is used to perform element-wise multiplication of the reflectance image cube and the collaborative attention tensor to obtain an enhanced image, extract features from the enhanced image and input them into a regression network to obtain a predicted value of pesticide residue concentration.

9. The system according to claim 8, characterized in that, Multispectral images of the food sample to be tested are acquired and corrected to obtain a reflectance image cube, including: A multispectral imaging system was used to acquire images of the food samples to be tested, obtaining raw images of multiple bands with wavelengths ranging from 400 nanometers to 1000 nanometers. Dark current correction is performed on the original multi-band image to obtain a dark current corrected image; Acquire a whiteboard reference image and a dark current image. Subtract the gray value of the dark current image from the gray value of the dark current correction image to obtain a correction difference value. Subtract the gray value of the dark current image from the gray value of the whiteboard reference image to obtain a reference difference value. Divide the correction difference by the reference difference to obtain the reflectance value of each pixel position in each band, and construct the reflectance image cube from the reflectance values.

10. The system according to claim 8, characterized in that, The reflectance image cube is input into a variational autoencoder for encoding to obtain a latent feature vector, including: Construct a variational autoencoder, which includes an encoder network and a decoder network; Extract the spectral reflectance values ​​of each pixel location in the reflectance image cube to obtain the spectral vector; The spectral vector is input into the encoder network, which includes an input layer, a first hidden layer, a second hidden layer, and an output layer connected in sequence. The output layer outputs a latent mean vector and a latent log-variance vector. The latent feature vector is generated through reparameterization operation based on the latent mean vector and the latent log-variance vector.

Citation Information

Patent Citations

  • Fruit and vegetable pesticide residue visual real-time detection method based on multispectral detection system

    CN113740276A

  • Multi-spectral detection system for rapid real-time detection of fruit and vegetable pesticide residues and use method thereof

    CN113777063A