A computer vision-based food inspection system and method

The computer vision-based food inspection system utilizes modules such as image acquisition, boundary clustering, feature analysis, and contamination identification to dynamically adjust detection parameters, solving the problem of insufficient adaptability of existing systems in diverse food inspections and achieving efficient and accurate multi-scenario food quality assessment.

CN120877277BActive Publication Date: 2025-12-02SICHUAN FOOD INSPECTION INST
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511383425.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-09-26
Publication Date
2025-12-02
Estimated Expiration
2045-09-26

AI Technical Summary

Technical Problem

Existing visual inspection systems struggle to achieve modular design and dynamic adaptation when faced with diverse food inspection needs. This results in extensive redevelopment required for cross-scenario applications, and the systems are unable to quickly adapt to the inspection requirements of different foods, impacting production efficiency and inspection accuracy.

Method used

A computer vision-based food inspection system is adopted. The system extracts contour features through an image acquisition module, classifies shapes through a boundary clustering module, calculates texture complexity through a feature analysis module, locates defect positions through a coordinate extraction module, assesses the degree of contamination through a contamination identification module, and dynamically adjusts the detection configuration through a parameter optimization module to achieve multi-scenario adaptation.

Benefits of technology

It significantly improves the accuracy and adaptability of food quality testing, enabling it to quickly adapt to different food testing needs without redeveloping the system, thereby improving the testing efficiency and accuracy of the production line.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120877277B_ABST
    Figure CN120877277B_ABST
Patent Text Reader

Abstract

This invention discloses a computer vision-based food inspection system and method, relating to the field of food visual inspection and intelligent recognition technology. It includes an image acquisition module that acquires initial food image data from the production line using a pre-set image acquisition device, processes the data using an edge detection algorithm to extract contour features, and incorporates a boundary point coordinate extraction process to obtain a description of the food's appearance boundary. A boundary clustering module, based on the extracted appearance boundary description, uses a clustering algorithm to group boundary points, combining a distance metric function and cluster center initialization for iterative convergence processing. If the number of groups exceeds a preset threshold, it indicates an irregular shape, resulting in a shape classification label. This computer vision-based food inspection system and method significantly improves the accuracy and adaptability of food quality inspection, and is suitable for the quality control needs of production lines in various scenarios.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of food visual inspection and intelligent recognition technology, specifically to a food inspection system and method based on computer vision. Background Technology

[0002] Visual inspection technology plays a crucial role in the food industry, significantly improving the efficiency and accuracy of food quality control and ensuring food safety and consumer trust. With the increasing diversification of food products, from fruits and vegetables to meat and seafood, the complexity of inspection scenarios is constantly increasing, placing higher demands on the flexibility and adaptability of inspection systems. Whether it's rapid sorting on the production line or quality verification in the market, visual inspection systems must accurately identify the appearance characteristics, defects, or contamination of food to meet stringent industry standards. Technological advancements in this field are directly related to the production efficiency of the food industry and public health, thus becoming an important research direction for intelligent manufacturing and food safety.

[0003] However, existing visual inspection systems often exhibit significant limitations when facing diverse food inspection needs. Many systems are deeply customized for specific foods or scenarios during their design; for example, algorithms developed specifically for detecting surface defects in apples are difficult to directly apply to foreign object detection in seafood. This customized development leads to extensive redevelopment when applying the system across different scenarios, resulting in significant time and effort commitments. Furthermore, existing methods often lack sufficient flexibility when handling the complex appearance features of different foods, struggling to cope with variations in food shape, texture, or gloss. This not only increases development costs but also limits the system's widespread applicability in actual production. Technically, the core challenge of visual inspection systems lies in achieving modular design and dynamic adaptability. Modular design requires decomposing functions such as image processing, feature extraction, and classification into independent and reusable components.

[0004] However, the diversity of food appearances presents challenges to the development of standardized modules. The visual characteristics of different foods vary greatly; for example, the color and texture of fruits differ significantly from the slippery surface and irregular shape of seafood. This necessitates frequent parameter adjustments or redesigns of modules when processing different foods. This diversity directly presents the first technical challenge: how to ensure that modules maintain efficient versatility across different food detection scenarios without requiring significant modifications for each food. Furthermore, this versatility issue introduces another challenge: the dynamic combination of modules and the adaptability of parameter configurations. Even if a module can independently perform a specific function, how to quickly adapt to the detection needs of different foods through flexible parameter configuration and module combination remains a complex problem. For example, when detecting surface defects in strawberries, the system may need to emphasize a feature extraction module that highlights color contrast, while when detecting foreign objects in fish, it needs to emphasize a texture analysis module. The collaborative work between modules requires precise parameter adjustments and combination logic, but existing technologies often struggle to achieve rapid and dynamic adaptation in actual production, leading to low system deployment efficiency or insufficient detection accuracy. Therefore, in real-world business scenarios, a key challenge for visual inspection technology applications is how to quickly adapt to diverse inspection needs, ranging from fruits and vegetables to meat and seafood, through flexible module combinations and parameter configurations, without redeveloping the system. Taking fruit sorting as an example, production lines may need to switch between inspected objects in a short period, from apples to bananas to shrimp. Existing systems often require downtime for adjustments or algorithm redevelopment, failing to achieve seamless switching. This not only impacts production efficiency but can also lead to unstable inspection results, making it difficult to meet the food industry's urgent need for efficient and accurate inspection. Summary of the Invention

[0005] The purpose of this invention is to provide a food detection system and method based on computer vision, thereby solving the problems existing in the prior art.

[0006] To achieve the above objectives, the present invention provides the following technical solution: a computer vision-based food inspection system, comprising an image acquisition module, which acquires initial food image data from the production line through a preset image acquisition device, processes the data using an edge detection algorithm to extract contour features, and incorporates a boundary point coordinate extraction process to obtain a description of the food's appearance boundary; a boundary clustering module, which groups boundary points according to the extracted appearance boundary description using a clustering algorithm, and performs iterative convergence processing based on a distance metric function and cluster center initialization, determining that if the number of groups exceeds a preset threshold, it indicates an irregular shape, thus obtaining a shape classification label; a feature analysis module, which, after obtaining the shape classification label, calculates the surface grayscale gradient distribution using a texture analysis algorithm based on the label type, incorporates grayscale image conversion and gradient operator applications to construct a distribution histogram, determines the peak position under the peak detection method, and obtains a texture complexity index; and a coordinate extraction module; and a feature analysis module. If the texture complexity index exceeds a preset threshold, irregular groups are selected from the shape classification labels. Noise point filtering combined with clustering label allocation is used to regroup the boundary points to obtain the defect location coordinate set. The contamination identification module obtains the coordinate distribution pattern from the defect location coordinate set. If the coordinate distribution pattern matches the preset contamination template, it is marked as a contaminated area. Gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram and determine the contamination level. The parameter optimization module adjusts the parameters for calculating the complexity index according to the contamination level through preset module combination logic. Label type adaptation is incorporated to optimize the grayscale image conversion process and obtain an optimized detection configuration scheme. The detection processing module uses the optimized detection configuration scheme to batch process subsequent image data. If the accuracy index of the processing result meets the preset standard, the final detection report is output to obtain food quality assessment results suitable for multiple scenarios.

[0007] Preferably, the image acquisition module acquires initial food image data from the production line using a preset image acquisition device, processes the data using an edge detection algorithm to extract contour features, and incorporates a boundary point coordinate extraction process to obtain a description of the food's appearance boundary. This includes: acquiring initial food image data from the production line using a preset image acquisition device, storing it in a standardized format to obtain an initial image dataset; processing the initial image dataset using the Canny edge detection algorithm to extract contour features, obtaining a first contour feature set; if the edge continuity of the first contour feature set is lower than a preset threshold, optimizing the contour through morphological operations to obtain a second contour feature set; extracting boundary point coordinates from the second contour feature set using a boundary point coordinate extraction algorithm to generate a food appearance boundary point set; generating a continuous boundary curve based on the boundary point set using a spline interpolation algorithm to obtain a food appearance boundary description; performing geometric analysis on the food appearance boundary description to calculate boundary curvature features, obtaining a food appearance shape feature set; and generating a food appearance analysis result by storing the food appearance shape feature set in a database.

[0008] Preferably, the boundary clustering module, based on the extracted appearance boundary description, uses a clustering algorithm to group boundary points, and combines a distance metric function and cluster center initialization for iterative convergence processing. If the number of groups exceeds a preset threshold, it indicates an irregular shape, and shape classification labels are obtained. This includes obtaining boundary point data for irregular shapes from the shape classification labels. Principal component analysis is used to reduce the dimensionality of the boundary point coordinates, resulting in a low-dimensional boundary point set. The spatial distribution characteristics between point sets are calculated using the low-dimensional boundary point set, and the distance between points is measured using an Euclidean distance metric function, resulting in a point set distribution feature set. If the distance between points in the point set distribution feature set is... If the variance exceeds a preset threshold, the minimum bounding polygon of the boundary points is generated using the convex hull algorithm to obtain the bounding boundary description. The geometric parameters of the polygon are calculated based on the bounding boundary description, and the shape complexity feature is obtained using the perimeter-to-area ratio calculation method. Feature vectors are extracted from the shape complexity feature, and the feature vectors are classified using the support vector machine algorithm to obtain food shape category labels. If the food shape category label indicates a non-standard shape, the boundary points are smoothed using a spline interpolation algorithm to obtain a smoothed boundary point set. The food appearance boundary description is regenerated based on the smoothed boundary point set, stored in the database, and the final food shape analysis result is obtained.

[0009] Preferably, the feature analysis module, after obtaining the shape classification label, calculates the surface gray-level gradient distribution using a texture analysis algorithm based on the label type, integrates gray-level image conversion and gradient operator application to construct a distribution histogram, and determines the peak position under the peak detection method to obtain the texture complexity index. This includes: obtaining surface texture data from the shape classification label; calculating texture features using a gray-level co-occurrence matrix algorithm to generate a texture feature matrix; processing the texture feature matrix using a gradient operator to calculate the gray-level gradient distribution and generate a gray-level gradient histogram; determining the peak position using a peak detection algorithm on the gray-level gradient histogram to obtain the texture complexity index; if the texture complexity index exceeds a preset threshold, separating the texture region using an image segmentation algorithm to generate a texture sub-region set; calculating the spatial distribution characteristics of the sub-regions based on the texture sub-region set to generate a distribution feature vector; grouping the texture sub-regions using a K-means clustering algorithm based on the distribution feature vector to obtain texture classification labels; and storing the classification results in a database for the texture classification labels to generate the final texture analysis result.

[0010] Preferably, the coordinate extraction module, if the texture complexity index is higher than a preset threshold, selects irregular groups from the shape classification labels, and regroups the boundary points using noise point filtering combined with clustering label allocation to obtain the defect location coordinate set. This includes: if the texture complexity index is higher than the preset threshold, obtaining irregular groups from the shape classification labels, processing the point sets in the irregular groups using mean filtering to obtain a denoised point set; using the denoised point set, regrouping the point set using the K-means clustering algorithm to generate preliminary boundary point groups; calculating the geometric center point of each group based on the preliminary boundary point groups to obtain a center point coordinate set; if the matching degree between the center point coordinate set and the preset defect template is higher than a threshold, expanding the boundary point groups using a region growing algorithm to generate an expanded boundary region; extracting the gray value distribution features within the expanded boundary region to obtain a gray value feature vector; classifying the region using a support vector machine algorithm based on the gray value feature vector to determine the defect type label; and storing the classification results in the database using the defect type label to generate a defect location coordinate set.

[0011] Preferably, the contamination identification module obtains a coordinate distribution pattern from the defect location coordinate set. If the coordinate distribution pattern matches a preset contamination template, it is marked as a contaminated area. Simultaneously, gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram. Determining the contamination level includes: obtaining the coordinate distribution pattern from the defect location coordinate set; analyzing the spatial distribution characteristics of the coordinate points using a density estimation method to generate a distribution pattern description; if the matching degree between the coordinate distribution pattern and the preset contamination template is higher than a threshold, it is marked as a contaminated area, and a contamination area label is generated; using the contamination area label, gradient direction analysis is used to extract edge features within the contaminated area to obtain an edge feature vector; based on the edge feature vector and surface texture description, a distribution histogram is constructed to generate histogram data; using the histogram intersection method, the matching degree between the distribution histogram and the preset contamination template histogram is calculated to obtain a matching degree score; if the matching degree score is higher than a threshold, the contamination level is determined based on the score interval mapping; and the contamination level data is stored in a database to generate a classification result for the contaminated area.

[0012] Preferably, the parameter optimization module, based on the pollution level, adjusts the parameters for calculating the complexity index through a preset module combination logic, and incorporates label type adaptation to optimize the grayscale image conversion process, thereby obtaining an optimized detection configuration scheme. This includes: determining the priority of the module combination logic according to the pollution level using a preset level classification rule to obtain a module selection sequence; adjusting the weight parameters of the complexity index according to the module selection sequence using a preset complexity calculation formula to obtain optimized complexity parameters; if the optimized complexity parameters are higher than a preset threshold, then using label classification rules and combining them with pollution area identifiers to generate label type configurations to obtain label adaptation results; and using a grayscale conversion algorithm to optimize the grayscale image conversion process and generate a first grayscale image based on the label adaptation results.

[0013] Preferably, the parameter optimization module, based on the pollution level, adjusts the parameters for calculating the complexity index through a preset module combination logic, incorporates label type adaptation to optimize the grayscale image conversion process, and obtains an optimized detection configuration scheme. This further includes: extracting boundary features of the polluted area using an image segmentation algorithm based on the first grayscale image to obtain boundary feature data; adjusting the threshold of the detection configuration scheme using a parameter optimization method based on the boundary feature data to obtain the final detection configuration scheme; and storing the scheme data in a database based on the final detection configuration scheme to generate a pollution detection configuration record.

[0014] Preferably, the detection processing module uses an optimized detection configuration scheme to batch process subsequent image data. If the accuracy index of the processing result meets the preset standard, a final detection report is output, resulting in a food quality assessment result adaptable to multiple scenarios. This includes: generating a priority sequence based on the food contamination level using preset classification rules to determine the module selection result; optimizing batch processing parameters using complexity adjustment rules based on the module selection result to obtain an optimized batch processing configuration; if the optimized batch processing configuration meets the preset accuracy standard, generating food quality labels using a support vector machine algorithm to obtain label classification results; processing image data using a grayscale conversion method based on the label classification results to generate a first grayscale image; extracting contaminated area boundaries using a region growing algorithm based on the first grayscale image to obtain boundary data; optimizing the detection configuration using a threshold adjustment method based on the boundary data to obtain the final detection configuration; and storing the processing result in a database using the final detection configuration to generate a food quality assessment record.

[0015] A computer vision-based food inspection method, employing the aforementioned computer vision-based food inspection system, includes the following steps: acquiring initial food image data from the production line using a pre-set image acquisition device; processing the data using an edge detection algorithm to extract contour features, while simultaneously incorporating a boundary point coordinate extraction process to obtain a description of the food's appearance boundary; based on the extracted appearance boundary description, grouping the boundary points using a clustering algorithm, and performing iterative convergence processing combined with a distance metric function and cluster center initialization; determining that if the number of groups exceeds a pre-set threshold, it indicates an irregular shape, thus obtaining shape classification labels; after obtaining the shape classification labels, calculating the surface grayscale gradient distribution using a texture analysis algorithm based on the label type, constructing a distribution histogram by incorporating grayscale image conversion and gradient operator applications, and determining the peak position under a peak detection method to obtain a texture complexity index. If the texture complexity index exceeds a preset threshold, irregular groups are selected from the shape classification labels. Noise point filtering combined with clustering label allocation is used to regroup the boundary points to obtain a set of defect location coordinates. The coordinate distribution pattern is obtained from the defect location coordinate set. If the coordinate distribution pattern matches the preset contamination template, it is marked as a contaminated area. Gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram and determine the contamination level. Based on the contamination level, the parameters for calculating the complexity index are adjusted through preset module combination logic. Label type adaptation is incorporated to optimize the grayscale image conversion process, resulting in an optimized detection configuration scheme. The optimized detection configuration scheme is used to batch process subsequent image data. If the accuracy index of the processing result meets the preset standard, the final detection report is output, resulting in a food quality assessment result suitable for multiple scenarios.

[0016] As can be seen from the above technical solution, the present invention has the following beneficial effects:

[0017] This computer vision-based food inspection system and method addresses the challenge of accurately assessing irregular shapes, surface defects, and contamination levels in food production lines. It achieves efficient quality assessment through multi-dimensional feature extraction and dynamic optimization. The invention first extracts food appearance boundary features using edge detection and clustering algorithms to determine shape irregularities. It then combines texture analysis to calculate the surface grayscale gradient distribution, generating a texture complexity index. For irregularly shaped foods, noise filtering and re-clustering are used to locate defect coordinates. The degree of contamination is assessed through contamination template matching and gradient direction analysis. Finally, detection parameters are dynamically adjusted based on the contamination level to optimize batch processing accuracy. This invention significantly improves the accuracy and adaptability of food quality inspection through the fusion analysis of multi-dimensional features (boundary, texture, and contamination) and adaptive parameter optimization, making it suitable for quality control needs in various production lines. Attached Figure Description

[0018] Figure 1This is a module connection diagram of the computer vision-based food detection system of the present invention;

[0019] Figure 2 This is a flowchart of the computer vision-based food detection method of the present invention. Detailed Implementation

[0020] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0021] like Figure 1 As shown, this invention provides a technical solution: a food inspection system based on computer vision, including an image acquisition module that acquires initial food image data from the production line through a preset image acquisition device, processes the data using an edge detection algorithm to extract contour features, and incorporates a boundary point coordinate extraction process to obtain a description of the food's appearance boundary; a boundary clustering module that, based on the extracted appearance boundary description, uses a clustering algorithm to group boundary points, combines a distance metric function and cluster center initialization for iterative convergence processing, and determines that if the number of groups exceeds a preset threshold, it indicates an irregular shape, thus obtaining a shape classification label; a feature analysis module that, after obtaining the shape classification label, calculates the surface grayscale gradient distribution using a texture analysis algorithm based on the label type, incorporates grayscale image conversion and gradient operator applications to construct a distribution histogram, determines the peak position under the peak detection method, and obtains a texture complexity index; and a coordinate extraction module... If the texture complexity index exceeds a preset threshold, irregular groups are selected from the shape classification labels. Noise point filtering combined with clustering label allocation is used to regroup the boundary points to obtain the defect location coordinate set. The contamination identification module obtains the coordinate distribution pattern from the defect location coordinate set. If the coordinate distribution pattern matches the preset contamination template, it is marked as a contaminated area. Gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram and determine the contamination level. The parameter optimization module adjusts the parameters for calculating the complexity index according to the contamination level through preset module combination logic. Label type adaptation is incorporated to optimize the grayscale image conversion process and obtain an optimized detection configuration scheme. The detection processing module uses the optimized detection configuration scheme to perform batch processing on subsequent image data. If the accuracy index of the processing result meets the preset standard, the final detection report is output to obtain food quality assessment results suitable for multiple scenarios.

[0022] This system employs a multi-module collaborative structure to automatically inspect food using computer vision. First, the image acquisition module acquires real-time images of the food using an industrial camera. It extracts contour features using edge detection algorithms such as Canny or Sobel, and combines this with pixel gradient changes to extract boundary point coordinates, thus constructing appearance boundary description data. The boundary clustering module further classifies boundary points using clustering algorithms such as K-means or DBSCAN, using the Euclidean distance between boundary points as a metric function, and converges within a set maximum number of iterations to determine the presence of irregular shapes. Next, the feature analysis module performs texture complexity analysis based on shape labels, using operators such as Gray-Level Co-occurrence Matrix (GLCM) or Laplacian to obtain a gray-level gradient histogram and extracting its peak positions to represent the texture complexity index. If this index exceeds a threshold, the coordinate extraction module selects irregular regions for regrouping, removes noise points, and extracts defect coordinates. The contamination identification module compares the contamination with a preset contamination pattern using a template matching algorithm and combines this with gradient direction analysis to determine the contamination level. The parameter optimization module adjusts image processing parameters, such as gradient operator type and grayscale conversion method, according to the contamination level to form an optimized detection configuration. Finally, the detection processing module uses this configuration to perform batch detection on subsequent food images, ensuring the output of high-precision detection reports.

[0023] This system boasts a high degree of automation and detection accuracy, accurately extracting boundary and texture features from food images to precisely identify areas with abnormal shapes and contamination. Through multi-level clustering analysis and texture complexity calculation, it effectively enhances the sensitivity and reliability of food defect detection. The contamination identification employs a multi-parameter matching strategy, adapting to different contamination types and variations in food appearance. A parameter optimization module further improves the system's adaptability across various scenarios, reducing false positives and false negatives, and increasing overall detection efficiency. The final detection processing module supports batch image processing, enabling rapid quality assessment and outputting result reports, making it suitable for real-time monitoring and quality control applications on production lines.

[0024] The image acquisition module acquires initial food image data from the production line using a pre-set image acquisition device. It then processes the data using an edge detection algorithm to extract contour features, incorporating a boundary point coordinate extraction process to obtain a description of the food's appearance boundaries. This includes acquiring initial food image data from the production line using the pre-set image acquisition device, storing it in a standardized format to obtain an initial image dataset; processing the initial image dataset using the Canny edge detection algorithm to extract contour features, resulting in a first contour feature set; optimizing the contours of the first contour feature set using morphological operations to obtain a second contour feature set; extracting boundary point coordinates from the second contour feature set using a boundary point coordinate extraction algorithm to generate a food appearance boundary point set; generating continuous boundary curves based on the boundary point set using a spline interpolation algorithm to obtain a food appearance boundary description; performing geometric analysis on the food appearance boundary description to calculate boundary curvature features, resulting in a food appearance shape feature set; and storing the food appearance shape feature set in a database to generate food appearance analysis results.

[0025] The image acquisition module, in actual operation, consists of industrial cameras installed on the food production line. The cameras have a focal length of 16mm and a field of view covering the entire width of the conveyor belt. As food passes the camera's location, a synchronous triggering system controls the capture of a picture every 200 milliseconds. Each image has a resolution of 1920 x 1080 pixels and is saved in portable network image format. Each image is named with a production batch number and a timestamp to ensure the uniqueness of the image file. The system uniformly saves the acquired image files to a preset image data path, forming the initial image dataset.

[0026] Each image in the image dataset undergoes preprocessing. The first step is grayscale conversion, which involves assigning weights to the red, green, and blue channels: 0.3 for the red channel, 0.59 for the green channel, and 0.11 for the blue channel. The calculation method involves multiplying the red, green, and blue component values ​​of each pixel by their corresponding weights and then summing the results to obtain the grayscale value, thus converting the color image into a single-channel grayscale image.

[0027] The second step is noise removal, which uses a Gaussian filtering algorithm to smooth the grayscale image. The filter kernel is a 5x5 pixel square window with a standard deviation of 1.0. The center point of the window has a greater weight than the edge points. The filtering method is to calculate the weighted average of the neighboring pixels centered on each pixel, and then replace the original pixel value with the average value to achieve image smoothing. The parameter values ​​were determined based on a comparative experiment of edge detection performance of multiple images processed under different standard deviations, and the value with the optimal edge integrity rate was selected as the standard deviation setting.

[0028] The third step executes the Canny edge detection algorithm, which specifically involves calculating the horizontal and vertical grayscale differences of each pixel in the image. The differential value is approximated by the difference between adjacent pixels to obtain the gradient intensity of each pixel. Then, the gradient direction of each pixel is judged by comparing the gradient value of the current pixel with the gradient values ​​of its two adjacent pixels in that direction. If the current value is the largest, it is retained; otherwise, it is set to zero, thus completing the non-maximum suppression operation and refining the edges. Subsequently, a dual thresholding process is performed, setting the low threshold to 10% of the maximum grayscale value of the image and the high threshold to 30% of the maximum grayscale value. By detecting strong edge points and extending to connected weak edge points, the first contour feature set is finally obtained. This threshold setting is based on the overlap test of manually labeled edges and automatically detected edges in experimental images, and is the optimal parameter combination used when the overlap exceeds 95% and the false detection rate is less than 5%.

[0029] To determine the edge continuity of the first contour feature set, the system employs a contour connectivity detection method. Detected edge points are grouped into connected regions based on their connectivity relationships. The number of edge pixels in each region is calculated, with a threshold of 250 pixels. If the number of edge points in a contour region is lower than this threshold, it is considered a discontinuous edge, requiring morphological optimization. This threshold setting is based on statistical analysis of the minimum closed segment length of the contour boundary of normal food appearance; the results show that edge segments shorter than 250 pixels cannot form effective closed loops.

[0030] Edge optimization involves first performing a dilation operation, using a 3x3 pixel square template as the structuring element. This covers the maximum value of each edge pixel within its neighborhood, connecting broken edges. Next, an erosion operation is performed, using the same structuring element to restore the non-edge expansion portion to its original shape, thus recovering the edge width and forming a second contour feature set. The execution order is dilation followed by erosion, with each operation performed once.

[0031] Next, the coordinates of boundary points are extracted from the second contour feature set. The contour tracking algorithm is used to scan the edge image pixel by pixel. Starting from the top left corner, the edge points with the maximum pixel value are searched from left to right in rows. Their horizontal and vertical coordinates are recorded. The coordinates of all continuous edge points are recorded in sequence according to the contour direction, and finally the food appearance boundary point set is formed.

[0032] Spline interpolation is performed on the boundary point set. Interpolation segments are constructed by grouping every 10 adjacent boundary points. The average direction change between adjacent points and the intermediate curve direction are calculated for each segment. The position of intermediate transition points is reconstructed using the interpolation algorithm, ensuring that each interpolation segment curve is continuous and smooth, with monotonic curvature changes and no sharp corners. The interpolated result forms the food appearance boundary curve, i.e., the appearance boundary description.

[0033] Next, the system calculates the curvature features of this boundary description. The method involves sampling every five points from the continuous curve to construct a three-point sequence. The angle between two adjacent straight lines is calculated, and the difference between this angle and the angle difference of the previous segment is used to obtain the curvature change. If the difference between two consecutive curvature changes exceeds 30%, that point is identified as a curvature abrupt change point. The system iterates through all boundary points to calculate the corresponding curvature indices, ultimately selecting all curvature abrupt change points to form a food appearance shape feature set. The curvature abrupt change judgment threshold is set to 30%, which is the optimal boundary value determined by comparing the results of manual annotation and automatic extraction of boundary feature points for various abnormal shape samples.

[0034] Finally, the system writes the food appearance shape feature set into a structured database. Record fields include image number, number of boundary points, number of curvature abrupt change points, the position index of each abrupt change point in the boundary curve, and the corresponding curvature change value. The data table structure is fixed, and the field naming rules follow the system's preset standard definition. The database type is a relational database. After all data is written, an appearance analysis report is automatically generated for use by subsequent modules.

[0035] The boundary clustering module, based on the extracted appearance boundary description, uses a clustering algorithm to group boundary points. It combines a distance metric function and cluster center initialization for iterative convergence processing. If the number of groups exceeds a preset threshold, it indicates an irregular shape, resulting in shape classification labels. Boundary point data for irregular shapes are extracted from these labels. Principal component analysis is used to reduce the dimensionality of the boundary point coordinates, yielding a low-dimensional boundary point set. The spatial distribution characteristics between point sets are calculated using this low-dimensional boundary point set, and the distance between points is measured using a Euclidean distance metric function, resulting in a point set distribution feature set. The variance of the distance between points in the point set distribution feature set is then considered. If the threshold is exceeded, the minimum bounding polygon of the boundary points is generated using the convex hull algorithm to obtain the bounding boundary description. The geometric parameters of the polygon are calculated based on the bounding boundary description, and the shape complexity feature is obtained by using the perimeter-to-area ratio calculation method. Feature vectors are extracted from the shape complexity feature, and the feature vectors are classified using the support vector machine algorithm to obtain the food shape category label. If the food shape category label indicates a non-standard shape, the boundary points are smoothed using the spline interpolation algorithm to obtain a smoothed boundary point set. The food appearance boundary description is regenerated based on the smoothed boundary point set, stored in the database, and the final food shape analysis result is obtained.

[0036] In the food inspection system, after receiving the appearance boundary description, the boundary clustering module first processes the coordinate data of all boundary points contained in the description. The system uses the K-means clustering algorithm to divide these boundary points into several groups, initially setting the number of clusters to 5. The initial cluster centers are selected by uniformly choosing the coordinates of 5 points from the boundary point set in sequence, ensuring a uniform distribution of the initial clusters. In each round of clustering iteration, the system calculates the Euclidean distance between each boundary point and all cluster centers, that is, it calculates the sum of the squares of the differences between the horizontal and vertical coordinates and takes the square root, selecting the cluster with the smallest distance as the cluster to which the point belongs. Then, the position of each cluster center is updated, that is, the average of the horizontal coordinates of all points in the cluster is used as the new horizontal coordinate center, and the vertical coordinate is calculated similarly. This process is repeated until the change value of the coordinates of all cluster centers is less than 1 pixel, or the number of iterations reaches 100, at which point the system terminates and takes the optimal clustering result. The system then counts the final number of clusters. If this number exceeds 5, it is determined that the shape is irregular. The threshold of 5 was set based on statistical analysis of the clustering results of 1,000 regular samples. It was found that the boundaries of normal food can be effectively defined under this setting, and exceeding this value usually indicates abnormal deformation or contour splitting.

[0037] For boundary point sets deemed irregular, the system further performs principal component analysis (PCA) for dimensionality reduction to simplify the data structure. First, the two-dimensional coordinates of all points in the set are extracted, and the horizontal and vertical covariance matrices are calculated. Then, eigenvalue decomposition is performed on these matrices, and the direction vector with the largest eigenvalue is selected as the principal direction. The system projects the coordinates of all points onto this principal direction, forming a one-dimensional coordinate sequence, resulting in a low-dimensional boundary point set. Next, the system calculates the distribution characteristics between points based on this low-dimensional point set, i.e., it iterates through the distance differences between each pair of points and records all differences. The system calculates the variance of this set of difference data and statistically analyzes the dispersion of all differences. If the variance exceeds a threshold of 100, the distribution of boundary points along the principal direction is considered too dispersed, indicating complexity or abrupt changes in the boundary structure. This threshold of 100 is determined based on the point distance analysis of 500 normal samples and 500 abnormal samples in this dimension, using the overlap of the difference distribution to determine the minimum difference standard that can effectively distinguish between the two.

[0038] When the variance exceeds a threshold, the system proceeds to the circumscribed boundary construction step, executing the convex hull algorithm to construct the minimum bounding polygon. Specifically, all boundary points are scanned sequentially, and a rotational scan method is used to find the minimum polygon that can enclose all points, with its vertices being the extreme points forming the enclosing circle. The system then calculates the polygon's geometric properties. First, it iterates through the line segments between every two adjacent points, calculates their lengths, and sums them to obtain the polygon's perimeter. Next, by dividing the polygon into triangles, the area of ​​each triangle is calculated and summed to obtain the total area. The perimeter-area ratio is obtained by dividing the perimeter by the area, and this ratio measures the complexity of the boundary shape. A larger ratio indicates a more tortuous and irregular shape boundary. The system uses this value as a shape complexity feature.

[0039] The system uses the shape complexity feature value as a feature vector input to a pre-trained support vector machine (SVM) classification model. The model employs a radial basis function kernel, supporting binary classification of standard and non-standard shapes based on a single feature input. Model training was completed using 1000 manually labeled standard datasets, achieving an accuracy of over 95%. The system obtains the classification labels output by the model. If the label is a non-standard shape, the system performs spline interpolation on its boundary point set. The processing steps involve grouping boundary points into sets of 10, constructing interpolation segments, and generating a continuous smooth curve for each segment to connect the endpoints. The intermediate curves smoothly transition according to the coordinate changes of the preceding and following points, avoiding corner points or abrupt changes. All interpolation segments are connected to form a complete smooth boundary point set.

[0040] The system extracts feature vectors from the shape complexity features calculated from the circumscribed boundary description. The specific steps are as follows: The system calculates the geometric parameters of the smallest circumscribed polygon, mainly including the perimeter and area. The perimeter is calculated by summing the straight-line distances between all adjacent boundary points in the polygon, i.e., calculating and summing the Euclidean distances between every two points sequentially. The area is calculated by dividing the polygon into several triangles, calculating the area of ​​each triangle separately, and then summing the areas. After completion, the system divides the perimeter by the area to obtain a specific numerical value, which serves as the shape complexity feature, reflecting the complexity of the boundary structure. The system uses this numerical value as a single feature to form a one-dimensional feature vector.

[0041] Subsequently, the system calls the pre-trained Support Vector Machine (SVM) classification model to classify the aforementioned feature vectors. This SVM model is trained on a large number of pre-labeled standard and non-standard food images, uses radial basis functions as kernel functions, and has the ability to perform binary classification on non-linear data. The classification process is as follows: the system inputs the extracted feature vectors into the model, the model maps them to a high-dimensional feature space, and determines the distance and direction of the vector's location relative to the classification hyperplane, thereby outputting a category label with the value of "standard shape" or "non-standard shape".

[0042] If the model outputs a label of "non-standard shape," the system immediately performs spline interpolation on the boundary point set corresponding to that food item. The processing steps are as follows: First, the system sorts the original boundary points according to the contour order, and then forms interpolation intervals in groups of 10 consecutive points. Within each interval, the system calculates the tangent direction and rate of change of the curve based on the coordinate change trend between boundary points. Then, it uses cubic spline interpolation to calculate the fitted coordinates of the intermediate points of the interval, ensuring that the interpolated curve is continuous at the connection points and that the derivative is continuous, thus achieving a smooth transition. The system sequentially performs interpolation on all intervals and connects all interpolated segments into a complete smooth boundary curve, ultimately forming a smooth boundary point set. Each point in this set contains accurate two-dimensional coordinate information and has good continuity and curvature consistency. After completing the interpolation, the system verifies the processing results to ensure that the number of smooth boundary point sets is not less than the number of original boundary point sets, that the boundary is well-closed, and that there are no self-intersections or breaks. Finally, the smooth boundary point set is used as updated boundary data for subsequent shape analysis or defect identification processing.

[0043] The system uses the smoothed point set to replace the original boundary point set, reconstructs the food appearance boundary description, and generates an updated boundary data structure, recording the point sequence, curvature change value, and coordinate data. Finally, the results are written to a database, with fields including image number, processing status, final shape label, whether smoothing was performed, and the number of original and processed boundary points, for use in subsequent module calls and final inspection report generation.

[0044] The feature analysis module, after obtaining shape classification labels, calculates the surface gray-level gradient distribution using texture analysis algorithms based on the label type. It incorporates gray-level image conversion and gradient operator applications to construct a distribution histogram, determining peak positions using peak detection methods to obtain a texture complexity index. This process includes obtaining surface texture data from shape classification labels, calculating texture features using the gray-level co-occurrence matrix algorithm to generate a texture feature matrix; processing the texture feature matrix using gradient operators to calculate the gray-level gradient distribution and generate a gray-level gradient histogram; using a peak detection algorithm to determine peak positions on the gray-level gradient histogram to obtain a texture complexity index; if the texture complexity index exceeds a preset threshold, separating texture regions using an image segmentation algorithm to generate texture sub-region sets; calculating the spatial distribution characteristics of the sub-regions based on the texture sub-region sets to generate distribution feature vectors; using the distribution feature vectors, grouping the texture sub-regions using the K-means clustering algorithm to obtain texture classification labels; and storing the classification results in a database for the texture classification labels to generate the final texture analysis result.

[0045] In the feature analysis module of this invention, after receiving the target region identified by the shape classification label, the system first locates the specific coordinates of the region in the original image and extracts the corresponding image block from the original image as the texture region to be analyzed. This image block is typically a rectangular region, with its length and width each consisting of the shape boundary plus an edge buffer, each extended by 10 pixels, ensuring that key edge information is not missed during texture extraction. The system performs color space conversion on this image block, using a weighted average method to convert the color image to a grayscale image. Specifically, the red channel is multiplied by 0.3, the green channel by 0.59, and the blue channel by 0.11, and then the results are summed. The conversion result is a single-channel image, with each pixel's grayscale value ranging from 0 to 255, meeting the requirements for subsequent grayscale feature extraction.

[0046] The system extracts image texture features using the gray-level co-occurrence matrix (GLCM) algorithm. The steps are as follows: The gray-level number is set to 256. The system iterates through all pixels in the gray-level image and their adjacent pixel value combinations at specified directions and distances. Four angles are set: 0 degrees, 45 degrees, 90 degrees, and 135 degrees. The pixel distance is set to 1 pixel, meaning the frequency of occurrence of all gray-level value pairs is counted in each of the four directions. A 256x256 two-dimensional matrix is ​​generated for each direction, where each element represents the frequency of occurrence of the corresponding gray-level value combination. Subsequently, the system performs a weighted average of the matrices in the four directions, with a weight of 0.25 for each element, to obtain the final texture feature matrix. The values ​​in the matrix represent the overall distribution of gray-level pair combinations in the image. The selection of the number of directions, pixel distance, and gray-level number is the optimal configuration determined after testing different combinations on 500 sample images to ensure rich texture detail and algorithm stability.

[0047] Next, the system performs image gradient processing on the texture feature matrix to analyze the severity of texture changes. The Sobel gradient operator is used to perform convolution operations on the texture matrix in both the horizontal and vertical directions. In each direction, a 3x3 weighted template is used to calculate the weighted difference of the grayscale values ​​in the pixel's neighborhood, yielding the gradient value for each pixel in both directions. The sum of the squares of these gradient values ​​in both directions is then taken as the square root to obtain the total gradient magnitude of the current pixel. This process iterates through all pixels in the entire image block, outputting a grayscale gradient map. The system then counts the frequency of each gradient magnitude value from the grayscale gradient map, constructing a grayscale gradient histogram. The horizontal axis of the histogram represents gradient magnitude values ​​from 0 to 255, and the vertical axis represents the number of times each value appears in the image. This histogram is used for subsequent peak detection of texture complexity.

[0048] After constructing the grayscale gradient histogram, the system performs peak detection analysis. First, it applies a moving average filter to the histogram data, with a filter window of 5. This means averaging the five data points before and after each position and replacing the median value to remove high-frequency fluctuations. Next, the system iterates through each data point, checking if it is greater than its two adjacent values. If so, it marks it as a peak point. The system counts all peak points as a texture complexity index. If this number exceeds a preset threshold of 50, the texture complexity is considered high. This threshold of 50 was determined by the system through statistical analysis of 1000 manually scored texture complexity samples from actual acquired images. When the number of peaks exceeds 50, the accuracy of manually identifying dense texture regions reaches over 92%.

[0049] If the texture complexity index exceeds the threshold of 50, the system further performs structural analysis of the texture region and initiates image segmentation. Image segmentation uses a region growing algorithm, selecting the top 100 points from all points in the gradient map with a gradient value greater than the average gradient value as seed points. Each seed point is expanded in four directions, with the expansion condition that the difference between the newly added pixel and the average gradient value of the seed region does not exceed the threshold of 5. If it does, expansion in that direction stops. Each seed point expands to form an independent texture sub-region, and after all expansions stop, a complete set of texture sub-regions is formed. The region growing termination threshold of 5 is the optimal value obtained through experimental testing under various lighting conditions to ensure region integrity and boundary accuracy.

[0050] For each texture sub-region formed, the system calculates its centroid coordinates by averaging the x and y coordinates of all pixels within that region. Then, the system sequentially calculates the Euclidean distance between the centroids of any two sub-regions, obtaining the average distance, maximum distance, minimum distance, and standard deviation of all regions. These are used as a spatial distribution feature vector comprising four dimensions for subsequent cluster analysis.

[0051] The clustering operation employs the K-means algorithm, setting the number of clusters to 3. Initial cluster centers are selected from samples corresponding to the maximum, minimum, and median values ​​of the feature vectors. In each iteration, the system calculates the Euclidean distance from each sample point to the three centers and assigns it to the cluster containing the nearest center. Then, the four-dimensional values ​​of the feature vectors of samples within each cluster are averaged to update the cluster center values. This process is repeated until the maximum difference in center changes between consecutive iterations is less than 1, or the number of iterations reaches 50. The clustering algorithm parameters are set to fixed values. The number of clusters (3) is based on the statistical patterns of food surface texture, which are typically categorized into regular, granular, and cracked patterns. The center change threshold and the maximum number of iterations are 1 and 50, respectively, ensuring the stability of the results.

[0052] After clustering is completed, the system assigns a cluster number as a texture classification label to each texture sub-region. Then, the classification label, image number, region number, centroid coordinates, texture complexity value, and cluster number of all sub-regions are organized into a structured record and uniformly written into the texture analysis results database for subsequent detection processing and quality assessment reference.

[0053] The coordinate extraction module, if the texture complexity index is higher than a preset threshold, selects irregular groups from the shape classification labels, and regroups the boundary points using noise point filtering combined with clustering label allocation to obtain the defect location coordinate set. If the texture complexity index is higher than the preset threshold, irregular groups are obtained from the shape classification labels, and the point sets in the irregular groups are processed using mean filtering to obtain a denoised point set. Using the denoised point set, the point set is regrouped using the K-means clustering algorithm to generate preliminary boundary point groups. Based on the preliminary boundary point groups, the geometric center point of each group is calculated to obtain the center point coordinate set. If the matching degree between the center point coordinate set and the preset defect template is higher than a threshold, the boundary point groups are expanded using a region growing algorithm to generate an expanded boundary region. Through the expanded boundary region, the grayscale value distribution features within the region are extracted to obtain a grayscale feature vector. Based on the grayscale feature vector, the region is classified using a support vector machine algorithm to determine the defect type label. Using the defect type label, the classification results are stored in the database to generate the defect location coordinate set.

[0054] The system first reads the texture complexity index, which is the number of peaks in the grayscale gradient histogram. This module is activated when the number exceeds a threshold of 50. The threshold of 50 is derived from a statistical evaluation of 2000 sample images. At this threshold, the defect detection rate is higher than 90%, and the false alarm rate is lower than 10%, serving as a stable operating point. Subsequently, the system filters out groups marked as irregular from the shape classification labels and reads the two-dimensional coordinates of all boundary points within each group in contour order, forming an input point sequence. This ensures that the coordinates are consistent with the original image coordinate system without any coordinate system transformation.

[0055] The system performs mean filtering on the input point sequence point by point. Specifically, it selects the current point as the center, and then selects its four preceding and four following points in the sequence, forming a neighborhood of nine points. The arithmetic mean of the horizontal coordinates of these nine points is calculated to obtain the denoised horizontal coordinates. Then, the arithmetic mean of the vertical coordinates of these nine points is calculated to obtain the denoised vertical coordinates. These two values ​​replace the current point's coordinates, completing one filtering operation. The window size is fixed at 3 x 3. For cases where there are fewer than nine points at the end of the sequence, a mirroring strategy is used to ensure that all nine points are included in the calculation. The fixed window size of 3 x 3 is based on a comparison of the boundary fidelity and noise suppression of different windows; 3 x 3 is optimal when the mean boundary offset is less than 1 pixel and the isolated point removal rate is higher than 85%. After processing the entire sequence, a denoised point set is output.

[0056] The system performs mean clustering on the denoised point set to obtain spatially independent candidate defect regions. The number of clusters is fixed at 3, derived from the dominant distribution statistics of three spatial morphologies—cracks, stains, and flaking—in historical data. Using a fixed number of clusters avoids uncertainty in online defect selection. The initial center selection adopts the strategy of adding the furthest point pair to the maximum sum: first, calculate the straight-line distance of all point pairs in the denoised point set, select the two points with the largest distance as the first two centers, and then select the point with the largest sum of distances from the remaining points as the third center. The clustering iteration process is as follows: calculate the straight-line distance of each point to the three centers, and assign the point to the group containing the center with the smallest distance; after the assignment, calculate the arithmetic mean of the horizontal coordinates of all points in each group as the horizontal coordinate of the new center, and calculate the arithmetic mean of the vertical coordinates as the vertical coordinate of the new center; if the maximum coordinate change of the three centers in two adjacent iterations is less than 1 pixel or the number of iterations reaches 50, the iteration stops and the preliminary boundary point grouping is output. The distance metric is fixed at straight-line distance, and the stop threshold of 1 pixel is determined through sensitivity analysis of cluster stability. At this threshold, the group jitter is less than 5.

[0057] The system calculates the geometric center for each initial group, that is, by taking the arithmetic mean of the horizontal coordinates of all points within the group to obtain the center's horizontal coordinates, and by taking the arithmetic mean of the vertical coordinates to obtain the center's vertical coordinates, forming a set of center point coordinates. Then, this set of center point coordinates is compared with a preset defect template to calculate the matching degree. The defect template consists of a set of manually annotated defect centers from history, with one template center set corresponding to each type of defect. The matching steps are as follows: Step 1, calculate the centroid coordinates of the current center set and the template center set respectively, and align the centroids; Step 2, calculate the average distance between adjacent centers of the two sets respectively, and use this distance ratio for uniform scaling to ensure that the average adjacent distances of the two sets are equal; Step 3, for each center point in the current set, find the nearest neighbor in the template set with a radius of 8 pixels. If the nearest neighbor is within the radius, it is considered a hit, and the template point is marked as matched; Step 4, calculate the ratio of the number of hits to the total number of current centers as the matching degree. The radius of 8 pixels is determined by the 95th percentile of the annotation point error to ensure that true matches are not rejected. The matching threshold is fixed at 0.8, derived from the optimal value obtained through cross-validation, at which the weighted score of overall precision and recall is highest. When the matching degree is greater than 0.8, the region is expanded; when the matching degree is not greater than 0.8, the group is not expanded and is retained as a candidate.

[0058] For groups requiring expansion, the system employs a region growing algorithm, starting expansion from the geometric center of the group as the seed point. Connectivity is set to 4-connectivity, meaning adjacent pixels are checked only in the top, bottom, left, and right directions. The expansion admission criteria are: the difference between the grayscale value of the pixel to be added and the current region's average grayscale value is no greater than 10; if the difference is greater than 10, the pixel is rejected, and expansion stops in that direction. The region's average grayscale value is updated immediately after each pixel is added to reflect the current region statistics. To prevent abnormal spread, two hard limits are set: a maximum expansion pixel count of 5000 and a maximum expansion radius of 100 pixels; expansion stops once either limit is triggered. The threshold of 10 is determined statistically by the material's texture fluctuations under different illumination levels from multiple batches of samples, ensuring grayscale consistency within the region without excessive shrinkage; 5000 and 100 are determined by the coverage requirements of extremely large defect samples and processing latency constraints, ensuring neither large defects are truncated nor computational runaway occurs. After expansion, the system outputs the pixel set of the expanded boundary region and the sequence of its outer boundary points.

[0059] The system calculates the grayscale value distribution characteristics within each extended boundary region, forming a fixed-length grayscale feature vector. Specifically, it has five dimensions: the first is the grayscale mean, reflecting the overall brightness level of the region; the second is the grayscale variance, reflecting the brightness fluctuation range of the region; the third is the minimum grayscale value, reflecting the level of the darkest pixel; the fourth is the maximum grayscale value, reflecting the level of the brightest pixel; and the fifth is the standard deviation of the grayscale difference between adjacent pixels, reflecting the uniformity and roughness of local grayscale changes. To eliminate the influence of dimensions, the system uses the minimum and maximum values ​​of each dimension recorded during the training phase to perform linear mapping on each of the five features, compressing each feature to the range of 0 to 1. The mapping parameters are fixed after model training and are not updated during the online phase, ensuring consistency in pre-classification processing. This five-dimensional structure constitutes the grayscale feature vector.

[0060] The system inputs grayscale feature vectors into a support vector machine (SVM) classifier. Before deployment, the classifier is trained with 2000 samples, 1000 defective samples and 1000 normal samples, with labels covering cracks, contamination, and bubbles. The kernel function is fixed as a radial basis function, and the penalty coefficient and kernel width are determined and fixed after grid search and cross-validation, and are not adjusted during runtime. During online classification, the classifier returns the confidence score and final class label for each class; if multiple candidates exist, the class with the highest confidence score is selected as the defect type label; if the confidence scores are all below 0.5, it is marked as uncertain and requires manual verification. The threshold of 0.5 is the critical point for minimizing the weighted loss of false negatives and false positives on the training and validation sets. The final output is the defect type label corresponding to each expanded region.

[0061] The system uses the geometric center of each extended boundary region as the defect location coordinates and simultaneously records the sequence of circumscribed boundary points, the number of pixels in the region, the coordinates of the top-left and bottom-right corners of the region's circumscribed rectangle, the matching degree, the reason for extension termination, the defect type label, and the classification confidence score. This information, along with the image number and production batch number, is written into the database. To improve retrieval and tracking efficiency, the database establishes a combined index: image number plus group sequence number, batch number plus timestamp, and defect type label plus image number. The generated records constitute a defect location coordinate set, which can be directly referenced in subsequent inspection reports. All fields use fixed data types and fixed field names to ensure consistent and comparable results across multiple runs.

[0062] The contamination identification module obtains the coordinate distribution pattern from the defect location coordinate set. If the coordinate distribution pattern matches the preset contamination template, it is marked as a contaminated area. Gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram and determine the contamination level. This includes obtaining the coordinate distribution pattern from the defect location coordinate set, analyzing the spatial distribution characteristics of the coordinate points using density estimation methods, and generating a distribution pattern description. If the matching degree between the coordinate distribution pattern and the preset contamination template is higher than a threshold, it is marked as a contaminated area, and a contaminated area label is generated. Using the contaminated area label, gradient direction analysis is used to extract edge features within the contaminated area, obtaining edge feature vectors. Based on the edge feature vectors and surface texture description, a distribution histogram is constructed, generating histogram data. The histogram intersection method is used to calculate the matching degree between the distribution histogram and the preset contamination template histogram, obtaining a matching degree score. If the matching degree score is higher than a threshold, the contamination level is determined based on the score interval mapping. The contamination level data is stored in the database, generating the classification result of the contaminated area.

[0063] The system first reads all coordinate points from the defect location coordinate set and establishes an equidistant grid on the original image coordinate system, dividing the entire image into several cells with a unit side length of 10 pixels. For each cell, a density value is calculated by counting the number of defect points falling into that cell and its four adjacent cells (top, bottom, left, and right). The density is then weighted according to the straight-line distance from the cell center to the defect point: 0-5 pixels have a weight of 1.0, 6-10 pixels have a weight of 0.5, and 11-15 pixels have a weight of 0.25. Pixels exceeding 15 pixels are not counted. This distance attenuation radius of 15 pixels was fixed after comparing the detection rate and false alarm rate of 2000 samples under different radius and weight combinations. All cell density values ​​are then applied to the entire image. The maximum density value is normalized to generate a coordinate distribution pattern description. Cells with a density of not less than 0.60 are used as the foreground for connectivity aggregation to obtain several high-density connected regions. The threshold of 0.60 is determined by the inflection point of the overlap curve between the manually labeled region and the machine density mask. Then, it is matched with a preset contamination template for the first time. The template is stored at the same grid scale. The matching degree is calculated by taking the smaller value of the normalized density of the corresponding cell, summing them up, and dividing by the sum of the densities of the two, to obtain a score between 0 and 1. If the score is greater than 0.75, the coordinate distribution of the current connected region is considered to be consistent with the template and a contamination region label is generated. The threshold of 0.75 is fixed at the optimal balance point between detection and false alarm through hierarchical cross-validation. For each tagged contaminated area, the system performs gradient direction analysis and surface texture description: Edge extraction uses a fixed window difference method to calculate the grayscale difference of each pixel in the horizontal and vertical directions. The edge intensity is the composite intensity of the differences in the two directions, and the intensity threshold is set to 20. Pixels with an intensity lower than 20 are not included in the edge statistics. This threshold of 20 is determined by the upper bound of the noise intensity of the defect-free area plus a safety margin. The retained edge pixels are quantized according to their direction, ranging from 0 degrees to 180 degrees, divided into 36 segments, each 5 degrees wide. The horizontal and vertical differences are mapped to specific angle segments using a lookup table method, and the number of edge pixels in each segment is accumulated to form an edge direction histogram of length 36. At the same time, surface texture description is performed, starting from the contaminated area with a step size of 1 pixel at 0 degrees, 45 degrees, 90 degrees, and 135 degrees. The algorithm iterates through adjacent pixel pairs in each direction, records the grayscale difference, and divides the grayscale difference into 32 segments with equal intervals from 0 to 255 for statistical frequency. The average of the frequencies in the four directions yields a grayscale difference histogram of length 32. This number of segments, 32, is determined by a trade-off between fine texture granularity and statistical stability. With a fixed sample size, this ensures that the proportion of each segment in the histogram remains stable while still being able to distinguish fine textures. The 36 edge direction histograms and the 32 grayscale difference histograms are sequentially concatenated to form a distribution histogram containing 68 segments, and sum normalization is performed to eliminate the scale effect caused by differences in area. Then, a secondary matching is performed. The histogram intersection method is used to sum the smaller values ​​of the current histogram and the histogram of the same type of contaminated template segment by segment, and then divide by the sum of the corresponding segment values ​​of the two to obtain a matching score between 0 and 1. If the score is greater than 0, the matching score is considered a match.70 samples are categorized into pollution levels. The threshold of 0.70 is determined by the inflection point of the receiver operating characteristic (ROC) curve on the validation set to exclude samples with unclear boundaries. Pollution level is determined based on score interval mapping: scores above 0.70 and below 0.80 are classified as mild, above 0.80 and below 0.90 as moderate, and above 0.90 and below 1.00 as severe. If the score is below 0.70, no grading is applied, only the coordinate distribution label is retained. The explanation and determination method for all parameters are as follows: 10 pixels per cell side... To ensure that each grid cell contains enough points for density estimation at common resolutions without crossing the boundaries of small contaminated areas, increasing the side length will reduce positioning accuracy, while decreasing it will increase the variance of density estimation. A distance attenuation radius of 15 pixels is determined synchronously with the three-segment weight combination to ensure that nearest neighbor points dominate the density contribution while distant points contribute only a limited amount, thus suppressing the influence of scattered noise on the pattern shape. The high-density threshold of 0.60 is set by selecting the midpoint of the plateau segment where the overlap between the actual contaminated area and the density mask is between 0.55 and 0.65. The initial matching threshold is 0.7. A threshold of 5 ensures that fine-grained verification only proceeds when the overall shape of the coordinate pattern matches the template height; an edge strength threshold of 20 suppresses low-contrast noise in the texture, and this value is higher than the mean background noise plus twice the fluctuation range; a direction segment of 36 and a segment width of 5 degrees strike a balance between direction resolution and statistical stability; a grayscale difference segment of 32 performs best in the comparative experiment of balanced performance between low-contrast and high-contrast textures; a secondary matching threshold of 0.70 is used to filter out pseudo-contaminated areas where the direction and texture statistics are inconsistent; the grading interval boundaries adopt a left-closed, right-open method to ensure that each sample is uniquely classified; all thresholds are fixed once deployed and are not adaptively modified during runtime to ensure reproducibility and traceability; finally, the system uniformly writes the contaminated area label, coordinate distribution matching score, histogram matching score, contamination level, upper-left and lower-right coordinates of the region's bounding rectangle, number of region pixels, generation time, and image number of each contaminated area into the database, and establishes a joint index using the image number plus the contaminated area sequence number and the batch number plus the timestamp to generate the classification results of the contaminated areas for subsequent processing.

[0064] The parameter optimization module adjusts the parameters for calculating the complexity index based on the pollution level using a preset module combination logic. It incorporates label type adaptation to optimize the grayscale image conversion process, resulting in an optimized detection configuration scheme. This includes: determining the priority of the module combination logic according to the pollution level using preset classification rules to obtain a module selection sequence; adjusting the weight parameters of the complexity index using a preset complexity calculation formula to obtain optimized complexity parameters; if the optimized complexity parameters exceed a preset threshold, using label classification rules combined with pollution area identifiers to generate label type configurations to obtain label adaptation results; optimizing the grayscale image conversion process using a grayscale conversion algorithm to generate a first grayscale image; extracting the boundary features of the pollution area using an image segmentation algorithm to obtain boundary feature data; adjusting the threshold of the detection configuration scheme using a parameter optimization method to obtain the final detection configuration scheme; and storing the scheme data in a database to generate a pollution detection configuration record.

[0065] The specific implementation process of the parameter optimization module is as follows, and is described in a single complete segment: After receiving the pollution level and pollution area label output by the pollution identification module, the system first classifies the pollution level into three categories—mild, moderate, and severe—according to a fixed classification rule and writes them into the runtime parameter cache; then, it determines the processing order based on a preset module combination logic table. For mild pollution, the order is grayscale conversion optimization, image segmentation, boundary feature extraction, and threshold adjustment; for moderate pollution, the order is grayscale conversion optimization, image segmentation, boundary feature extraction, and a combination of threshold strengthening and relaxation; for severe pollution, the order is grayscale conversion strengthening, adaptive segmentation, full extraction of boundary features, and multi-level linkage adjustment of threshold. Based on this, the system generates a module selection sequence and enters the complexity parameter calculation. The complexity parameter consists of three standardized components. The indicators are weighted and summed to form three sub-indicators: the normalized mean value of edge intensity, the normalized value of texture peak density, and the normalized value of curvature abrupt change point density. All three are compressed to the range of 0 to 100 by linearly mapping the minimum and maximum values ​​of historical statistics before participating in the weighted summation. The weights of the three indicators are 0.4, 0.3, and 0.3 for mild cases, 0.35, 0.4, and 0.25 for moderate cases, and 0.3, 0.5, and 0.2 for severe cases. These three sets of weights are determined by the optimal correlation of the complete set of samples under the constraints of detection rate and false alarm rate and are fixed during deployment. If the calculated complexity parameter is greater than 100, the label type adaptation process is triggered. The threshold of 100 is taken from the stable dividing point between normal and abnormal samples in the complexity parameter distribution.

[0066] Label type adaptation employs deterministic rules, merging contaminated area labels with shape classification labels and using three boundary features—area, aspect ratio, and porosity—for discrete judgment. The area threshold is 500 pixels, the aspect ratio threshold is 3.0, and the porosity threshold is 0.05. These three thresholds are determined by the inflection point values ​​of statistical distributions, ultimately generating three label type configurations: speckle, strip, and patch. The system then optimizes grayscale conversion accordingly. The grayscale conversion uses fixed values ​​for the red, green, and blue channel weights based on the label type configuration. For speckle types, a red weight of 0.35, a green weight of 0.55, and a blue weight of 0.10 are used to enhance the contrast of fine particles; for strip types, a red weight of 0.30, a green weight of 0.60, and a blue weight of 0.60 are used. A weight of 0.10 is used to emphasize texture direction. For moderate or severe patchy textures, a red weight of 0.40, a green weight of 0.50, and a blue weight of 0.10 are used to improve the contrast within the boundaries and regions. All weights are guaranteed to sum to 1 and do not adaptively change during runtime. After grayscale conversion, the first grayscale image is obtained and then processed for image segmentation. For mild cases, region growing segmentation is used, with the starting point selected from the geometric center of the contaminated region label. The admission criteria are that the difference between the grayscale of the newly added pixel and the average grayscale of the current region is no greater than 10, the maximum expansion radius is 100 pixels, and the maximum number of expanded pixels is 5000. These three parameters are jointly determined by coverage requirements and time delay constraints. For moderate cases, Otsu's method is used for global thresholding segmentation, and the segmentation threshold is set accordingly. The system performs morphological refinement near the edges, using a 3x3 structuring element and executing it once. For heavily contaminated areas, adaptive thresholding is used with a local window size of 31 pixels and a constant offset of 5. After segmentation, an opening and a closing operation are performed, with the structuring element being a 3x3 square to eliminate isolated points and fill small holes. The system extracts boundary feature data from the segmentation results, including perimeter, area, shape factor, and curvature abrupt change density. The shape factor represents the degree of boundary curvature as the ratio of perimeter to area, and the curvature abrupt change density represents the detail complexity as the number of abrupt changes per unit boundary length. During the parameter optimization phase, the system jointly adjusts the detection configuration based on the contamination level and boundary features. The key thresholds in the scheme are as follows: for mild cases, the edge strength threshold is set to 20, the curvature change judgment threshold is set to 0.30, and the minimum retained area threshold after segmentation is set to 300 pixels; for moderate cases, the edge strength threshold is lowered to 18, the curvature change judgment threshold remains at 0.30, and the minimum area threshold is increased to 400 pixels; for severe cases, the edge strength threshold is lowered to 15, the curvature change judgment threshold is increased to 0.35, and the minimum area threshold is increased to 600 pixels. The edge strength threshold is determined based on the upper bound of the noise intensity of the defect-free area plus a safety margin. The curvature threshold is determined based on the optimal boundary of the abnormal contour annotation point comparison test. The area threshold is determined based on the 95th percentile of the area distribution of the false alarm area.

[0067] Simultaneously, the gray-level difference threshold for region growth and the constant offset for adaptive segmentation are adjusted in tandem. When the shape factor is greater than 2.5 and the density of curvature abrupt change points is higher than 3 per 100 pixels, the gray-level difference threshold is increased from 10 to 12 and the constant offset is increased from 5 to 7 to improve the connectivity of fine contamination. When the shape factor is less than 1.8 and the density of curvature abrupt change points is less than 1 per 100 pixels, the gray-level difference threshold is decreased from 10 to 8 and the constant offset is decreased from 5 to 3 to avoid overexpansion. All linkage rules are executed with deterministic interval determination and only one set of corrections is applied at a time. The priority order is: baseline threshold driven by contamination degree first, boundary feature linkage second, and minimum area protection last. After the threshold and process parameters are determined, the system generates the final detection configuration scheme and immediately performs consistency verification. The verification includes the validity of the numerical range, detection of conflicts between mutually exclusive parameters, and the integrity of the module sequence. If the verification passes, the pollution level, module selection sequence, three weights, complexity parameter values, all thresholds and process parameters, generation time, image number, and batch number are written into the database. A joint index is established by image number, batch number, and generation time. At the same time, a configuration record number is generated for direct reference in subsequent batch processing. This completes the entire parameter optimization process, driven by the pollution level, through complexity weight adjustment and label adaptation, grayscale conversion optimization and segmentation parameter linkage adjustment, and finally solidification and storage in the database.

[0068] The detection and processing module employs an optimized detection configuration scheme to batch process subsequent image data. If the accuracy of the processing results meets preset standards, a final detection report is output, yielding food quality assessment results adaptable to various scenarios. This includes: generating a priority sequence based on preset classification rules according to food contamination levels to determine module selection results; optimizing batch processing parameters using complexity adjustment rules based on module selection results to obtain an optimized batch processing configuration; if the optimized batch processing configuration meets preset accuracy standards, using a support vector machine algorithm to generate food quality labels and obtain label classification results; processing image data using a grayscale conversion method based on the label classification results to generate a first grayscale image; extracting contaminated area boundaries using a region growing algorithm based on the first grayscale image to obtain boundary data; optimizing the detection configuration using a threshold adjustment method based on the boundary data to obtain the final detection configuration; and storing the processing results in a database using the final detection configuration to generate a food quality assessment record.

[0069] Upon receiving the final detection configuration scheme and the contamination level of each batch of food, the system immediately enters the batch processing flow. First, it generates a priority sequence and determines the module selection results based on fixed classification rules: when the contamination level is mild, the priority sequence is grayscale conversion, region growth, boundary data generation, threshold verification, quality label generation, and result storage; when the contamination level is moderate, the priority sequence is grayscale conversion, region growth, boundary data generation, enhanced complexity threshold verification, quality label generation, and result storage; when the contamination level is severe, the priority sequence is enhanced grayscale conversion, region growth, full boundary data generation, linked threshold verification, quality label generation, and result storage. This sequence is fixed once during deployment and remains unchanged during operation. Subsequently, the system executes complexity adjustment rules according to the module selection results to obtain an optimized batch processing configuration. The rules provide parameter values ​​in a deterministic mapping table and explain and calculate each item: the grayscale difference threshold for region growth is set to 10, 12, and 1 for mild, moderate, and severe contamination levels, respectively. 4. The value of 10 is taken from the upper bound of grayscale fluctuation in the defect-free area plus a safety margin. The values ​​for moderate and severe cases are increased by 2 and 4 respectively to ensure connectivity under complex textures. The maximum expansion radius is set to 100 pixels for mild and moderate cases and 120 pixels for severe cases. The value is determined based on the 95th percentile of the maximum defect extension statistics to cover heavily contaminated edges. The maximum expansion pixel count is 5000, 7000, and 9000 for mild, moderate, and severe cases to limit the upper bound of growth and meet the time delay constraint. The edge strength threshold is 20, 18, and 15 for mild, moderate, and severe cases. The value is determined by the upper bound of the defect-free background noise intensity plus twice the fluctuation range and is adjusted downward with the contamination level to improve the sensitivity of weak edges. The initial value of the minimum retained area threshold is 300 pixels, which serves as the baseline for subsequent threshold verification. The shape factor judgment threshold range is 1.8 to 2.5, and the baseline threshold for curvature change point density is 2 per 100 pixels. The above two items are determined by the joint statistics of boundary complexity and false alarm area.

[0070] After configuration, the system performs an online accuracy self-check to determine if the preset accuracy standard is met. The self-check process involves simultaneously extracting 10% of the labeled reference images from the current batch, performing inductively coupled processing, and calculating accuracy, recall, and false positive rate. The accuracy threshold is 0.92, the recall threshold is 0.90, and the false positive rate threshold is 0.08. These three thresholds are determined by minimizing the weighted loss between missed detections and false positives on the validation set and are fixed during deployment. If the optimized batch processing configuration satisfies all three thresholds, the quality label generation process begins. Otherwise, a fixed backoff table is triggered, lowering the region growth grayscale difference threshold by 2 or the edge intensity threshold by 2 and re-verifying only once. If the requirements are still not met, the batch is marked as needing review, and the current result flag remains unchanged. Under the premise of meeting the accuracy standard, the system calls the support vector machine algorithm to generate food quality labels. The input features are five items: shape complexity value, texture complexity value, mean edge strength, gray-level variance, and curvature abrupt change point density. Each feature is linearly mapped to the interval of 0 to 1 according to the minimum and maximum values ​​recorded during the training phase, and then simultaneously fed into the classifier. The classification output is three categories: qualified, doubtful, and unqualified, and gives the confidence level of each category. The judgment rule is that when the confidence level of unqualified is greater than or equal to 0.6, it is marked as unqualified; when the confidence level of qualified is greater than or equal to 0.7 and the confidence level of unqualified is less than 0.4, it is marked as qualified; the rest are marked as doubtful. The above values ​​are jointly determined by the inflection point of the receiver operating characteristic curve on the validation set and the business risk preference; the system root Based on the label classification results, the image data is processed using a grayscale conversion method to generate a first grayscale image. For qualified and questionable images, a red channel weight of 0.30, a green channel weight of 0.60, and a blue channel weight of 0.10 are used to enhance texture details. For unqualified images, a red channel weight of 0.40, a green channel weight of 0.50, and a blue channel weight of 0.10 are used to improve boundary and patch contrast. The weights of the three channels are fixed during deployment, ensuring a sum of 1. On the first grayscale image, the system uses a region growing algorithm to extract the boundaries of contaminated areas according to the module selection results. The seed point is the geometric center of the defect location coordinate set, with a connectivity of 4. The admission condition is that the difference between the grayscale of a newly added pixel and the average grayscale of the current region does not exceed a previously determined grayscale difference threshold. Each new pixel... After initialization, the grayscale value is immediately included in the statistics. The average grayscale value of the region is recalculated by dividing the updated total grayscale value by the updated number of pixels. If multiple pixels are included at once, their grayscale values ​​are summed before being included in the above calculation. The stopping condition is reaching the maximum expansion radius, reaching the maximum number of expanded pixels, or no new pixels are added. The output boundary data includes the boundary point coordinate sequence, the upper left and lower right coordinates of the circumscribed rectangle, the perimeter, area, shape factor, and curvature abrupt change point density. The system performs a threshold adjustment method based on this boundary data to form the final detection configuration. The rule is that when the shape factor is between 1.8 and 2.5 and the curvature abrupt change point density is between 2 and 3 per 100 pixels, the original threshold remains unchanged. When the shape factor is greater than 2...When the curvature abrupt change point density is not less than 3 per 100 pixels, the grayscale difference threshold is increased by 2 and the minimum retained area threshold is increased to 600 pixels to suppress fragmentation noise. When the shape factor is less than 1.8 and the curvature abrupt change point density is less than 1 per 100 pixels, the grayscale difference threshold is decreased by 2 and the minimum retained area threshold is decreased to 300 pixels to avoid undersegmentation. All adjustments take effect at once and are not cumulative. The priority order is: pollution level baseline first, boundary feature linkage second. Finally, the system processes the remaining images in the current batch with the final detection configuration and generates the quality label, first grayscale image statistical summary, boundary data summary, actual usage threshold, and optimized batch processing for each image. The configuration and online accuracy metrics are written to the database. Record fields include image ID, batch ID, contamination level, module selection sequence, region growth grayscale difference threshold, maximum expansion radius, maximum expansion pixel count, edge strength threshold, minimum retained area threshold, shape factor, curvature abrupt change density, quality label and its confidence level, accuracy, recall, false alarm rate, processing time, and generation time. A joint index is established by image ID, batch ID, and generation time. This generates food quality assessment records and completes the entire process from level-driven to batch processing optimization, from online accuracy verification to label generation, from grayscale conversion and region growth to threshold closed-loop correction, and finally, result storage.

[0071] like Figure 2As shown, a computer vision-based food inspection method is also provided. Using the aforementioned computer vision-based food inspection system, the method includes: acquiring initial food image data from the production line through a preset image acquisition device; processing the data using an edge detection algorithm to extract contour features, while simultaneously incorporating a boundary point coordinate extraction process to obtain a description of the food's appearance boundary; based on the extracted appearance boundary description, grouping the boundary points using a clustering algorithm, and performing iterative convergence processing combined with a distance metric function and cluster center initialization; determining that if the number of groups exceeds a preset threshold, it indicates an irregular shape, thus obtaining shape classification labels; after obtaining the shape classification labels, calculating the surface grayscale gradient distribution using a texture analysis algorithm based on the label type, constructing a distribution histogram by incorporating grayscale image conversion and gradient operator applications, and determining the peak position under the peak detection method to obtain texture complexity. The process involves several steps: First, if the texture complexity index exceeds a preset threshold, irregular groups are selected from the shape classification labels. Noise point filtering combined with clustering label allocation is used to regroup the boundary points, resulting in a set of defect location coordinates. The coordinate distribution pattern is then obtained from this set. If the pattern matches a preset contamination template, it is marked as a contaminated area. Gradient direction analysis and surface texture description are incorporated to assess the matching degree of the distribution histogram and determine the contamination level. Based on the contamination level, the parameters for calculating the complexity index are adjusted using preset module combination logic. Label type adaptation is incorporated to optimize the grayscale image conversion process, resulting in an optimized detection configuration scheme. This optimized scheme is then used to batch process subsequent image data. If the accuracy index of the processing results meets a preset standard, a final detection report is output, yielding a food quality assessment result suitable for multiple scenarios.

[0072] The first step of this method is image acquisition and contour extraction: an industrial camera installed on the production line acquires images at a resolution of 1920 x 1080 and stores them in an uncompressed format; the color images are converted to grayscale with a weight of 0.30 for the red channel, 0.59 for the green channel, and 0.11 for the blue channel; Gaussian smoothing is performed on the grayscale images with a window size of 5 x 5 and a standard deviation of 1.0; an edge detection algorithm is used for gradient and non-maximum suppression processing, and a double threshold hysteresis is performed with a low threshold of 10 for the maximum grayscale value and a high threshold of 30 for the maximum grayscale value to obtain the first contour feature; when the contour continuity is lower than the threshold of 250 pixels, one dilation and one erosion are performed with a 3 x 3 square as the structuring element to obtain the second contour feature; then the coordinates of the boundary points are tracked and recorded clockwise, and spline interpolation is performed every 10 points to form a continuous boundary curve, while the curvature change is statistically analyzed and the curvature abrupt change points are recorded by a three-point sliding method every 5 points. Step 2, Boundary Clustering and Shape Determination: Perform mean clustering on all boundary points, with an initial cluster center count of 5. The initial centers are uniformly extracted in point order, using straight-line distance as the distance metric. The convergence condition is a center coordinate change of less than 1 pixel or 100 iterations. If the final number of groups is greater than 5, it is determined to be an irregular shape, and a shape classification label is generated. Step 3, Texture Complexity Calculation: Extract the surface texture region according to the shape label, setting the direction to 0 degrees, 45 degrees, 90 degrees, 135 degrees, and pixel spacing to 1. Calculate the gray-level co-occurrence matrix and take the average of the four directions. Apply the gradient operator to the result to calculate the horizontal and vertical gradient intensities and synthesize them into gradient magnitudes. Statistically generate a gradient histogram, smooth it using a moving average with a window length of 5, and then perform peak detection. Count the number of peaks as a texture complexity index. When the number of peaks is greater than 50, proceed to the next step. Step 4, coordinate-level defect localization: After denoising the boundary points in the irregular grouping by applying mean filtering to the 3x3 neighborhood, mean clustering with a cluster size of 3 is used to regroup the points and calculate the geometric center of each group. The set of centers is matched with the defect template with a matching radius of 8 pixels and a matching degree threshold of 0.80. When the matching degree is greater than 0.80, region growing is performed on the group with a connectivity mode of 4-connectivity. The admission criteria are that the difference between the gray level of the newly added pixel and the average gray level of the region is not greater than 10, the maximum expansion radius is 100 pixels, and the maximum number of expanded pixels is 5000. The boundary and center coordinates of the expanded region are obtained and summarized into a set of defect location coordinates.Step 5, Pollution Identification and Level Determination: Coordinate density is statistically calculated using 10x10 grid cells. Density estimation and normalization are performed within a 15-pixel radius using three-segment distance weights. High-density areas are aggregated and connected at a density threshold of 0.60. The coordinate distribution is intersected grid-by-grid with the pollution template to calculate the initial matching score. Areas with a score greater than 0.75 are marked as polluted. Within each polluted area, 36 directional histograms and 32 grayscale difference histograms are statistically analyzed and stitched together to form a 68-segment histogram. The intersection method is used to calculate the matching score with the template histogram. Areas with a score greater than 0.70 are mapped to a level according to intervals: 0.70 to less than 0.80 indicates mild pollution, 0.80 to less than 0.90 indicates moderate pollution, and 0.90 to no more than 1.00 indicates severe pollution. Step 6, Parameter Optimization and Configuration Generation: Based on the pollution level, query the fixed module combination logic and weight table, and calculate the complexity parameter as a weighted sum of three standardized sub-indicators: the standardized value of the mean edge intensity, the standardized value of the texture peak density, and the standardized value of the curvature abrupt change point density. The weights are 0.40, 0.30, and 0.30 for mild cases, 0.35, 0.40, and 0.25 for moderate cases, and 0.30, 0.50, and 0.20 for severe cases. When the complexity parameter is greater than 100, perform label type adaptation, based on the region area being greater than or equal to 500 pixels and the aspect ratio being greater than... Discrete rules with a value of 3.0 and a porosity greater than or equal to 0.05 are used to generate label type configurations. Based on the configuration, the weights of the grayscale conversion channels are deterministically set: 0.35, 0.55, and 0.10 for speckle type, 0.30, 0.60, and 0.10 for strip type, and 0.40, 0.50, and 0.10 for patch type. The first grayscale image is then output. Mild cases are segmented using region growing, moderate cases are segmented using global thresholding with a 3x3 thinning step, and severe cases are segmented using adaptive thresholding with a local window of 31 pixels and a constant offset of 5 pixels. An opening operation and a closing operation are then performed. Step 7, Batch Detection and Report Output: Processing is performed in parallel with a batch size of 32 images and 4 parallel tasks; Online accuracy self-check is performed by sampling labeled images at a ratio of 10 and calculating accuracy, recall, and false alarm rate, with thresholds of 0.92, 0.90, and 0.08, respectively. If all conditions are met, quality labels are generated based on the trained classifier and batch processing continues; otherwise, the grayscale difference threshold or edge intensity threshold is reduced by 2 according to a fixed backoff table and retested once; After all samples have been processed, the quality labels, boundary summaries, thresholds used, and performance indicators are written into the database and the final detection report is generated.

[0073] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.

Claims

1. A computer vision-based food inspection system, characterized in that, include: The image acquisition module acquires initial food image data from the production line through a preset image acquisition device, processes the data using an edge detection algorithm to extract contour features, and incorporates a boundary point coordinate extraction process to obtain a description of the food's appearance boundary. The boundary clustering module uses a clustering algorithm to group boundary points based on the extracted appearance boundary description. It combines a distance metric function and cluster center initialization to perform iterative convergence processing. If the number of groups exceeds a preset threshold, it indicates that the shape is irregular, and shape classification labels are obtained. The feature analysis module, after obtaining the shape classification label, calculates the surface gray-level gradient distribution according to the label type using a texture analysis algorithm. It integrates gray-level image conversion and gradient operator application to construct a distribution histogram, and determines the peak position under the peak detection method to obtain the texture complexity index. If the texture complexity index is higher than the preset threshold, the coordinate extraction module selects irregular groups from the shape classification labels, uses noise point filtering combined with clustering label allocation to regroup the boundary points, and obtains the defect location coordinate set. The contamination identification module obtains the coordinate distribution pattern from the defect location coordinate set. If the coordinate distribution pattern matches the preset contamination template, it is marked as a contamination area. At the same time, gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram and determine the contamination level. The parameter optimization module adjusts the parameters calculated by the complexity index according to the pollution level through preset module combination logic, and incorporates label type adaptation to optimize the grayscale image conversion process, thereby obtaining an optimized detection configuration scheme. The detection and processing module uses an optimized detection configuration scheme to process subsequent image data in batches. If the accuracy index of the processing result meets the preset standard, the final detection report is output, resulting in food quality assessment results that are suitable for multiple scenarios.

2. The food inspection system based on computer vision according to claim 1, characterized in that: The image acquisition module acquires initial food image data from the production line through a preset image acquisition device, processes the data using an edge detection algorithm to extract contour features, and incorporates a boundary point coordinate extraction process to obtain a description of the food's appearance boundary, including: Initial image data of food is acquired from the production line using a pre-set image acquisition device and stored in a standardized format to obtain an initial image dataset. The Canny edge detection algorithm is used to process the initial image dataset and extract contour features to obtain the first contour feature set; If the edge continuity of the first contour feature set is lower than a preset threshold, the contour is optimized through morphological operations to obtain the second contour feature set. The boundary point coordinates are extracted from the second contour feature set using a boundary point coordinate extraction algorithm to generate a food appearance boundary point set. A continuous boundary curve is generated based on the boundary point set using a spline interpolation algorithm to obtain a description of the food appearance boundary. Geometric analysis is performed on the boundary description of food appearance to calculate the boundary curvature characteristics and obtain the food appearance shape feature set. By storing food appearance shape feature sets in a database, food appearance analysis results are generated.

3. The food inspection system based on computer vision according to claim 1, characterized in that: The boundary clustering module, based on the extracted appearance boundary description, uses a clustering algorithm to group boundary points. It then combines a distance metric function and cluster center initialization for iterative convergence processing. If the number of groups exceeds a preset threshold, it indicates an irregular shape, resulting in shape classification labels including: Irregular shape boundary point data are obtained from shape classification labels, and the boundary point coordinates are reduced in dimensionality using principal component analysis algorithm to obtain a low-dimensional boundary point set. The spatial distribution characteristics between point sets are calculated by using a low-dimensional boundary point set, and the distance between points is measured using the Euclidean distance metric function to obtain the point set distribution characteristic set; If the variance of the distance between points in the feature set of the point set exceeds a preset threshold, the minimum bounding polygon of the boundary points is generated by the convex hull algorithm to obtain the bounding boundary description. The geometric parameters of the polygon are calculated based on the description of its circumscribed boundary, and the shape complexity characteristics are obtained by using the perimeter-to-area ratio calculation method. Feature vectors are extracted from shape complexity features, and the support vector machine algorithm is used to classify the feature vectors to obtain food shape category labels; If the food shape category label indicates a non-standard shape, the boundary points are smoothed using a spline interpolation algorithm to obtain a smooth boundary point set. The food appearance boundary description is regenerated based on the smooth boundary point set, stored in the database, and the final food shape analysis result is obtained.

4. The food inspection system based on computer vision according to claim 1, characterized in that: The feature analysis module, after obtaining shape classification labels, calculates the surface gray-level gradient distribution using a texture analysis algorithm based on the label type. It then incorporates gray-level image conversion and gradient operator applications to construct a distribution histogram, determining the peak position under the peak detection method to obtain texture complexity indices, including: Surface texture data is obtained from shape classification labels, and texture features are calculated using the gray-level co-occurrence matrix algorithm to generate a texture feature matrix; The texture feature matrix is ​​processed by gradient operators to calculate the gray-level gradient distribution and generate a gray-level gradient histogram. For the gray-level gradient histogram, a peak detection algorithm is used to determine the peak position and obtain the texture complexity index; If the texture complexity index exceeds the preset threshold, the texture region is separated by an image segmentation algorithm to generate a texture sub-region set; Based on the texture sub-region set, calculate the spatial distribution characteristics of the sub-regions and generate a distribution feature vector; By using the distributed feature vectors, the K-means clustering algorithm is used to group the texture sub-regions to obtain texture classification labels; For each texture classification label, the classification results are stored in the database to generate the final texture analysis results.

5. A food inspection system based on computer vision according to claim 1, characterized in that: If the texture complexity index of the coordinate extraction module is higher than a preset threshold, it selects irregular groups from the shape classification labels, and regroups the boundary points using noise point filtering combined with clustering label allocation to obtain the defect location coordinate set, including: If the texture complexity index is higher than the preset threshold, irregular groups are obtained from the shape classification labels, and the point set in the irregular group is processed by mean filtering to obtain the denoised point set. By using the denoised point set, the K-means clustering algorithm is used to regroup the point set to generate preliminary boundary point groups; Based on the initial boundary point grouping, calculate the geometric center point of each group to obtain the center point coordinate set; If the matching degree between the center point coordinate set and the preset defect template is higher than the threshold, the region growing algorithm is used to expand the boundary point group to generate an expanded boundary region. By expanding the boundary region, the gray value distribution characteristics within the region are extracted to obtain the gray value feature vector; Based on the grayscale feature vectors, the support vector machine algorithm is used to classify the regions and determine the defect type label; By using defect type labels, the classification results are stored in the database, and a set of defect location coordinates is generated.

6. The food detection system based on computer vision according to claim 1, characterized in that: The contamination identification module obtains the coordinate distribution pattern from the defect location coordinate set, and marks the area as contaminated if the coordinate distribution pattern matches a preset contamination template. It also incorporates gradient direction analysis and surface texture description to evaluate the matching degree of the distribution histogram, determining the contamination level including: The coordinate distribution pattern is obtained from the defect location coordinate set, and the spatial distribution characteristics of the coordinate points are analyzed by the density estimation method to generate a distribution pattern description. If the matching degree between the coordinate distribution pattern and the preset contamination template is higher than the threshold, it is marked as a contamination area and a contamination area label is generated. By using the labels of polluted areas, gradient direction analysis is employed to extract edge features within the polluted areas, resulting in edge feature vectors. Based on the edge feature vectors and surface texture descriptions, a distribution histogram is constructed to generate histogram data; The histogram intersection method is used to calculate the matching degree between the distribution histogram and the preset pollution template histogram, and the matching degree score is obtained. If the matching score is higher than the threshold, the level of contamination is determined based on the score range mapping. Based on the pollution level, the level data is stored in the database to generate classification results for polluted areas.

7. A food inspection system based on computer vision according to claim 1, characterized in that: The parameter optimization module, based on the level of contamination, adjusts the parameters used to calculate the complexity index through a preset module combination logic, and incorporates label type adaptation to optimize the grayscale image conversion process, resulting in an optimized detection configuration scheme including: Based on the pollution level, the priority of the module combination logic is determined using preset classification rules, resulting in a module selection sequence. Based on the module selection sequence, the weight parameters of the complexity index are adjusted using a preset complexity calculation formula to obtain the optimized complexity parameters. If the optimized complexity parameter is higher than the preset threshold, the label classification rule is adopted, combined with the pollution area identification, to generate the label type configuration and obtain the label adaptation result. Based on the label adaptation results, a grayscale conversion algorithm is used to optimize the grayscale image conversion process and generate the first grayscale image.

8. A computer vision-based food inspection system according to claim 7, characterized in that: The parameter optimization module, based on the level of contamination, adjusts the parameters used to calculate the complexity index through a preset module combination logic, incorporates label type adaptation to optimize the grayscale image conversion process, and obtains an optimized detection configuration scheme, which also includes: Based on the first grayscale image, an image segmentation algorithm is used to extract the boundary features of the polluted area to obtain boundary feature data; Based on the boundary feature data, the threshold of the detection configuration scheme is adjusted using a parameter optimization method to obtain the final detection configuration scheme; Based on the final detection configuration scheme, the scheme data is stored in the database, and a configuration record for pollution detection is generated.

9. A food inspection system based on computer vision according to claim 1, characterized in that: The detection and processing module uses an optimized detection configuration scheme to batch process subsequent image data. If the accuracy index of the processing result meets the preset standard, it outputs the final detection report, obtaining food quality assessment results suitable for multiple scenarios, including: Based on the food contamination level, a priority sequence is generated using preset classification rules to determine the module selection result; By selecting modules and applying complexity adjustment rules, batch processing parameters are optimized to obtain an optimized batch processing configuration. If the optimized batch processing configuration meets the preset accuracy standard, the support vector machine algorithm is used to generate food quality labels and obtain label classification results. Based on the label classification results, the image data is processed using a grayscale conversion method to generate the first grayscale image; Using the first grayscale image, a region growing algorithm is employed to extract the boundaries of the contaminated area, thus obtaining boundary data. Based on the boundary data, a threshold adjustment method is used to optimize the detection configuration and obtain the final detection configuration. The final testing configuration stores the processing results in the database, generating food quality assessment records.

10. A computer vision-based food detection method, employing the computer vision-based food detection system according to any one of claims 1-9, characterized in that, The method includes: Initial food image data is acquired from the production line using a pre-set image acquisition device. The data is then processed using an edge detection algorithm to extract contour features. At the same time, the boundary point coordinate extraction process is incorporated to obtain a description of the food's appearance boundary. Based on the extracted appearance boundary description, a clustering algorithm is used to group the boundary points. Combined with the distance metric function and cluster center initialization, iterative convergence processing is performed. If the number of groups exceeds a preset threshold, it indicates that the shape is irregular, and shape classification labels are obtained. After obtaining the shape classification labels, the surface gray-level gradient distribution is calculated using a texture analysis algorithm based on the label type. A distribution histogram is constructed by incorporating gray-level image conversion and gradient operator applications, and the peak position under the peak detection method is determined to obtain the texture complexity index. If the texture complexity index is higher than the preset threshold, irregular groups are selected from the shape classification labels, and the boundary points are regrouped by combining noise point filtering with clustering label allocation to obtain the defect location coordinate set. The coordinate distribution pattern is obtained from the defect location coordinate set. If the coordinate distribution pattern matches the preset contamination template, it is marked as a contaminated area. At the same time, gradient direction analysis and surface texture description are incorporated to evaluate the matching degree of the distribution histogram and determine the contamination level. Based on the pollution level, the parameters for calculating the complexity index are adjusted through preset module combination logic, and label type adaptation is incorporated to optimize the grayscale image conversion process, thereby obtaining an optimized detection configuration scheme. An optimized detection configuration scheme is used to process subsequent image data in batches. If the accuracy index of the processing result meets the preset standard, the final detection report is output, resulting in food quality assessment results that are suitable for multiple scenarios.

Citation Information

Patent Citations

  • Real-time online detection method and system for fruit appearance defects

    CN119845962A

  • Food freshness detection method and system based on image recognition

    CN120672874A