Food freshness detection method and system based on image recognition

Through white balance correction and multi-step target area segmentation combined with multi-color space feature extraction, the problems of color deviation and background interference in image recognition methods are solved, and high precision and stability of food freshness detection are achieved, which is suitable for rapid detection of various food types.

CN120672874AActive Publication Date: 2025-09-19菏泽市行政审批踏勘评审中心

Patent Information

Application Number
CN202510656833.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-21
Publication Date
2025-09-19
Estimated Expiration
2045-05-21

AI Technical Summary

Technical Problem

Existing image recognition methods have problems in food freshness detection, such as image color deviation, background interference, and difficulty in target extraction, which affect the accuracy and objectivity of detection.

Method used

White balance correction, multi-step target area segmentation and multi-color space feature extraction are adopted, combined with the weighted kernel function principal component analysis model, and Euclidean distance and Mahalanobis distance calculation are used to accurately extract food color features and determine the freshness level.

Benefits of technology

It improves the accuracy and stability of food freshness detection, adapts to different environments and food types, realizes non-destructive and rapid detection, reduces detection costs, and is suitable for on-site applications.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120672874A_ABST
    Figure CN120672874A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of image recognition, and discloses a food freshness detection method and system based on image recognition, and the method comprises the steps: collecting an initial image, carrying out the white balance correction through a white reference card, and obtaining a corrected image with an accurate color; performing target area segmentation on the corrected image, extracting a food main area and eliminating background interference; pixel sampling is carried out on the main area image, the main area image is converted to Lab, HSV and RGB color spaces, and redness, yellowness, brightness, hue angle and red-green ratio are extracted to form a multi-dimensional color feature set; establishing a difference feature vector based on the feature set and a preset standard sample, and comparing the deviation degrees of the target food and the standard sample on the multi-dimensional color features; and finally, judging the freshness grade of the food according to the deviation degree. According to the invention, lossless and rapid food freshness grading detection is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of image processing, and in particular to a food freshness detection method and system based on image recognition. Background Art

[0002] Food freshness is a core indicator of food quality and safety. This is particularly true for perishable foods like fresh fruits and vegetables, meat, and aquatic products, as changes in freshness directly impact consumers' sensory experience and food safety. Currently, the following methods are primarily used to assess food freshness: Sensory evaluation methods typically rely on subjective sensory input, such as vision and smell, to assess food freshness. While simple to perform, these methods are subject to significant influence from individual experience and subjective judgment, lacking objectivity and reproducibility. Physical and chemical indicators, such as those based on volatile basic nitrogen (TVB-N), pH, and peroxide value, can reflect chemical changes during food spoilage. However, these methods typically require sampling, destructive procedures, complex experimental procedures, and specialized equipment, making them unsuitable for rapid, non-destructive testing or field applications. Intelligent sensors and gas detection methods assess freshness by monitoring changes in the atmosphere surrounding or on the surface of food. While these methods offer certain non-contact and real-time capabilities, they are susceptible to environmental interference, exhibit unstable detection accuracy, and exhibit high equipment costs.

[0003] In recent years, with the development of image recognition and computer vision technologies, analyzing food surface characteristics using image data has become a potential approach for contactless, rapid freshness testing. 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 collected 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 accurately segment, which can easily cause color feature confusion and affect the judgment results.

[0004] Therefore, there is an urgent need for a food freshness detection method based on image recognition 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 in the existing freshness image detection technology, the target food area in the complex background is difficult to accurately segment, which easily causes color feature confusion and affects the judgment result.

[0006] In one aspect, the present invention proposes a method for detecting food freshness based on image recognition, comprising: Acquire an initial image of the target food, the initial image including the target food and a white reference card, and perform white balance correction on the initial image based on the white reference card to obtain a corrected image; Performing target area segmentation on the corrected image, extracting the target food main area and removing background interference information; Perform pixel sampling and color space conversion on the target food main area image 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 in the RGB color space to form an image color feature set; Establishing a difference feature vector based on the image color feature set and a preset standard sample, and comparing the degree of deviation between the target food and the standard sample in multidimensional color features; The freshness level of the target food is determined according to the degree of deviation.

[0007] Furthermore, the white balance correction includes: Obtain the R value, G value, and B value of the white reference card area in the initial image respectively, and compare the R value, G value, and B value 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 original image are respectively multiplied by the R channel gain, the G channel gain, and the B channel gain to obtain a corrected image.

[0008] Furthermore, when performing target region segmentation on the corrected image, the following steps are included: Determine the brightness value of each pixel in the corrected image one by one, select pixels in the image whose brightness value is greater than or equal to a preset brightness threshold and whose saturation is less than a preset saturation threshold, remove them, and calculate the removal area; when the removal area is greater than or equal to 0.5 times the area of ​​the corrected image, adjust the lighting conditions and / or acquisition position when acquiring the initial image; Obtaining redness values ​​and yellowness values ​​of the remaining area in the corrected image, calculating the Euclidean distance between the redness values ​​and yellowness values ​​and the standard redness values ​​and standard yellowness values ​​in the standard food sample, and obtaining the maximum Euclidean distance; Comparing the Euclidean distance with 0.95 times the maximum Euclidean distance, and eliminating pixels whose distance is greater than 0.95 times the maximum Euclidean distance; The edge detection algorithm is used to eliminate the non-food area in the corrected image and obtain the target food main area.

[0009] Furthermore, each pixel in the remaining area is recorded as (a i , b i ), the Euclidean distance is calculated by the following relationship: ; Among them, D i represents the Euclidean distance of the i-th pixel, a i with b i They represent the redness value and yellowness value of the i-th pixel respectively, a0 and b0 represent the standard redness value and standard yellowness value in the standard food sample respectively.

[0010] Furthermore, a difference feature vector is established based on the image color feature set and a preset standard sample, and the deviation degree of the target food and the standard sample in the multidimensional color feature is compared, including: Input the image color feature set of the target food into the trained kernel function principal component analysis model, perform nonlinear dimensionality reduction, and obtain the principal component feature vector of the target food; Obtaining a set of image color features of standard samples, performing dimensionality reduction using the kernel function principal component analysis model, and obtaining principal component feature mean vectors and covariance matrices of standard samples corresponding to different freshness levels; The Mahalanobis distance between the principal component feature vector of the target food and the principal component feature mean vector of the standard sample at each freshness level is calculated, and the Mahalanobis distance is converted into similarity based on the exponential decay function, and the freshness level is determined based on the similarity.

[0011] Furthermore, the Mahalanobis distance satisfies the following relationship: ; Among them, D M,j represents the Mahalanobis distance at the jth level of freshness, x represents the principal component eigenvector of the target food, μ represents the mean of the principal component eigenvectors of several standard samples in any freshness level, T represents the transpose of the matrix, and S represents the covariance matrix of the principal component eigenvectors of the standard samples of any freshness level.

[0012] Furthermore, the Mahalanobis distance is converted to similarity based on the exponential decay function through the following relationship: ; Among them, F j represents the similarity at the jth level of freshness, α represents the distance influence coefficient, D M,j represents the Mahalanobis distance at the jth level of freshness; According to the similarities corresponding to each freshness level, the freshness level with the highest similarity is selected as the final freshness level of the target food.

[0013] Furthermore, the kernel function principal component analysis model is obtained by the following method: An image library containing standard samples of multiple freshness levels was constructed, and the image color feature set of each standard sample was extracted. Sample weights were set for each standard sample image color feature set to construct a weighted sample set. The radial basis function was selected as the kernel function, and a weighted kernel covariance matrix was constructed using the sample weights. The weighted kernel covariance matrix was subjected to eigenvalue decomposition to extract the principal component directions and mapping matrix. The parameters of different kernel functions were tested based on cross-validation to determine the optimal parameter combination. The mapping matrix and kernel function parameters were saved, and a kernel function principal component analysis model was constructed.

[0014] Furthermore, several candidate values ​​of 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 result is selected as the value of the distance influence coefficient.

[0015] Compared with the prior art, the present invention has the following beneficial effects: By introducing a white reference card during image acquisition and performing white balance correction based on it, the effects of ambient lighting variations and device parameter differences on image color are effectively eliminated. The resulting corrected image colors are more accurate and consistent, improving the stability and reliability of subsequent color feature extraction, avoiding color deviations caused by uneven lighting, and addressing the color instability issue inherent in traditional image recognition methods. A multi-step process, including brightness and saturation thresholding, outlier pixel removal based on Euclidean distance, and an edge detection algorithm, accurately extracts the primary target food area while effectively removing interference from complex backgrounds and non-food areas. This multi-step screening and elimination mechanism ensures the purity of color feature sampling, preventing background color from obscuring the food's primary features, thereby improving the accuracy and stability of freshness determination. Multidimensional color features are extracted in Lab, HSV, and RGB color spaces, enabling a multi-faceted and comprehensive description of the food's apparent color. This multi-color space feature fusion overcomes the limitations of a single color space, fully capturing subtle variations in food color, enhancing the ability to capture freshness details, and increasing the detection model's adaptability to diverse food types and states. A weighted kernel principal component analysis model, combining sample weights and radial basis kernel functions, performs nonlinear dimensionality reduction on multidimensional color features and extracts principal component eigenvectors, effectively compressing and refining complex high-dimensional color data. Compared to traditional linear PCA, this method better captures nonlinear relationships between color features, improving the discrimination and robustness of freshness classification. By calculating the Mahalanobis distance between the target food and standard samples of different freshness grades and converting the distance into similarity using an exponential decay function, a reasonable similarity metric is established. This matching method, based on statistics and distance metrics, considers the correlation and covariance structure between features, reducing the probability of false positives and achieving more scientific and accurate freshness classification. Unsupervised cluster analysis analyzes the similarity performance of different distance influence coefficients to automatically select the optimal distance influence coefficient parameters, avoiding subjective bias caused by manual settings. This mechanism improves the model's adaptability to different food types and collection environments, ensuring the system's stability and reliability in a variety of practical application scenarios. The entire inspection process is based on image acquisition and computational analysis, eliminating the need for destructive sampling or complex physical and chemical testing equipment. The inspection process is simple and rapid, suitable for real-time application on-site or on the production line. It greatly improves the detection efficiency and practical value, reduces the detection cost, and meets the food industry's demand for rapid response to freshness detection.

[0016] On the other hand, the present invention also proposes a food freshness detection system based on image recognition for the above method, comprising: an acquisition module configured to acquire an initial image of a target food, the initial image including the target food and a white reference card, and perform white balance correction on the initial image based on the white reference card to obtain a corrected image; a segmentation module configured to segment the corrected image into target regions, extract the target food main region, and remove background interference information; The feature construction module is configured to perform pixel sampling and color space conversion on the target food main area image 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 in the RGB color space to form an 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 degree of deviation between the target food and the standard sample in multidimensional color features; and determine the freshness level of the target food based on the degree of deviation.

[0017] It should be noted that the food freshness detection method and system based on image recognition proposed in the present invention have the same beneficial effects and will not be described in detail here. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Various other advantages and benefits will become apparent to those skilled in the art upon reading the detailed description of the preferred embodiment below. The accompanying drawings are for illustration purposes only and are not to be considered as limiting the present invention. The same reference symbols are used throughout the drawings to represent the same components. In the drawings: Figure 1 This is a flowchart of a food freshness detection method based on image recognition provided by an embodiment of the present invention.

[0019] Figure 2 This is a functional block diagram of a food freshness detection system based on image recognition provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0020] The exemplary embodiments disclosed in the present application will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art. It should be noted that, unless there is a conflict, the embodiments of the present invention and the features described in the embodiments can be combined with each other. The present invention will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0021] See Figure 1 As shown, an embodiment of the present invention proposes a method for detecting food freshness based on image recognition, comprising: S1: Acquire an initial image of the target food, which includes the target food and a white reference card, and perform white balance correction on the initial image based on the white reference card to obtain a corrected image; S2: Segment the target area of ​​the corrected image, extract the main area of ​​the target food and remove background interference information; S3: Perform pixel sampling and color space conversion on the target food main area image 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 in the RGB color space to form the image color feature set; S4: Establish a difference feature vector based on the image color feature set and the preset standard sample, and compare the deviation degree of the target food and the standard sample in the multidimensional color feature; S5: Determine the freshness level of the target food based on the degree of deviation.

[0022] It is important to note that the inclusion of a white reference card for white balance correction effectively eliminates the impact of ambient lighting variations on image color, ensuring faithful color reproduction of the captured image and improving the accuracy of subsequent color feature extraction, thereby enhancing the objectivity of freshness assessment. Target region segmentation technology extracts the primary food region and removes background information, preventing interference from complex backgrounds on color features. This improves the specificity and reliability of color feature analysis, ensuring more accurate detection results. A comprehensive analysis combining multidimensional color features (redness, yellowness, brightness, hue angle, and red-green ratio) in Lab, HSV, and RGB color spaces comprehensively reflects surface color variations in food, enhancing the sensitivity and discrimination of freshness assessment. A differential feature vector is established based on the image color feature set and a standard sample, quantifying the multidimensional color deviation between the target food and the standard sample. This helps to capture variations in food freshness and achieve refined grading. The entire method, leveraging image recognition and multidimensional color feature analysis, eliminates the need for destructive sampling and complex chemical testing. It offers rapid, non-contact, and automated detection capabilities, making it suitable for a variety of applications, from on-site to industrial production lines, improving detection efficiency and practical value.

[0023] In some embodiments of the present application, white balance correction includes: Obtain the R value, G value, and B value of the white reference card area in the initial image respectively, and compare the R value, G value, and B value 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 original image are multiplied by the R channel gain, G channel gain, and B channel gain respectively to obtain a corrected image.

[0024] It should be noted that the white reference card area is accurately located in the captured initial image, and the average red, green, and blue channel values ​​of all pixels in this area are extracted. These actual measured red, green, and blue values ​​are then compared with the theoretical ideal white values ​​to calculate the red, green, and blue channel gains. The theoretical white value here represents the red, green, and blue values ​​of an ideal pure white, typically the maximum value, serving as a 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, thereby color-adjusting the image and generating a corrected image that has undergone white balance correction.

[0025] This white balance correction method based on a white reference card brings several significant benefits: Firstly, it effectively eliminates color deviations caused by varying light sources. Whether shooting under complex lighting conditions, such as natural light, fluorescent lighting, or incandescent lighting, the standard white reference card allows for dynamic color correction of the image, ensuring more realistic and natural color.

[0026] Secondly, through precise color correction, subsequent color feature extraction of food images yields more accurate and stable key metrics such as redness, yellowness, brightness, hue angle, and red-green ratio. This significantly enhances the credibility of image color information and reduces feature errors caused by lighting variations.

[0027] In addition, this method enhances the robustness and stability of food freshness detection, so that no matter how the detection environment changes, the system can output consistent and reliable freshness assessment results, improving the practicality and promotion value of the detection.

[0028] Finally, this correction method is simple to use and suitable for rapid implementation in the field. Simply place a standard white reference card in the shooting scene. By automatically calculating the gain and applying it to the image, white balance correction is quickly achieved, providing a solid foundation for subsequent image processing and freshness assessment.

[0029] In some embodiments of the present application, when performing target area segmentation on the corrected image, the process includes: Determine the brightness value of each pixel in the corrected image one by one, select pixels in the image with brightness values ​​greater than or equal to a preset brightness threshold and saturation less than a preset saturation threshold, remove them, and calculate the removal area; when the removal area is greater than or equal to 0.5 times the area of ​​the corrected image, adjust the lighting conditions and / or acquisition position when acquiring the initial image; Obtain the redness value and yellowness value of the remaining area in the corrected image, calculate the Euclidean distance between the redness value, the yellowness value and the standard redness value, the standard yellowness value in the standard food sample, and obtain the maximum Euclidean distance; Compare the Euclidean distance with 0.95 times the maximum Euclidean distance, and remove pixels whose distance is greater than 0.95 times the maximum Euclidean distance; The edge detection algorithm is used to eliminate the non-food area in the corrected image and obtain the target food main area.

[0030] As you can understand, first, the brightness value of each pixel in the corrected image is determined one by one. The system then selects pixels in the image whose brightness value is greater than or equal to a preset brightness threshold and whose saturation value is less than a preset saturation threshold. These pixels are considered as possible non-target areas and are removed. At the same time, the area occupied by the removed pixels is calculated.

[0031] When the eliminated area reaches or exceeds half of the total area of ​​the corrected image, it indicates that there is a large range of lighting anomalies or background interference in the currently acquired image. At this time, the system will automatically prompt to adjust the lighting conditions and / or acquisition angle and position when acquiring the initial image to ensure the subsequent image quality and segmentation effect.

[0032] 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 the pre-set standard food sample, calculates the Euclidean distance between the two, and measures the similarity of color features.

[0033] The system then calculates the maximum Euclidean distance of all pixels and compares each pixel's Euclidean distance to the 95th percentile of this maximum value. Pixels with Euclidean distances exceeding this threshold are considered to have a significant color difference from the target food and are removed to further optimize the accuracy of the target area.

[0034] Finally, the edge detection algorithm is used to identify and eliminate the non-food areas in the corrected image, remove the background and messy edges, and finally obtain a complete and accurate target food main area.

[0035] By applying dual thresholds to brightness and saturation, areas with excessively strong or weak lighting, background reflections, and low saturation are effectively filtered out, reducing interference from irrelevant pixels at the source and improving the accuracy and robustness of image preprocessing. By calculating the Euclidean distance between redness and yellowness values ​​and combining them with the color characteristics of standard samples, pixels with significant color differences from the target food can be accurately identified, achieving fine elimination of non-target areas and greatly enhancing the pertinence and accuracy of the segmentation results. Edge detection technology is used to further remove non-food areas from the image, ensuring the integrity and clarity of the target area boundaries, avoiding interference from background noise on subsequent analysis, and improving the accuracy of the overall segmentation. The ratio judgment mechanism of the eliminated area to the image area can dynamically monitor the lighting quality of the acquisition environment. When lighting conditions are poor, it automatically reminds you to adjust the acquisition settings to ensure the stability of the input image quality and fundamentally guarantee the reliability and consistency of subsequent detection.

[0036] In some embodiments of the present application, each pixel in the remaining area is recorded as (a i , b i ), the Euclidean distance is calculated by the following relationship: ; Among them, D i represents the Euclidean distance of the i-th pixel, a i with b i They represent the redness value and yellowness value of the i-th pixel respectively, a0 and b0 represent the standard redness value and standard yellowness value in the standard food sample respectively.

[0037] It's important to note that 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 accurately reflects the degree of deviation of pixel color from the standard color, effectively distinguishing target food areas from non-target areas or abnormal areas.

[0038] The beneficial effects are mainly reflected in the following aspects: First, the Euclidean distance constructed by the 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 indicator.

[0039] Secondly, by calculating the distance between the color features of each pixel and the color of the standard sample, fine-grained analysis at the pixel level is achieved, which helps to eliminate noise pixels and non-food pixels with abnormal colors and improve the accuracy of target area extraction.

[0040] Thirdly, this method can flexibly adapt to the color standards of different batches and varieties of food, has good versatility and scalability, and can meet the freshness detection needs of a variety of foods.

[0041] In addition, the quantitative characteristics of this distance can also provide a scientific basis for subsequent freshness level determination, enhancing the objectivity and reliability of the test results.

[0042] In some embodiments of the present application, a difference feature vector is established based on the image color feature set and a preset standard sample, and the degree of deviation of the target food and the standard sample in multidimensional color features is compared, including: Input the image color feature set of the target food into the trained kernel function principal component analysis model, perform nonlinear dimensionality reduction, and obtain the principal component feature vector of the target food; Obtain the image color feature set of the standard samples, use the kernel function principal component analysis model to reduce the dimension, and obtain the principal component feature mean vector and covariance matrix of the standard samples corresponding to different freshness levels; The Mahalanobis distance between the principal component feature vector of the target food and the principal component feature mean vector of the standard sample at each freshness level is calculated, and the Mahalanobis distance is converted into similarity based on the exponential decay function, and the freshness level is determined based on the similarity.

[0043] Specifically, the first step is to input the color feature set of the target food image into a pre-trained kernel principal component analysis model. Kernel principal component analysis is a nonlinear dimensionality reduction technique that uses kernel mapping to map the input features into a high-dimensional space. Principal component analysis is then performed within this high-dimensional space to extract the key features of the data. After training, this model effectively captures the complex nonlinear relationships between color features, improving feature representation. After executing the model, the principal component feature vector of the target food is obtained. This vector represents the key features after dimensionality reduction and summarizes the main variations in the target food's color information.

[0044] In the second step, a collection of color features from standard food sample images of varying freshness levels was pre-collected and organized, and the same kernel function principal component analysis (PCA) dimensionality reduction process was applied to each sample. For each standard sample of each freshness level, the mean vector and covariance matrix of the principal component eigenvectors were calculated. The mean vector represents the central tendency of the color features for that freshness level, while the covariance matrix reflects the distribution and correlation of color features within that category.

[0045] The third step is to calculate the Mahalanobis distance between the principal component eigenvector of the target food and the principal component mean vector of the standard samples for each freshness grade. The Mahalanobis distance is a distance metric that considers sample distribution characteristics. It effectively measures the statistical distance between samples and reflects the degree of similarity between the target food's color characteristics and the standard samples for each grade.

[0046] The fourth step is to convert the calculated Mahalanobis distance into a similarity value using an exponential decay function. The exponential decay function maps distance values ​​to similarities between 0 and 1, with smaller distances indicating higher similarity and smaller distances indicating lower similarity. This conversion helps transform the quantitative value of the Mahalanobis distance into a more intuitive similarity metric.

[0047] 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 freshness grade determination result of the target food.

[0048] In some embodiments of the present application, the Mahalanobis distance satisfies the following relationship: ; Among them, D M,j represents the Mahalanobis distance at the jth level of freshness, x represents the principal component eigenvector of the target food, μ represents the mean of the principal component eigenvectors of several standard samples in any freshness level, T represents the transpose of the matrix, and S represents the covariance matrix of the principal component eigenvectors of the standard samples of any freshness level.

[0049] It should be noted that the Mahalanobis distance not only measures the distance between two eigenvectors, but also takes into account the covariance matrix of the sample features of each freshness level, which can reflect the correlation and distribution form of the sample features. This makes the distance calculation more reasonable, avoids the errors that may be caused by the simple Euclidean distance when there is correlation, and improves the accuracy of freshness judgment. Through weighted adjustment of the covariance matrix, the Mahalanobis distance can more sensitively capture the subtle differences in the feature distribution between different freshness levels, thereby enhancing the discrimination between the levels and facilitating a more detailed and reliable freshness grading. This method is suitable for complex data with high sample feature dimensions and correlations. It enhances the adaptability and stability of the detection model to multidimensional color features, and improves the versatility and robustness of freshness detection under different food types and collection conditions.

[0050] In some embodiments of the present application, the Mahalanobis distance is converted into similarity based on an exponential decay function through the following relationship: ; Among them, F j represents the similarity at the jth level of freshness, α represents the distance influence coefficient, D M,j represents the Mahalanobis distance at the jth level of freshness; According to the similarities corresponding to each freshness level, the freshness level with the highest similarity is selected as the final freshness level of the target food.

[0051] It should be noted that the exponential decay function can map non-negative values ​​such as the Mahalanobis distance to a similarity value. As the distance increases, the similarity gradually decreases, ensuring the smoothness and continuity of the freshness judgment and avoiding the judgment mutation problem caused by the hard threshold. The parameter α, as the distance influence coefficient, adjusts the influence of distance on similarity, allowing the model to flexibly adjust the sensitivity to feature differences according to actual needs, thereby improving the flexibility and adaptability of the judgment. Converting distance to similarity makes the judgment result more intuitive and interpretable, facilitates subsequent comparison and selection based on similarity, and clearly reflects the degree of match between the target food and each freshness level. By selecting the freshness level with the highest similarity as the final judgment result, it effectively avoids the abnormal judgment that may be caused by relying solely on the minimum distance value, and enhances the accuracy and stability of freshness grading.

[0052] In some embodiments of the present application, the kernel function principal component analysis model is obtained by the following method: An image library containing standard samples of multiple freshness levels was constructed, and the image color feature set of each standard sample was extracted. Sample weights were set for each standard sample image color feature set to construct a weighted sample set. The radial basis function was selected as the kernel function, and a weighted kernel covariance matrix was constructed using the sample weights. The weighted kernel covariance matrix was subjected to eigenvalue decomposition to extract the principal component directions and mapping matrix. The parameters of different kernel functions were tested based on cross-validation to determine the optimal parameter combination. The mapping matrix and kernel function parameters were saved, and a kernel function principal component analysis model was constructed.

[0053] Specifically, a library of standard sample images of foods of varying freshness grades is first prepared. For each standard sample in the library, the method of the present invention is used to extract a set of image color features. These color features include metrics in various color spaces, such as redness, yellowness, and brightness. To enhance the representativeness and robustness of the model, each standard sample is assigned a weight based on its importance and representativeness, forming a weighted sample set.

[0054] Next, we select the radial basis function as the kernel function. This function effectively maps the original feature space to a higher-dimensional feature space, thereby capturing more complex nonlinear structures in the data. Using sample weights, we weight the kernel matrix of each sample to calculate the weighted kernel covariance matrix. This matrix reflects the covariance relationship of the samples in the mapped feature space and, when combined with the weights, can highlight the contributions of important samples.

[0055] Next, the weighted kernel covariance matrix is ​​subjected to eigenvalue decomposition. Eigenvalue decomposition decomposes 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 eigenvectors in the high-dimensional mapping space to the lower-dimensional principal component space. This method preserves important data information while reducing the dimensionality, improving the efficiency and stability of subsequent analysis.

[0056] To ensure the generalization and accuracy of the model, we used cross-validation to fine-tune the parameters in the kernel function. Specifically, we set multiple possible parameter combinations, such as the bandwidth parameter of the radial basis function, divided the sample set into several folds, and repeatedly trained and validated the model's performance. The optimal parameter combination was selected as the one that performed best on the validation set.

[0057] Finally, the resulting 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 input target food image color features into the principal component space, achieving effective nonlinear dimensionality reduction and feature extraction, and providing accurate principal component feature vectors for subsequent freshness determination.

[0058] In some embodiments of the present application, several candidate values ​​of the distance influence coefficient are set, and the similarity of each candidate value is calculated, and the similarity is subjected to unsupervised clustering processing, and the candidate value with the largest ratio of the average distance within the class to the average distance between classes in the clustering result is selected as the value of the distance influence coefficient.

[0059] It should be noted that, first, a set of candidate values ​​for the distance influence coefficient is pre-set. These candidate values ​​cover a reasonable range of values, for example, gradually increasing from smaller values ​​to larger values, to ensure coverage of varying degrees of distance sensitivity. The distance influence coefficient is a parameter in the exponential decay function used to convert the Mahalanobis distance to similarity, and it directly affects the similarity calculation result.

[0060] Next, for each candidate distance influence coefficient, the similarity between the target food and the standard samples for each freshness grade is calculated based on that coefficient. Specifically, a set of similarity values ​​is calculated using the aforementioned Mahalanobis distance and the candidate coefficients. These similarities reflect the matching between the target food and the various standard samples at different coefficients.

[0061] Subsequently, the similarity sets corresponding to all candidate coefficients are used as data input for unsupervised cluster analysis. Common unsupervised clustering algorithms include K-means clustering and hierarchical clustering. The goal of clustering is to divide candidate coefficients into several categories based on the distribution characteristics of similarity, thereby identifying which coefficients can make the similarity data show a more obvious grouping structure.

[0062] After clustering is complete, the average intra-class distance and the average inter-class distance are calculated for each cluster result. The average intra-class distance represents the average distance between data points in the same class, reflecting the compactness of the class; the average inter-class distance represents the average distance between different classes, reflecting the degree of separation of the cluster results. Ideally, the intra-class distance should be as small as possible and the inter-class distance as large as possible.

[0063] Finally, based on each clustering result, calculate the ratio of the average intra-cluster distance to the average inter-cluster distance. The larger the ratio, the more distinct the clustering structure, the greater the inter-cluster differences, and the more concentrated the data within the cluster. The candidate distance influence coefficient corresponding to the cluster with the largest ratio is selected as the final distance influence coefficient value.

[0064] Through the above method, the embodiment of the present invention can automatically screen out the distance influence coefficient that can best distinguish the freshness level of food from multiple candidate values, ensure that the similarity calculated by the exponential decay function is more accurate and stable, and does not rely on manual experience, and can adaptively select the optimal parameters.

[0065] See Figure 2 As shown, the embodiment of the present invention further proposes a food freshness detection system based on image recognition for the above method, comprising: an acquisition module configured to acquire an initial image of a target food, the initial image including the target food and a white reference card, and perform white balance correction on the initial image based on the white reference card to obtain a corrected image; a segmentation module configured to segment the corrected image into target regions, extract the target food main region, and remove background interference information; The feature construction module is configured to perform pixel sampling and color space conversion on the target food main area image 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 in the RGB color space to form an 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 degree of deviation between the target food and the standard sample in multidimensional color features; and determine the freshness level of the target food based on the degree of deviation.

[0066] 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, ordinary technicians in the field should understand that the specific implementation methods of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be covered by the scope of protection of the claims of the present invention.

Claims

1. A food freshness detection method based on image recognition, characterized in that: include: Acquire an initial image of the target food, the initial image including the target food and a white reference card, and perform white balance correction on the initial image based on the white reference card to obtain a corrected image; Performing target area segmentation on the corrected image, extracting the target food main area and removing background interference information; Perform pixel sampling and color space conversion on the target food main area image 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 in the RGB color space to form an image color feature set; Establishing a difference feature vector based on the image color feature set and a preset standard sample, and comparing the degree of deviation between the target food and the standard sample in multidimensional color features; The freshness level of the target food is determined according to 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: Obtain the R value, G value, and B value of the white reference card area in the initial image respectively, and compare the R value, G value, and B value 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 original image are respectively multiplied by the R channel gain, the G channel gain, and the B channel gain to obtain a corrected image.

3. The food freshness detection method based on image recognition according to claim 2, characterized in that: When performing target area segmentation on the corrected image, it includes: Determine the brightness value of each pixel in the corrected image one by one, select pixels in the image whose brightness value is greater than or equal to a preset brightness threshold and whose saturation is less than a preset saturation threshold, remove them, and calculate the removal area; when the removal area is greater than or equal to 0.5 times the area of ​​the corrected image, adjust the lighting conditions and / or acquisition position when acquiring the initial image; Obtaining redness values ​​and yellowness values ​​of the remaining area in the corrected image, calculating the Euclidean distance between the redness values ​​and yellowness values ​​and the standard redness values ​​and standard yellowness values ​​in the standard food sample, and obtaining the maximum Euclidean distance; Comparing the Euclidean distance with 0.95 times the maximum Euclidean distance, and eliminating pixels whose distance is greater than 0.95 times the maximum Euclidean distance; The edge detection algorithm is used to eliminate the non-food area in the corrected image and obtain the target food main area.

4. The food freshness detection method based on image recognition according to claim 3, characterized in that: Each pixel in the remaining area is recorded as (a i , b i ), the Euclidean distance is calculated by the following relationship: ; Among them, D i represents the Euclidean distance of the i-th pixel, a i with b i They represent the redness value and yellowness value of the i-th pixel respectively, a0 and b0 represent the standard redness value and standard yellowness value in the standard food sample respectively.

5. The food freshness detection method based on image recognition according to claim 4, characterized in that: A difference feature vector is established based on the image color feature set and a preset standard sample, and the deviation degree of the target food and the standard sample in the multi-dimensional color feature is compared, including: Input the image color feature set of the target food into the trained kernel function principal component analysis model, perform nonlinear dimensionality reduction, and obtain the principal component feature vector of the target food; Obtaining a set of image color features of standard samples, performing dimensionality reduction using the kernel function principal component analysis model, and obtaining principal component feature mean vectors and covariance matrices of standard samples corresponding to different freshness levels; The Mahalanobis distance between the principal component feature vector of the target food and the principal component feature mean vector of the standard sample at each freshness level is calculated, and the Mahalanobis distance is converted into similarity based on the exponential decay function, and the freshness level 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 represents the Mahalanobis distance at the jth level of freshness, x represents the principal component eigenvector of the target food, μ represents the mean of the principal component eigenvectors of several standard samples in any freshness level, T represents the transpose of the matrix, and S represents the covariance matrix of the principal component eigenvectors of the standard samples of 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 to similarity based on an exponential decay function through the following relationship: ; Among them, F j represents the similarity at the jth level of freshness, α represents the distance influence coefficient, D M,j represents the Mahalanobis distance at the jth level of freshness; According to the similarities corresponding to each freshness level, the freshness level with the highest similarity is selected as the final freshness level 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 is obtained by the following method: An image library containing standard samples of multiple freshness levels was constructed, and the image color feature set of each standard sample was extracted. Sample weights were set for each standard sample image color feature set to construct a weighted sample set. The radial basis function was selected as the kernel function, and a weighted kernel covariance matrix was constructed using the sample weights. The weighted kernel covariance matrix was subjected to eigenvalue decomposition to extract the principal component directions and mapping matrix. The parameters of different kernel functions were tested based on cross-validation to determine the optimal parameter combination. The mapping matrix and kernel function parameters were saved, and a kernel function principal component analysis model was constructed.

9. The food freshness detection method based on image recognition according to claim 8, characterized in that: Several candidate values ​​of 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 result 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 to 9, characterized in that: include: an acquisition module configured to acquire an initial image of a target food, the initial image including the target food and a white reference card, and perform white balance correction on the initial image based on the white reference card to obtain a corrected image; a segmentation module configured to segment the corrected image into target regions, extract the target food main region, and remove background interference information; The feature construction module is configured to perform pixel sampling and color space conversion on the target food main area image 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 in the RGB color space to form an 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 degree of deviation between the target food and 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

  • Colorimetric analysis method of ion concentration detection test paper suitable for mobile phone App

    CN119762455A

  • Freshness determining method using digital camera image data

    JP2016085117A

Cited By

  • Food detection system and method based on computer vision

    CN120877277A

  • A computer vision-based food inspection system and method

    CN120877277B

  • Tennis court type determination method and device based on visual algorithm and storage medium

    CN121033560A

  • Fruit and vegetable powder color uniformity detection method and system based on image processing

    CN121190475A

  • Medicinal material quality surface image detection processing method and system

    CN121883912A