A method and system for detecting food freshness based on image recognition
By combining white balance correction and multi-step target region segmentation with multi-color space feature extraction and weighted kernel function principal component analysis model, the problems of image color deviation and background interference in food freshness detection are solved, and high-precision and stable food freshness determination is achieved.
Patent Information
- Application Number
- CN202510656833.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-21
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2045-05-21
AI Technical Summary
Existing image recognition methods suffer from problems such as image color deviation, background interference, and difficulty in target extraction when detecting food freshness, which affect the accuracy and stability of the detection.
By employing white balance correction, multi-step target region segmentation, multi-color space feature extraction, and a weighted kernel function principal component analysis model, combined with Mahalanobis distance and exponential decay function, accurate segmentation and freshness grade determination of food images can be achieved.
It improves the accuracy and stability of food freshness detection, adapts to different environments and food types, and achieves rapid, non-destructive, non-contact, high-precision detection.
Smart Images

Figure CN120672874B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and more specifically, to a method and system for detecting food freshness based on image recognition. Background Technology
[0002] Food freshness is one of the core indicators for measuring food quality and safety, especially for perishable foods such as fresh fruits and vegetables, meat, and aquatic products, where changes in freshness directly affect consumers' sensory experience and food safety. Currently, the main methods in the field of food freshness testing include: Sensory evaluation: This method typically involves professionals assessing the freshness of food through subjective senses such as sight and smell. While simple to operate, the evaluation results are greatly influenced by individual experience and subjective judgment, lacking objectivity and repeatability. Physicochemical index testing: Methods such as the detection of parameters like volatile basic nitrogen (TVB-N), pH value, and peroxide value can reflect chemical changes during food spoilage, but usually require sampling, destructive operations, complex experimental procedures, and specialized equipment, making them unsuitable for rapid, non-destructive testing or on-site applications. Intelligent sensor and gas detection methods: These methods determine freshness by detecting changes in the gases surrounding the food or its surface composition. While offering some non-contact and real-time capabilities, they are susceptible to environmental interference, have unstable detection accuracy, and involve high equipment costs.
[0003] In recent years, with the development of image recognition and computer vision technologies, analyzing the appearance characteristics of food using image data has become a potential approach to achieve non-contact, rapid freshness detection. However, existing image recognition methods still face the following technical bottlenecks: image color deviation: affected by factors such as ambient lighting and equipment parameters, images acquired under different conditions exhibit significant color differences, affecting the accuracy of feature extraction; background interference and difficulty in target extraction: target food areas in complex backgrounds are difficult to segment accurately, easily causing color feature confusion and affecting the judgment results.
[0004] Therefore, there is an urgent need for an image recognition-based food freshness detection method that can achieve non-destructive, rapid, and high-precision freshness assessment in the food industry. Summary of the Invention
[0005] In view of this, the present invention proposes a food freshness detection method and system based on image recognition to solve the problem that the target food area in complex backgrounds is difficult to segment accurately when detecting freshness images in the prior art, which easily causes confusion of color features and affects the judgment results.
[0006] On one hand, the present invention proposes a food freshness detection method based on image recognition, comprising:
[0007] An initial image of the target food is acquired, the initial image including the target food and a white reference card. The initial image is then white-balance corrected based on the white reference card to obtain a corrected image.
[0008] The corrected image is segmented into target regions to extract the main target food region and remove background interference information;
[0009] The main area image of the target food is sampled and converted in color space to obtain the redness value, yellowness value and brightness value in Lab color space, the hue angle in HSV color space, and the red-green ratio value in RGB color space, which constitute the image color feature set.
[0010] Based on the image color feature set and the preset standard samples, a difference feature vector is established, and the deviation of the target food from the standard samples in multidimensional color features is compared.
[0011] The freshness grade of the target food is determined based on the degree of deviation.
[0012] Furthermore, the white balance correction includes:
[0013] The R, G, and B values of the white reference card region in the initial image are obtained respectively, and the R, G, and B values are compared with the theoretical white value to calculate the R channel gain, G channel gain, and B channel gain.
[0014] The R, G, and B channels of the initial image are multiplied by the corresponding R channel gain, G channel gain, and B channel gain to obtain the corrected image.
[0015] Furthermore, when performing target region segmentation on the corrected image, the following steps are included:
[0016] The brightness value of each pixel in the corrected image is judged one by one. Pixels with brightness values higher than or equal to a preset brightness threshold and saturation values lower than a preset saturation threshold are selected and removed. The removal area is calculated. When the removal area is greater than or equal to 0.5 times the area of the corrected image, the lighting conditions and / or acquisition position when the initial image was acquired are adjusted.
[0017] Obtain the redness and yellowness values of the remaining areas in the corrected image, calculate the Euclidean distance between the redness and yellowness values and the standard redness and yellowness values in the standard food sample, and obtain the maximum Euclidean distance;
[0018] The Euclidean distance is compared with 0.95 times the maximum Euclidean distance, and pixels that are greater than 0.95 times the maximum Euclidean distance are removed.
[0019] By using an edge detection algorithm to remove non-food areas from the corrected image, the main region of the target food is obtained.
[0020] Furthermore, each pixel in the remaining region is denoted as (a i b i The Euclidean distance is calculated using the following relationship:
[0021] ;
[0022] Among them, D i Let a represent the Euclidean distance to the i-th pixel. i With b i Let a0 and b0 represent the redness and yellowness values of the i-th pixel, respectively, and let b0 represent the standard redness and yellowness values in the standard food sample, respectively.
[0023] Furthermore, based on the image color feature set and preset standard samples, a difference feature vector is established, and the deviation of the target food from the standard samples in multidimensional color features is compared, including:
[0024] The image color feature set of the target food is input into the trained kernel function principal component analysis model, and nonlinear dimensionality reduction is performed to obtain the principal component feature vector of the target food.
[0025] The image color feature set of standard samples is obtained, and the dimensionality is reduced by the kernel function principal component analysis model to obtain the mean vector and covariance matrix of the principal component features of standard samples corresponding to different freshness levels.
[0026] The Mahalanobis distance between the principal component feature vector of the target food and the mean principal component feature vector of the standard samples under each freshness grade is calculated. The Mahalanobis distance is converted into similarity based on the exponential decay function, and the freshness grade is determined based on the similarity.
[0027] Furthermore, the Mahalanobis distance satisfies the following relationship:
[0028] ;
[0029] Among them, D M,j Let represent the Mahalanobis distance at the j-th freshness level, x represent the principal component eigenvector of the target food, μ represent the mean of the principal component eigenvectors of several standard samples in any freshness level, T represent the transpose of the matrix, and S represent the covariance matrix of the principal component eigenvectors of the standard samples in any freshness level.
[0030] Furthermore, the Mahalanobis distance is converted into similarity based on the exponential decay function using the following relationship:
[0031] ;
[0032] Among them, F jLet D represent the similarity at the j-th level of freshness, α represent the distance influence coefficient, and D... M,j This represents the Mahalanobis distance at the j-th level of freshness.
[0033] Based on the similarity corresponding to each freshness grade, the freshness grade with the highest similarity is selected as the final freshness grade of the target food.
[0034] Furthermore, the kernel function principal component analysis model is obtained through the following method:
[0035] An image library containing standard samples with multiple freshness levels is constructed. Image color feature sets for each standard sample are extracted, and sample weights are assigned to each standard sample's image color feature set to construct a weighted sample set. A radial basis function is selected as the kernel function, and a weighted kernel covariance matrix is constructed using the sample weights. Eigenvalue decomposition is performed on the weighted kernel covariance matrix to extract principal component directions and mapping matrices. The parameters of different kernel functions are tested based on cross-validation to determine the optimal parameter combination. The mapping matrix and kernel function parameters are saved to construct a kernel function principal component analysis model.
[0036] Furthermore, several candidate values for the distance influence coefficient are set, and the similarity of each candidate value is calculated. Unsupervised clustering is performed on the similarity, and the candidate value with the largest ratio of the intra-class average distance to the inter-class average distance in the clustering results is selected as the value of the distance influence coefficient.
[0037] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0038] By introducing a white reference card during image acquisition and performing white balance correction based on it, the influence of ambient lighting changes and device parameter differences on image color is effectively eliminated. This results in more accurate and consistent corrected image colors, improving the stability and reliability of subsequent color feature extraction, avoiding color deviations caused by uneven lighting, and solving the problem of color instability in traditional image recognition methods. A multi-step combination of brightness and saturation threshold removal, Euclidean distance-based anomaly pixel removal, and edge detection algorithms accurately extracts the main region of the target food, effectively eliminating interference from complex backgrounds and non-food areas. This multi-stage screening and removal mechanism ensures the purity of color feature sampling, preventing background colors from obscuring the food's intrinsic features, thereby improving the accuracy and stability of freshness determination. Multi-dimensional color features are extracted in Lab, HSV, and RGB color spaces, enabling a multi-angle, comprehensive description of the food's apparent color. Multi-color space feature fusion compensates for the limitations of a single color space, fully reflecting subtle changes in food color, improving the ability to capture freshness details, and enhancing the adaptability of the detection model to different food types and states. A weighted kernel principal component analysis (PCA) model is introduced, combining sample weights and radial basis function kernels to perform nonlinear dimensionality reduction on multidimensional color features, extracting principal component feature vectors. This achieves effective compression and feature extraction of complex high-dimensional color data. Compared with traditional linear PCA, this method can better capture the nonlinear relationships between color features, improving the discriminative power and robustness of freshness grade classification. By calculating the Mahalanobis distance between the target food and standard samples of different freshness grades, and using an exponential decay function to convert the distance into similarity, a reasonable similarity measurement mechanism is established. This matching method based on statistics and distance measurement considers the correlation and covariance structure between features, reducing the probability of misjudgment and achieving more scientific and accurate freshness grading. Through unsupervised cluster analysis of the similarity performance of different distance influence coefficients, the optimal distance influence coefficient parameters are automatically selected, avoiding subjective bias caused by manual setting. This mechanism improves the model's adaptability to different food types and collection environments, ensuring the stability and reliability of the system in diverse practical application scenarios. The entire detection process is based on image acquisition and computational analysis, requiring no destructive sampling or complex physicochemical testing equipment. The detection process is simple and fast, suitable for real-time applications on-site or production lines. It greatly improves testing efficiency and practical value, reduces testing costs, and meets the food industry's need for rapid response to freshness testing.
[0039] On the other hand, the present invention also proposes an image recognition-based food freshness detection system for the above-described method, comprising:
[0040] The acquisition module is configured to acquire an initial image of the target food, which includes the target food and a white reference card. The initial image is then white-balanced based on the white reference card to obtain a corrected image.
[0041] The segmentation module is configured to segment the target region of the corrected image, extract the main region of the target food, and remove background interference information.
[0042] The feature construction module is configured to perform pixel sampling and color space conversion on the main region image of the target food to obtain the redness value, yellowness value and brightness value in the Lab color space, the hue angle in the HSV color space, and the red-green ratio value in the RGB color space, which constitute the image color feature set.
[0043] The calculation module is configured to establish a difference feature vector based on the image color feature set and a preset standard sample, compare the deviation of the target food from the standard sample in multidimensional color features, and determine the freshness level of the target food based on the degree of deviation.
[0044] It should be noted that the image recognition-based food freshness detection method and system proposed in this invention have the same beneficial effects, which will not be repeated here. Attached Figure Description
[0045] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:
[0046] Figure 1 This is a flowchart of a food freshness detection method based on image recognition, provided as an embodiment of the present invention.
[0047] Figure 2 This is a functional block diagram of a food freshness detection system based on image recognition, provided in an embodiment of the present invention. Detailed Implementation
[0048] Exemplary embodiments of the present application will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the disclosure to those skilled in the art. It should be noted that, unless otherwise specified, the embodiments and features described herein can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.
[0049] See Figure 1 As shown in the embodiment of the present invention, a food freshness detection method based on image recognition includes:
[0050] S1: Acquire an initial image of the target food, which includes the target food and a white reference card. Perform white balance correction on the initial image based on the white reference card to obtain a corrected image.
[0051] S2: Perform target region segmentation on the corrected image, extract the main target food region, and remove background interference information;
[0052] S3: Perform pixel sampling and color space conversion on the main area image of the target food to obtain the redness value, yellowness value and brightness value in Lab color space, the hue angle in HSV color space, and the red-green ratio value in RGB color space, which constitute the image color feature set.
[0053] S4: Establish a differential feature vector based on the image color feature set and the preset standard samples, and compare the deviation of the target food from the standard samples in multidimensional color features;
[0054] S5: Determine the freshness grade of the target food based on the degree of deviation.
[0055] It should be noted that by introducing a white reference card for white balance correction, the influence of ambient lighting changes on image color is effectively eliminated, ensuring the true reproduction of colors in the acquired images and improving the accuracy of subsequent color feature extraction, thereby enhancing the objectivity of freshness assessment. Target region segmentation technology extracts the main food region and removes background information, avoiding interference from complex backgrounds on color features, improving the specificity and reliability of color feature analysis, and ensuring more accurate detection results. A comprehensive analysis combining multi-dimensional color features (redness, yellowness, brightness, hue angle, and red-green ratio) from Lab, HSV, and RGB color spaces comprehensively reflects changes in food surface color, enhancing the sensitivity and discriminative power of freshness assessment. A differential feature vector is established based on the image color feature set and standard samples, quantifying the multi-dimensional color deviation between the target food and standard samples, which helps to meticulously capture changes in food freshness and achieve refined grading. The entire method relies on image recognition and multi-dimensional color feature analysis, requiring no destructive sampling or complex chemical detection, and possesses rapid, non-contact, and automated detection capabilities. It is suitable for various application scenarios, including on-site and industrial production lines, improving detection efficiency and practical value.
[0056] In some embodiments of this application, white balance correction includes:
[0057] The R, G, and B values of the white reference card region in the initial image are obtained respectively, and the R, G, and B values are compared with the theoretical white value to calculate the R channel gain, G channel gain, and B channel gain.
[0058] The R, G, and B channels of the initial image are multiplied by the corresponding R channel gain, G channel gain, and B channel gain to obtain the corrected image.
[0059] It's important to note that the white reference card area is accurately located from the acquired initial image, and the average values of the red, green, and blue channels for all pixels within that area are extracted. Then, these measured red, green, and blue values are compared to the theoretical ideal white values, calculating the red, green, and blue channel gains. Here, the theoretical white values represent the ideal red, green, and blue values for pure white, typically the maximum values, serving as the benchmark for color correction. Next, the red channel value of each pixel in the initial image is multiplied by the red channel gain, the green channel value by the green channel gain, and the blue channel value by the blue channel gain, thus adjusting the image's color and generating a white balance-corrected image.
[0060] This white balance correction method based on a white reference card brings several significant benefits:
[0061] First, it effectively eliminates color deviations caused by different light sources. Whether shooting in complex lighting environments such as natural light, fluorescent lights, or incandescent lights, it can dynamically correct image colors using the standard white of the reference card, ensuring more realistic and natural image colors.
[0062] Secondly, through precise color correction, the key indicators such as redness, yellowness, brightness, hue angle, and red-green ratio obtained during subsequent color feature extraction of food images are more accurate and stable. This greatly improves the reliability of image color information and reduces feature errors caused by changes in lighting.
[0063] Furthermore, this method enhances the robustness and stability of food freshness detection, ensuring that the system can output consistent and reliable freshness assessment results regardless of changes in the detection environment, thereby improving the practicality and promotional value of the detection.
[0064] Finally, this correction method is simple to operate and suitable for rapid on-site implementation. Simply place a standard white reference card in the shooting scene, and the white balance correction is quickly achieved by automatically calculating the gain and applying it to the image, providing a solid foundation for subsequent image processing and freshness determination.
[0065] In some embodiments of this application, target region segmentation of the corrected image includes:
[0066] The brightness value of each pixel in the corrected image is judged one by one. Pixels with brightness values higher than or equal to a preset brightness threshold and saturation values lower than a preset saturation threshold are selected and removed. The removal area is calculated. When the removal area is greater than or equal to 0.5 times the area of the corrected image, the lighting conditions and / or acquisition position when the initial image was acquired are adjusted.
[0067] Obtain the redness and yellowness values of the remaining areas in the corrected image, calculate the Euclidean distance between the redness and yellowness values and the standard redness and yellowness values in the standard food sample, and obtain the maximum Euclidean distance;
[0068] The Euclidean distance is compared with 0.95 times the maximum Euclidean distance, and pixels with a distance greater than 0.95 times the maximum Euclidean distance are removed.
[0069] By using an edge detection algorithm to remove non-food areas from the corrected image, the main region of the target food is obtained.
[0070] Understandably, the system first assesses the brightness value of each pixel in the corrected image. Pixels with brightness values higher than or equal to a preset brightness threshold and saturation values lower than a preset saturation threshold are selected and treated as potential non-target regions, then removed. Simultaneously, the area occupied by the removed pixels is calculated.
[0071] When the area removed reaches or exceeds half of the total area of the corrected image, it indicates that there is a large area of lighting abnormality or background interference in the currently acquired image. At this time, the system will automatically prompt you to adjust the lighting conditions and / or acquisition angle and position when acquiring the initial image to ensure the quality of subsequent images and the segmentation effect.
[0072] Next, after removing unqualified pixels, the system obtains the redness and yellowness values of each pixel in the remaining area of the corrected image, compares them with the redness and yellowness values of a pre-set standard food sample, calculates the Euclidean distance between the two, and measures the similarity of color features.
[0073] Furthermore, the system obtains the maximum Euclidean distance among all pixels and compares the Euclidean distance of each pixel with 95% of this maximum value. Any pixel whose Euclidean distance exceeds this threshold is considered to have a significant difference from the target food color and is therefore discarded to further optimize the accuracy of the target area.
[0074] Finally, edge detection algorithms are used to identify and remove non-food areas in the corrected image, removing background and cluttered edges, ultimately obtaining a complete and accurate main area of the target food.
[0075] By employing dual thresholding for brightness and saturation, areas of excessively strong or weak illumination, background reflection, and low saturation are effectively filtered out, reducing interference from irrelevant pixels at the source and improving the accuracy and robustness of image preprocessing. Calculating the Euclidean distance between red and yellow values, combined with the color features of standard samples, accurately identifies pixels significantly different from the target food color, enabling fine-grained removal of non-target areas and greatly enhancing the targeting and accuracy of the segmentation results. Edge detection technology further removes non-food areas from the image, ensuring the integrity and clarity of the target area boundaries, avoiding interference from background noise in subsequent analysis, and improving the overall segmentation accuracy. The ratio of the removed area to the image area dynamically monitors the lighting quality of the acquisition environment, automatically prompting adjustments to acquisition settings when lighting conditions are poor, ensuring stable input image quality and fundamentally guaranteeing the reliability and consistency of subsequent detection.
[0076] In some embodiments of this application, each pixel in the remaining region is denoted as (a i b i The Euclidean distance is calculated using the following relationship:
[0077] ;
[0078] Among them, D i Let a represent the Euclidean distance to the i-th pixel. i With b i Let a0 and b0 represent the redness and yellowness values of the i-th pixel, respectively, and let b0 represent the standard redness and yellowness values in the standard food sample, respectively.
[0079] It should be noted that the quantitative assessment of pixel color differences is achieved by calculating the Euclidean distance between the redness and yellowness values of each pixel in the target food image and the corresponding color values in the standard food sample. This Euclidean distance calculation method can accurately reflect the degree of deviation between pixel colors and standard colors, thereby effectively distinguishing the target food area from non-target areas or abnormal areas.
[0080] The beneficial effects are mainly reflected in the following aspects: First, the Euclidean distance constructed by using two dimensions of redness and yellowness comprehensively considers the multi-dimensional characteristics of food color, making the color difference assessment more comprehensive and accurate, and avoiding misjudgment caused by a single color index.
[0081] Secondly, by calculating the distance between the color features of each pixel and the standard sample color, pixel-level fine-grained analysis is achieved, which helps to eliminate noise pixels with abnormal colors and non-food pixels, thereby improving the accuracy of target area extraction.
[0082] Furthermore, this method can flexibly adapt to the color standards of different batches and varieties of food, and has good versatility and scalability, which can meet the freshness detection needs of various foods.
[0083] Furthermore, the quantitative characteristics of this distance can provide a scientific basis for subsequent freshness level determination, enhancing the objectivity and reliability of the test results.
[0084] In some embodiments of this application, a difference feature vector is established based on an image color feature set and a preset standard sample, and the deviation of the target food from the standard sample in multidimensional color features is compared, including:
[0085] The image color feature set of the target food is input into the trained kernel function principal component analysis model, and nonlinear dimensionality reduction is performed to obtain the principal component feature vector of the target food.
[0086] The image color feature set of standard samples is obtained, and the dimensionality is reduced by using the kernel function principal component analysis model to obtain the mean vector and covariance matrix of the principal component features of standard samples corresponding to different freshness levels.
[0087] The Mahalanobis distance between the principal component feature vector of the target food and the mean principal component feature vector of the standard samples under each freshness grade is calculated. The Mahalanobis distance is converted into similarity based on the exponential decay function, and the freshness grade is determined based on the similarity.
[0088] Specifically, the first step involves inputting the color feature set of the target food image into a pre-trained kernel function principal component analysis (PCA) model. PCA is a non-linear dimensionality reduction technique that maps input features to a high-dimensional space using a kernel function, and then performs principal component analysis in that high-dimensional space to extract the main features of the data. After training, this model can effectively capture the complex non-linear relationships between color features, improving feature representation capabilities. Executing the model yields the principal component feature vector of the target food, which represents the key features after dimensionality reduction and summarizes the main changes in the color information of the target food.
[0089] The second step involves pre-collecting and organizing color feature sets of standard food sample images for different freshness grades, and then performing the same kernel function principal component analysis for dimensionality reduction. For each freshness grade of standard sample, the mean vector and covariance matrix of its principal component eigenvectors are calculated. The mean vector represents the central tendency of the color features for that freshness grade, while the covariance matrix reflects the distribution range and correlation of color features within that category.
[0090] The third step is to calculate the Mahalanobis distance between the principal component feature vector of the target food and the principal component mean vector of the standard samples for each freshness grade. Mahalanobis distance is a distance metric that considers the distribution characteristics of samples and can effectively measure the statistical distance between samples, reflecting the degree of similarity between the color characteristics of the target food and the standard samples for each grade.
[0091] The fourth step involves converting the calculated Mahalanobis distance into a similarity value using an exponential decay function. The exponential decay function maps distance values to a similarity score between 0 and 1; the smaller the distance, the higher the similarity, and vice versa. This conversion helps transform the quantitative value of Mahalanobis distance into a more intuitive similarity metric.
[0092] The fifth step is to compare the similarity values of the target food with the standard samples of each freshness grade, and select the grade with the highest similarity as the result of the freshness grade determination of the target food.
[0093] In some embodiments of this application, the Mahalanobis distance satisfies the following relationship:
[0094] ;
[0095] Among them, D M,j Let represent the Mahalanobis distance at the j-th freshness level, x represent the principal component eigenvector of the target food, μ represent the mean of the principal component eigenvectors of several standard samples in any freshness level, T represent the transpose of the matrix, and S represent the covariance matrix of the principal component eigenvectors of the standard samples in any freshness level.
[0096] It should be noted that Mahalanobis distance not only measures the distance between two feature vectors but also considers the covariance matrix of the sample features at each freshness level, reflecting the correlation and distribution pattern of sample features. This makes the distance calculation more reasonable, avoiding the errors that may be caused by simple Euclidean distance when correlation exists, and improving the accuracy of freshness determination. Through weighted adjustment of the covariance matrix, Mahalanobis distance can more sensitively capture subtle differences in feature distribution between different freshness levels, thereby enhancing the distinguishability between levels and facilitating more detailed and reliable freshness grading. This method is suitable for complex data with high dimensionality and correlation of sample features, enhancing the adaptability and stability of the detection model to multidimensional color features, and improving the versatility and robustness of freshness detection under different food types and collection conditions.
[0097] In some embodiments of this application, Mahalanobis distance is converted into similarity based on an exponential decay function using the following relationship:
[0098] ;
[0099] Among them, F jLet D represent the similarity at the j-th level of freshness, α represent the distance influence coefficient, and D... M,j This represents the Mahalanobis distance at the j-th level of freshness.
[0100] Based on the similarity corresponding to each freshness grade, the freshness grade with the highest similarity is selected as the final freshness grade of the target food.
[0101] It's important to note that the exponential decay function maps the non-negative Mahalanobis distance to a similarity value. As the distance increases, the similarity gradually decreases, ensuring the smoothness and continuity of freshness assessment and avoiding abrupt changes in assessment caused by hard thresholds. The parameter α, acting as a distance influence coefficient, adjusts the impact of distance on similarity, allowing the model to flexibly adjust its sensitivity to feature differences according to actual needs, thereby improving the flexibility and adaptability of the assessment. Converting distance to similarity makes the assessment results more intuitive and interpretable, facilitating subsequent similarity-based comparisons and selections, and clearly reflecting the degree of matching between the target food and each freshness grade. By selecting the freshness grade with the highest similarity as the final assessment result, it effectively avoids the abnormal assessments that might result from solely relying on the minimum distance, enhancing the accuracy and stability of freshness grading.
[0102] In some embodiments of this application, the kernel function principal component analysis model is obtained through the following methods:
[0103] An image library containing standard samples with multiple freshness levels is constructed. Image color feature sets for each standard sample are extracted, and sample weights are assigned to each standard sample's image color feature set to construct a weighted sample set. A radial basis function is selected as the kernel function, and a weighted kernel covariance matrix is constructed using the sample weights. Eigenvalue decomposition is performed on the weighted kernel covariance matrix to extract principal component directions and mapping matrices. The parameters of different kernel functions are tested based on cross-validation to determine the optimal parameter combination. The mapping matrix and kernel function parameters are saved to construct a kernel function principal component analysis model.
[0104] Specifically, firstly, a standard sample image library containing multiple food samples of different freshness grades is prepared. For each standard sample in the library, its image color feature set is extracted using the method of this invention. These color features include indicators in multiple color spaces, such as redness value, yellowness value, and brightness value. To enhance the representativeness and robustness of the model, different standard samples are assigned corresponding sample weights based on their importance or representativeness, forming a weighted sample set.
[0105] Next, radial basis functions (RBFs) are chosen as the kernel function. RBFs effectively map the original feature space to a high-dimensional feature space, thereby capturing more complex nonlinear structures in the data. Using sample weights, the kernel matrix of the samples is weighted to calculate the weighted kernel covariance matrix. This matrix reflects the covariance relationship of the samples in the mapped feature space, and the weights highlight the contributions of important samples.
[0106] Then, eigenvalue decomposition is performed on the weighted kernel covariance matrix. Eigenvalue decomposition breaks down the matrix into several eigenvalues and corresponding eigenvectors. By selecting the eigenvectors corresponding to the first few larger eigenvalues, the main principal component directions are extracted. These principal component directions form a mapping matrix, which reduces the dimensionality of the eigenvectors in the high-dimensional mapping space to the lower-dimensional principal component space. This preserves the important information of the data while reducing the dimensionality, improving the efficiency and stability of subsequent analysis.
[0107] To ensure the model's generalization ability and accuracy, cross-validation is used to fine-tune the parameters in the kernel function. Specifically, multiple possible parameter combinations are set, such as the bandwidth parameter of the radial basis function. The sample set is divided into several folds, and the model's performance is repeatedly trained and validated. The parameter combination that best performs on the validation set is then selected as the optimal parameter.
[0108] Finally, the obtained mapping matrix and optimal kernel function parameters are saved, completing the construction of the kernel function principal component analysis model. This model can map the color features of the input target food image to the principal component space, achieving effective nonlinear dimensionality reduction and feature extraction, and providing accurate principal component feature vectors for subsequent freshness determination.
[0109] In some embodiments of this application, several candidate values for distance influence coefficients are set, and the similarity of each candidate value is calculated. Unsupervised clustering is performed on the similarity, and the candidate value with the largest ratio of intra-class average distance to inter-class average distance in the clustering results is selected as the value of the distance influence coefficient.
[0110] It should be noted that, firstly, a set of candidate values for the distance influence coefficient is pre-defined. These candidate values cover a reasonable range, for example, gradually increasing from smaller values to larger ones, to ensure coverage of different levels of distance sensitivity. The distance influence coefficient is a parameter in the exponential decay function used to convert Mahalanobis distance into similarity, and it directly affects the similarity calculation results.
[0111] Next, for each candidate distance influence coefficient, the similarity between the target food and standard samples of each freshness grade is calculated based on this coefficient. Specifically, a set of similarity values is calculated using the aforementioned Mahalanobis distance and candidate coefficients. These similarities reflect the matching degree between the target food and various standard samples under different coefficients.
[0112] Subsequently, the similarity sets corresponding to all candidate coefficients are used as input data for unsupervised clustering analysis. Commonly used unsupervised clustering algorithms include K-means clustering and hierarchical clustering. The purpose of clustering is to divide candidate coefficients into several categories based on the distribution characteristics of similarity, in order to identify which coefficients can make the similarity data exhibit a more obvious grouping structure.
[0113] After clustering is completed, for each clustering result, the intra-cluster average distance and the inter-cluster average distance are calculated. The intra-cluster average distance represents the average distance between data points within the same cluster, reflecting the compactness of the cluster; the inter-cluster average distance represents the average distance between different clusters, reflecting the separation of the clustering results. Ideally, the clustering result should have the intra-cluster distance as small as possible and the inter-cluster distance as large as possible.
[0114] Finally, the ratio of the average intra-cluster distance to the average inter-cluster distance is calculated based on the clustering results. A larger ratio indicates a more pronounced clustering structure, greater inter-cluster differences, and more clustered intra-cluster data. The candidate distance influence coefficient corresponding to the cluster with the largest ratio is selected as the final determined distance influence coefficient value.
[0115] Using the above method, the embodiments of the present invention can automatically select the distance influence coefficient that best distinguishes the freshness level of food from multiple candidate values, ensuring that the similarity calculated by the exponential decay function is more accurate and stable, and can adaptively select the optimal parameters without relying on human experience.
[0116] See Figure 2 As shown, this embodiment of the invention also proposes an image recognition-based food freshness detection system for the above method, comprising:
[0117] The acquisition module is configured to acquire an initial image of the target food, which includes the target food and a white reference card. The initial image is then white-balanced based on the white reference card to obtain a corrected image.
[0118] The segmentation module is configured to segment the target region of the corrected image, extract the main region of the target food, and remove background interference information.
[0119] The feature construction module is configured to perform pixel sampling and color space conversion on the main region image of the target food to obtain the redness value, yellowness value and brightness value in the Lab color space, the hue angle in the HSV color space, and the red-green ratio value in the RGB color space, which constitute the image color feature set.
[0120] The calculation module is configured to establish a difference feature vector based on the image color feature set and a preset standard sample, compare the deviation of the target food from the standard sample in multidimensional color features, and determine the freshness level of the target food based on the degree of deviation.
[0121] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for detecting food freshness based on image recognition, characterized in that, include: An initial image of the target food is acquired, the initial image including the target food and a white reference card. The initial image is then white-balance corrected based on the white reference card to obtain a corrected image. The corrected image is segmented into target regions to extract the main target food region and remove background interference information; The main area image of the target food is sampled and converted in color space to obtain the redness value, yellowness value and brightness value in Lab color space, the hue angle in HSV color space, and the red-green ratio value in RGB color space, which constitute the image color feature set. Based on the image color feature set and the preset standard samples, a difference feature vector is established, and the deviation of the target food from the standard samples in multidimensional color features is compared. The freshness grade of the target food is determined based on the degree of deviation.
2. The food freshness detection method based on image recognition according to claim 1, characterized in that, The white balance correction includes: The R, G, and B values of the white reference card region in the initial image are obtained respectively, and the R, G, and B values are compared with the theoretical white value to calculate the R channel gain, G channel gain, and B channel gain. The R, G, and B channels of the initial image are multiplied by the corresponding R channel gain, G channel gain, and B channel gain to obtain the corrected image.
3. The food freshness detection method based on image recognition according to claim 2, characterized in that, When performing target region segmentation on the corrected image, the following steps are included: The brightness value of each pixel in the corrected image is judged one by one. Pixels with brightness values higher than or equal to a preset brightness threshold and saturation values lower than a preset saturation threshold are selected and removed. The removal area is calculated. When the removal area is greater than or equal to 0.5 times the area of the corrected image, the lighting conditions and / or acquisition position when the initial image was acquired are adjusted. Obtain the redness and yellowness values of the remaining areas in the corrected image, calculate the Euclidean distance between the redness and yellowness values and the standard redness and yellowness values in the standard food sample, and obtain the maximum Euclidean distance; The Euclidean distance is compared with 0.95 times the maximum Euclidean distance, and pixels that are greater than 0.95 times the maximum Euclidean distance are removed. By using an edge detection algorithm to remove non-food areas from the corrected image, the main region of the target food is obtained.
4. The food freshness detection method based on image recognition according to claim 3, characterized in that, For each pixel in the remaining region, denote it as (a i b i The Euclidean distance is calculated using the following relationship: ; Among them, D i Let a represent the Euclidean distance of the i-th pixel. i With b i Let a0 and b0 represent the redness and yellowness values of the i-th pixel, respectively, and let b0 represent the standard redness and yellowness values in the standard food sample, respectively.
5. The food freshness detection method based on image recognition according to claim 4, characterized in that, Based on the image color feature set and preset standard samples, a difference feature vector is established, and the deviation of the target food from the standard samples in multidimensional color features is compared, including: The image color feature set of the target food is input into the trained kernel function principal component analysis model, and nonlinear dimensionality reduction is performed to obtain the principal component feature vector of the target food. The image color feature set of standard samples is obtained, and the dimensionality is reduced by the kernel function principal component analysis model to obtain the mean vector and covariance matrix of the principal component features of standard samples corresponding to different freshness levels. The Mahalanobis distance between the principal component feature vector of the target food and the mean principal component feature vector of the standard samples under each freshness grade is calculated. The Mahalanobis distance is converted into similarity based on the exponential decay function, and the freshness grade is determined based on the similarity.
6. The food freshness detection method based on image recognition according to claim 5, characterized in that, The Mahalanobis distance satisfies the following relationship: ; Among them, D M,j Let represent the Mahalanobis distance at the j-th freshness level, x represent the principal component eigenvector of the target food, μ represent the mean of the principal component eigenvectors of several standard samples in any freshness level, T represent the transpose of the matrix, and S represent the covariance matrix of the principal component eigenvectors of the standard samples in any freshness level.
7. The food freshness detection method based on image recognition according to claim 6, characterized in that, The Mahalanobis distance is converted into similarity based on the exponential decay function using the following relationship: ; Among them, F j Let D represent the similarity at the j-th level of freshness, α represent the distance influence coefficient, and D... M,j This represents the Mahalanobis distance at the j-th level of freshness; Based on the similarity corresponding to each freshness grade, the freshness grade with the highest similarity is selected as the final freshness grade of the target food.
8. The food freshness detection method based on image recognition according to claim 7, characterized in that, The kernel function principal component analysis model was obtained through the following method: An image library containing standard samples with multiple freshness levels is constructed. Image color feature sets for each standard sample are extracted, and sample weights are assigned to each standard sample's image color feature set to construct a weighted sample set. A radial basis function is selected as the kernel function, and a weighted kernel covariance matrix is constructed using the sample weights. Eigenvalue decomposition is performed on the weighted kernel covariance matrix to extract principal component directions and mapping matrices. The parameters of different kernel functions are tested based on cross-validation to determine the optimal parameter combination. The mapping matrix and kernel function parameters are saved to construct a kernel function principal component analysis model.
9. The food freshness detection method based on image recognition according to claim 8, characterized in that, Several candidate values for the distance influence coefficient are set, and the similarity of each candidate value is calculated. Unsupervised clustering is performed on the similarity, and the candidate value with the largest ratio of the average intra-class distance to the average inter-class distance in the clustering results is selected as the value of the distance influence coefficient.
10. A food freshness detection system based on image recognition, used to implement the method according to any one of claims 1-9, characterized in that, include: The acquisition module is configured to acquire an initial image of the target food, which includes the target food and a white reference card. The initial image is then white-balanced based on the white reference card to obtain a corrected image. The segmentation module is configured to segment the target region of the corrected image, extract the main region of the target food, and remove background interference information. The feature construction module is configured to perform pixel sampling and color space conversion on the main region image of the target food to obtain the redness value, yellowness value and brightness value in the Lab color space, the hue angle in the HSV color space, and the red-green ratio value in the RGB color space, which constitute the image color feature set. The calculation module is configured to establish a difference feature vector based on the image color feature set and a preset standard sample, compare the deviation of the target food from the standard sample in multidimensional color features, and determine the freshness level of the target food based on the degree of deviation.
Citation Information
Patent Citations
Nondestructive determining device and determining method for freshness of fresh livestock meat
CN104049068A
Semantic segmentation-based food material image processing method and device, equipment and medium
CN117975440A