Egg product defect detecting and grading method based on machine vision

By introducing automatic eggshell color recognition and a few-sample meta-learning mechanism, combined with multispectral image acquisition and adaptive threshold adjustment, the generalization ability and cost issues of machine vision egg inspection methods under different eggshell colors are solved, achieving high-precision, adaptive defect detection and grading.

CN121544551APending Publication Date: 2026-02-17XIAN RAINBOW PLANET AGRI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511683471.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-17
Publication Date
2026-02-17

AI Technical Summary

Technical Problem

Existing machine vision methods for detecting defects in eggs lack inherent adaptability in feature extraction and discrimination mechanisms when faced with different eggshell colors. They also have poor generalization ability of detection parameters and rely on large-scale labeled data, resulting in high costs and difficulty in adapting to the needs of production lines that frequently change varieties.

Method used

By introducing an automatic eggshell color recognition mechanism, a small sample learning parameter generation mechanism, and a defect discrimination threshold dynamic adjustment mechanism based on the generated parameters, cross-variety egg defect detection and grading can be achieved through multispectral image acquisition, eggshell color type recognition, small sample learning, and adaptive threshold adjustment.

Benefits of technology

It significantly improves the system's ability to generalize to diverse egg varieties, reduces the cost of model training and data labeling when new varieties are launched, improves detection accuracy and the system's flexible response speed, and reduces hardware investment costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121544551A_ABST
    Figure CN121544551A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of machine vision, in particular to an egg product defect detecting and grading method based on machine vision, and aims to solve the problems that an existing method is poor in adaptability, weak in generalization ability, heavy in dependence on large-scale labeled data and high in deployment cost when eggshells are different in color. The method comprises the following steps: acquiring a multispectral image; identifying eggshell color types; generating small sample element learning parameters; carrying out variety adaptive defect detection, and judging a threshold value by dynamically adjusting; and multi-dimensional grading decision making is carried out. By adopting the technical scheme, high-precision and self-adaptive detection and grading of the surface defects of multiple varieties of egg products can be realized, the generalization ability of the system is improved, and the model training and data labeling cost is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of machine vision technology, and in particular to a machine vision-based method for detecting and grading defects in egg products. Background Technology

[0002] Currently, machine vision-based methods for egg defect detection mainly rely on traditional image processing (such as thresholding and edge detection) or deep learning models (such as CNN and YOLO). However, these methods face the following problems in practical applications: 1. Weak variety generalization ability: Different eggshell colors (such as red and white shells) lead to significant differences in surface reflectivity, and the same set of image processing parameters or models are unstable on different varieties. For example, stains on white-shelled eggs have high contrast in grayscale images and are easy to segment; while the same stains on red-shelled eggs are often misjudged as normal textures due to the dark background color. 2. Reliance on large amounts of labeled data: Deep learning methods require the separate collection and labeling of hundreds to thousands of defect samples for each eggshell color, which is costly and difficult to adapt to the needs of frequent product changes on the production line; 3. Fixed defect discrimination threshold: Existing methods mostly use global or static thresholds to judge dirtiness and dark spots, which cannot be dynamically adjusted according to eggshell color, leading to false detection or false detection; 4. Lack of color-driven adaptive mechanism: Although there are methods to attempt to group by color, they require manual setting of rules or retraining of the model, and cannot achieve automated and intelligent parameter transfer.

[0003] Therefore, there is an urgent need for a machine vision-based egg defect detection and grading method that can achieve cross-variety adaptation without large-scale retraining and only rely on a small number of samples. Summary of the Invention

[0004] To address the technical challenges of existing machine vision-based egg defect detection methods, such as the lack of inherent adaptability in feature extraction and discrimination mechanisms, poor generalization ability of detection parameters, heavy reliance on large-scale labeled data, and high deployment costs when faced with different eggshell colors, this invention provides a machine vision-based egg defect detection and grading method. This method aims to achieve high-precision, adaptive detection and grading of surface defects in multiple egg varieties by introducing an automatic eggshell color recognition mechanism, a small-sample meta-learning parameter generation mechanism, and a dynamic adjustment mechanism for defect discrimination thresholds based on the generated parameters. This significantly improves the system's generalization ability to diverse egg varieties and reduces the model training and data labeling costs when introducing new egg varieties.

[0005] To achieve the above-mentioned objectives, the present invention provides the following technical solution: A machine vision-based method for egg product defect detection and grading includes the following steps: S1. Multispectral Image Acquisition: On the industrial inspection line, eggs awaiting inspection are sequentially conveyed to the image acquisition station via a conveyor mechanism. At this station, an image acquisition module is installed, comprising one or more industrial-grade area scan cameras, a white light illumination unit, and a warm-color light illumination unit. The industrial-grade area scan camera employs a global shutter CMOS sensor with an effective pixel resolution of no less than 2448×2048 pixels, a pixel size no larger than 3.45μm×3.45μm, an image acquisition frame rate of at least 100fps, and data transmission with the image processing unit via a Gigabit Ethernet interface. The camera is equipped with a low-distortion, fixed-focal-length industrial lens with an aperture range of F / 2.8-F / 16. The white light illumination unit consists of a ring-shaped array of high-brightness white LEDs with a color temperature of 5000K and a luminous intensity of 25000 candela. It is powered by a constant-current LED driver and its brightness is precisely controlled by a pulse-width modulation (PWM) signal to ensure uniform illumination on the surface of the eggs being inspected. The warm-color light illumination unit consists of a ring-shaped array of warm-color LEDs with a color temperature of 3000K, a spectral center of approximately 620nm, a spectral half-width of ±20nm, and a luminous intensity of 18000 candela. It is also powered by an independent constant-current LED driver and controlled by a PWM signal. The white light illumination unit and the warm-color light illumination unit employ an alternating flash pattern and are precisely time-controlled by a hardware trigger unit synchronized with the camera shutter.

[0006] Specifically, within one egg inspection cycle, the industrial-grade area scan camera first acquires a first frame image with the white light illumination unit momentarily turned on, obtaining an RGB image of the egg's surface. Then, the white light illumination unit is turned off, the warm light illumination unit is momentarily turned on, and the industrial-grade area scan camera acquires a second frame image, obtaining a warm light reflection image of the egg. The image data captured by the image acquisition module is transmitted in real-time to the subsequent image processing unit via a high-speed data interface. The conveying mechanism includes a food-grade conveyor belt and a position encoder. The position encoder is used to accurately detect the position of each egg on the production line and sends the position signal to the hardware trigger unit to ensure that each egg is imaged at the accurate detection position.

[0007] S2. Eggshell color type identification: After receiving the RGB image obtained in step S1, the image processing unit first performs a background segmentation operation.

[0008] Specifically, by applying the Otsu thresholding algorithm to the luminance (L) channel of the RGB image, combined with morphological opening operations to eliminate small noise points, and connected component analysis to select the region with the largest area as the main body of the egg, the egg is accurately separated from the background. From the segmented main body region of the egg, a central region is extracted as a region of interest (ROI) for color feature extraction. The size of the ROI is 50% of the length of the minor axis of the largest circumscribed ellipse of the main body region of the egg, with the geometric center of the egg as the center. Then, the RGB pixel values ​​of the ROI region are converted to the CIE Lab color space.

[0009] The conversion process from RGB to Lab color space follows the International Commission on Illumination (CIE) standard, using the D65 standard light source as the reference white point, linearly transforming the RGB values ​​to the CIE XYZ space, and then further transforming them to the Lab space.

[0010] Specifically, the 'a' channel in the Lab color space represents the color range from green to red. Statistical analysis is performed on the 'a' channel values ​​of all pixels within the ROI region to construct a grayscale histogram of the 'a' values. Based on this histogram, the K-means clustering algorithm is used to cluster the 'a' channel values, where K is set to a preset number of egg color types, for example, K=3, corresponding to the three common color types: red shell, white shell, and pink shell. The K-means algorithm uses a K-means++ initialization strategy to optimize cluster center selection, and the iterative convergence condition is set to the cluster center position moving a distance of less than 0.001 units of 'a' value in five consecutive iterations. After clustering, based on the specific numerical range of each cluster center on the 'a' axis, the eggshell color type of the current egg is automatically determined as red shell, white shell, or pink shell.

[0011] The specific criteria are as follows: if the cluster center value of cluster a falls within the range [15, 30], it is determined to be red-shelled; if the cluster center value of cluster a falls within the range [-5, 5], it is determined to be white-shelled; if the cluster center value of cluster a falls within the range [5, 15], it is determined to be pink-shelled. The determination result will be assigned as a color label to the egg product to be inspected and stored in the egg product information database for subsequent defect detection and grading decisions.

[0012] S3. Generation of parameters for small sample meta-learning: A list of known eggshell color types and their corresponding defect discrimination parameter library are maintained in the egg product information database. When the current eggshell color type (i.e., the color label) determined in step S2 does not match any item in the list of known color types, a newly appearing eggshell color type is identified. At this time, the system will trigger the small sample meta-learning parameter generation process.

[0013] Specifically, 5 to 10 eggs with the first appearance of the color label are automatically selected from the current batch. These eggs are initially screened as "no visible defects" by a pre-defined general coarse defect detection model to ensure the quality of the support set. The general coarse defect detection model is based on a convolutional neural network (CNN) extensively trained on various eggshell colors, which can quickly identify obvious dirt or cracks with low precision, thereby excluding samples with significant defects. The RGB images of the 5 to 10 "no visible defects" eggs constitute a support set and are input into a pre-trained meta-learner. The meta-learner adopts a prototypical network architecture, the core of which consists of a deep encoder (e.g., a modified ResNet-18 with the top classification layer removed) and a distance metric module. The deep encoder has been meta-trained offline using a large-scale, multi-domain egg dataset to learn a metric space such that eggs of the same category (e.g., "normal") are close in the feature space, while eggs of different categories are far apart, and the model has the ability to quickly adapt to new categories. After the support set images are input into the deep encoder of the meta-learner, a high-dimensional feature vector is extracted for each image. Subsequently, the feature vectors of all images in the support set are averaged to obtain the "normal egg product prototype" feature vector for the first appearance of the eggshell color type.

[0014] Based on the feature vector of the "normal egg product prototype", the system further calculates the visual feature normalization parameters (α, β) of the egg product of this color type.

[0015] Specifically, the α parameter is defined as the arithmetic mean (μ) of the dirtiness index of all "no visible defects" eggs in the support set, and the β parameter is defined as the standard deviation (σ) of the dirtiness index of all "no visible defects" eggs in the support set. The calculation method of the dirtiness index is detailed in step S4. The α and β parameters are considered as statistical representations of the normal appearance characteristics of eggs of this new color type. The α and β parameters are then stored in the parameter library and associated with the new color label. If the color type corresponding to the color label already exists in the list of known color types, the system directly loads the pre-stored parameters α and β associated with the color label from the parameter library, without having to execute the small sample meta-learning parameter generation process again.

[0016] S4. Variety-Adaptive Defect Detection: This step performs surface anomaly detection and dirt level assessment in parallel, and dynamically adjusts the discrimination threshold based on the eggshell color.

[0017] S4.1 Dark Spot Area Identification: Based on the warm-colored light reflection image obtained in step S1, the image processing unit first performs preprocessing operations, including applying Gaussian filtering (e.g., a 5x5 kernel with a standard deviation of 1.0) to smooth the image and remove random noise. An adaptive thresholding algorithm (e.g., the Niblack or Sauvola algorithm, with a window size of 15x15 pixels) is used to identify surface reflection anomalies or color difference regions in the image. The adaptive thresholding algorithm dynamically adjusts the threshold according to local image characteristics, effectively addressing uneven illumination. After segmentation, morphological closing operations (e.g., 3x3 circular structuring elements) are applied to connect the segmented small fracture areas and fill micro-holes, thereby identifying independent abnormal regions through connected component analysis. For each identified connected component, its pixel area, perimeter, equivalent diameter, and average gray value are calculated. If the total pixel area of ​​the abnormal surface regions exceeds a preset threshold (e.g., 50 pixels), or the equivalent diameter of any dark spot exceeds a preset threshold (e.g., 3 mm), or its average gray value is lower than the average gray value of its surrounding local background by a preset percentage (e.g., 15%), then the egg product is determined to have a surface abnormality.

[0018] S4.2 Dirtiness Index Calculation: Based on the RGB image obtained in step S1, it is first converted into a grayscale image. The conversion follows a weighted average method, i.e. Subsequently, the local grayscale standard deviation and texture entropy are calculated on the grayscale image.

[0019] The local grayscale standard deviation is calculated by taking each pixel in the image and calculating the standard deviation of all pixel grayscale values ​​within a local window (e.g., a 7x7 pixel square kernel) centered on that pixel. The formula for calculating the standard deviation is: ; in, The pixel grayscale value within a local window. It is the average value of the pixel grayscale values ​​within the local window. This represents the number of pixels within a local window. This metric reflects the degree of grayscale variation in a local area, i.e., the richness of image detail or texture.

[0020] The texture entropy is calculated using the Gray-Level Co-occurrence Matrix (GLCM) method. For the grayscale image, a 32x32 GLCM is constructed by setting the grayscale levels to 32 levels and performing calculations at four directions (0°, 45°, 90°, and 135°) with a pixel distance of 1. Then, the GLCMs at these four directions are averaged to obtain a comprehensive GLCM. Based on this comprehensive GLCM, the texture entropy of the image is calculated using the following formula: ; in, is the normalized frequency value at position (i,j) in GLCM, representing the probability of pixel pair (i,j) occurring. Texture entropy reflects the complexity and disorder of local texture in an image.

[0021] The local grayscale standard deviation map and the texture entropy map are each normalized (e.g., linearly mapped to the range of 0 to 1), and then weighted and summed to obtain the final dirtiness index. The weighting coefficients of the weighted summation are set empirically; for example, the dirtiness index is calculated as follows: Dirtyness index = 0.6X (normalized local grayscale standard deviation) + 0.4X (normalized texture entropy); The higher the dirtiness index, the greater the degree of dirtiness or abnormal texture on the surface of the egg product.

[0022] S4.3 Dirtiness Index Judgment Threshold Dynamic Adjustment: Using the parameters α and β obtained in step S3 for the current egg color label, the discrimination threshold of the dirtiness index is dynamically adjusted. Specifically, for any color type of egg, the discrimination threshold of its dirtiness index (hereinafter referred to as the "dirtiness threshold") is dynamically determined according to the following formula: ; Wherein, α and β are the average (μ) and standard deviation (σ) of the current egg color type calculated or loaded in step S3, respectively, and K is a preset sensitivity coefficient. The value of K is determined through offline experiments, for example, set to 2.5, which represents 2.5 standard deviations outward from the average dirtiness level of normal eggs, serving as the boundary for distinguishing between normal and defective products.

[0023] The parameters α and β of this invention, as statistical quantities describing the distribution of normal surface features of eggs of a specific color, enable the dirt threshold to be adaptively adjusted according to the inherent optical properties and texture differences of different eggshell colors.

[0024] Specifically, for known red-shelled eggs, the corresponding α and β parameters, after being substituted into the above formula and combined with K=2.5, result in a dirt threshold of 0.5. For white-shelled eggs, the corresponding α and β parameters, after being substituted into the above formula, result in a dirt threshold of 0.3. For pink-shelled eggs, the corresponding α and β parameters, after being substituted into the above formula, result in a dirt threshold of 0.4. These specific values ​​reflect the differences in image features due to the physical characteristics of different eggshell colors, such as the depth of their base color and the coarseness of their texture, resulting in the same level of dirt. For example, red-shelled eggs, due to their darker base color, have relatively lower contrast in their stains, thus their dirt threshold is higher than that of white-shelled and pink-shelled eggs, thereby avoiding misjudging normal dark textures or slight uneven lighting as dirt. The dynamic adjustment mechanism can adaptively process different eggshell colors without manual intervention or retraining.

[0025] S5. Multi-dimensional hierarchical decision-making: Based on the surface anomaly detection results obtained in step S4.1 and the dirtiness index and dynamically adjusted dirtiness threshold obtained in steps S4.2 and S4.3, a multi-dimensional classification judgment is performed. The classification decision logic is as follows: If the dirtiness index of the current egg product is less than or equal to its corresponding dynamically adjusted dirtiness threshold, and no abnormal surface areas are detected in step S4.1 (i.e., the total pixel area of ​​abnormal areas is less than 50 pixels and no single abnormal area has an equivalent diameter exceeding 3 mm), then the egg product is determined to be Grade A. Grade A eggs represent high-quality eggs with clean surfaces and no obvious defects.

[0026] If the dirtiness index of the current egg product is greater than its corresponding dynamically adjusted dirtiness threshold, but less than or equal to 0.7, and no surface abnormalities are detected in step S4.1, then the egg product is determined to be Grade B. Grade B eggs represent qualified eggs with slight surface dirt but no obvious structural defects.

[0027] If any egg product exhibits any form of surface abnormality (regardless of size or grayscale deviation) or its dirtiness index is greater than 0.7, the egg product is classified as Grade C or directly rejected. Grade C or rejected eggs represent defective eggs with significant surface dirt that may affect food safety or severely impact appearance.

[0028] The grading decision results will be sent via digital signals to the sorting execution mechanism on the production line (such as a PLC-controlled robotic arm or pneumatic sorting device) to achieve physical sorting of the eggs. Simultaneously, all detection data, including the grading results, eggshell color labels, dirtiness index, and surface anomaly detection details, are stored in a central database for production batch tracking, quality traceability, and subsequent data analysis and system optimization.

[0029] The machine vision-based egg defect detection and grading method provided by this invention has the following advantages compared with the prior art: 1. This invention introduces a meta-learner and a few-shot learning mechanism. When encountering a newly emerging eggshell color type, it only needs to collect 5 to 10 images of eggs with "no visible defects" after initial screening as a support set to generate the normalized parameters (α, β) of the visual features of that color type of egg, thereby dynamically determining its defect discrimination threshold. This process eliminates the need for large-scale defect sample annotation and model retraining, significantly shortening the adaptation cycle for new product launches or production line switching, greatly reducing the manpower and time costs of data annotation, and improving the system's flexibility and response speed.

[0030] 2. This invention achieves dynamic adjustment of the dirtiness index discrimination threshold by accurately identifying eggshell color type and combining it with parameters α and β generated by a meta-learner. The dirtiness threshold can be adaptively adjusted based on the inherent optical characteristics and background texture differences of different eggshell colors (such as red, white, and pink). For example, addressing the issue of reduced contrast in stains on red-shelled eggs due to their darker background color, the system can increase its dirtiness threshold, effectively avoiding misjudging normal dark textures or slight uneven lighting as dirt, thereby significantly improving the detection accuracy of dark-colored eggs and reducing the false positive and false negative rates.

[0031] 3. This invention is primarily based on optimized image processing algorithms and an innovative parameter adaptive adjustment process. Compared to solutions that rely entirely on end-to-end deep learning models and require massive computing resources for real-time inference, this invention achieves high detection accuracy while having relatively low requirements for computing hardware. Its main computational overhead is concentrated on image feature extraction and parameter calculation, without relying on complex neural network architectures for real-time discrimination. This makes the entire system easier to deploy on standard industrial PCs or embedded platforms, reducing hardware investment costs and improving the system's deployability and maintenance convenience.

[0032] 4. This invention combines surface anomaly detection results from warm-colored light reflection image recognition with a dirtiness index calculated from RGB image analysis to construct a multi-dimensional, multi-level egg grading decision logic. By comprehensively considering the degree of dirtiness on the egg surface and the presence of microscopic reflection anomalies or local color differences that are difficult to detect with the naked eye, it can make a refined A, B, and C grade judgment for eggs. The grading standards are clear and quantifiable, which helps to achieve stricter quality control and more accurate product classification, thereby increasing product premium and meeting the different market demands for egg quality control.

[0033] 5. This invention employs multispectral image acquisition, combining white light RGB images and warm light reflectance images to capture egg defect information from different physical dimensions. White light images are used for feature extraction such as surface dirt and texture, while warm light reflectance images are specifically used to detect abnormal areas such as uneven reflection and localized discoloration on the egg surface. The synergistic effect of both improves the comprehensiveness and robustness of defect detection. Furthermore, the generation mechanism of parameters α and β is based on small sample statistics, enabling the system to maintain the stability and accuracy of the discrimination threshold when faced with minor changes in lighting and subtle differences in eggshell color within production batches, reducing the system's sensitivity to changes in the external environment. Attached Figure Description

[0034] Figure 1 Schematic diagram of the egg product defect detection and grading system of the present invention; Figure 2 A schematic diagram of the image acquisition module structure of the present invention; Figure 3 Flowchart of the egg product defect detection and grading method of the present invention; Figure 4 A schematic diagram of the eggshell color type recognition process of the present invention; Figure 5 A schematic diagram illustrating the dynamic adjustment of the dirtiness index discrimination threshold in this invention. Detailed Implementation

[0035] This invention provides a machine vision-based method for egg defect detection and grading, aiming to address a series of technical challenges in existing egg defect detection systems, such as insufficient adaptability and generalization ability when faced with variable eggshell colors, heavy reliance on large-scale labeled data, and high deployment costs. This method achieves high-precision, adaptive detection and refined grading of various egg surface defects by introducing an innovative automatic eggshell color recognition mechanism, a small-sample meta-learning parameter generation mechanism, and a dynamic adjustment mechanism for defect discrimination thresholds based on the generated parameters. The following detailed explanation of the method flow, working principles of each module, key parameter settings, and technical details, combined with specific embodiments, will ensure that those skilled in the art can fully understand and implement this invention.

[0036] In a preferred embodiment, the egg product defect detection and grading method based on machine vision of the present invention includes five main steps in its core workflow: multispectral image acquisition, eggshell color type recognition, small sample learning parameter generation, variety adaptive defect detection, and multi-dimensional grading decision-making.

[0037] Specifically, the method of this invention first acquires the surface visual information of the eggs to be inspected through a high-precision, multispectral image acquisition module. This image acquisition module ensures stable and efficient capture of high-quality images on an industrial inspection production line. The module integrates at least one industrial-grade area scan camera, a white light illumination unit, and a warm-color light illumination unit. The industrial-grade area scan camera uses a global shutter CMOS sensor to effectively avoid motion blur and ensure image clarity under high-speed production line operations. Its effective pixel resolution reaches 2448×2048 pixels, with a pixel size as fine as 3.45μm×3.45μm, and it has an image acquisition frame rate of no less than 100 frames per second (fps), ensuring continuous coverage of fast-moving eggs. The camera transmits data to the image processing unit at high speed and reliably via a standard Gigabit Ethernet interface, avoiding data bottlenecks. The industrial-grade area array camera is equipped with a low-distortion, fixed-focal-length industrial lens with an aperture range that can be precisely adjusted between F / 2.8 and F / 16. This allows the system to be flexibly configured according to ambient lighting conditions and depth-of-field requirements, enabling it to capture sharp and minimally distorted egg product images in various working environments.

[0038] The white light illumination unit consists of a ring-shaped array of high-brightness white LEDs. The color temperature of these LEDs is set to 5000K to simulate natural sunlight, ensuring accurate color reproduction of the acquired RGB images. The total luminous intensity of the LED array reaches 25,000 candela, powered by a high-efficiency constant-current LED driver and precisely controlled by a pulse-width modulation (PWM) signal, creating a highly uniform and shadow-free lighting environment on the surface of the eggs being inspected. The warm-colored light illumination unit also consists of a ring-shaped array of warm-colored LEDs, with a color temperature set to 3000K, a spectral center of approximately 620nm, a spectral half-width of ±20nm, and a luminous intensity of 18,000 candela. It is also powered by an independent constant-current LED driver and controlled by a PWM signal, ensuring the stability and controllability of its brightness output. The warm light is primarily used to enhance the visibility of differences in reflection and microscopic color variations on the eggshell surface, thereby aiding in the identification of localized reflection anomalies that are difficult to detect with the naked eye.

[0039] The white light illumination unit and the warm light illumination unit employ an alternating flash mode. This mode is controlled by a hardware trigger unit synchronized with the camera shutter at the millisecond level to ensure accurate acquisition of images with different spectra. In specific operation, within a complete inspection cycle of an egg, the industrial-grade area array camera first acquires the first frame image—the RGB image of the egg's surface—while the white light illumination unit is momentarily activated. Immediately afterward, the white light illumination unit quickly deactivates, the warm light illumination unit instantly activates, and the industrial-grade area array camera acquires the second frame image—the warm light reflection image of the egg. This dual-light, dual-image acquisition strategy allows the system to obtain egg surface information from complementary physical dimensions, improving the comprehensiveness and robustness of defect detection. All image data captured by the image acquisition module is transmitted in real-time to the subsequent image processing unit via the Gigabit Ethernet interface for rapid analysis. The conveyor mechanism is driven by a food-grade conveyor belt and integrates a high-resolution position encoder. This encoder detects the instantaneous position of each egg on the production line and sends a precise position signal to the hardware trigger unit. This close collaborative working mechanism ensures that each egg is triggered to acquire an image at the precise moment it arrives at the image acquisition station, thereby guaranteeing the consistency and accuracy of the detection.

[0040] Further, after acquiring the RGB image of the egg to be inspected, the image processing unit proceeds to the eggshell color type recognition step. This step aims to automatically and accurately determine the eggshell color type of the current egg to be inspected, providing crucial information for subsequent adaptive defect detection. The image processing unit first performs background segmentation on the received RGB image. Specifically, by applying the Otsu thresholding algorithm to the luminance (L) channel of the RGB image, the foreground (egg) can be effectively separated from the background (conveyor belt, support, etc.). To eliminate any small noise points or irregular edges that may occur during the segmentation process, morphological opening operations are then applied (e.g., using a 3x3 circular structuring element), and then connected component analysis is performed to select the connected region with the largest area as the main body region of the egg, thereby achieving accurate extraction of the egg's outline.

[0041] From the segmented main egg region, the system automatically extracts a central area as the Region of Interest (ROI) for color feature extraction. The size of the ROI is precisely set to 50% of the length of the minor axis of the largest circumscribed ellipse of the main egg region, centered on the geometric center of the egg. This ensures that the selected area fully represents the inherent color characteristics of the eggshell while avoiding the influence of uneven lighting or shadows at the eggshell edges. All RGB pixel values ​​within the ROI are precisely converted to the CIE Lab color space. The RGB to Lab color space conversion process strictly follows the International Commission on Illumination (CIE) D65 standard light source as the reference white point, converting RGB values ​​to the CIE XYZ space through a linear transformation matrix, and then further converting them to the Lab space through nonlinear transformations (including gamma correction and cube root function). This conversion ensures the consistency of color perception, allowing color differences to be quantified more uniformly.

[0042] In the Lab color space, the 'a' channel represents the color range from green to red, and its value can effectively distinguish the degree of redness in eggshells. Statistical analysis is performed on the 'a' channel values ​​of all pixels within the ROI region to construct a grayscale histogram of the 'a' values. Based on this grayscale histogram, the K-means clustering algorithm is used to cluster the 'a' channel values. Here, the K value is preset to 3 to correspond to the three common egg color types: red, white, and pink. The K-means algorithm uses a K-means++ initialization strategy to optimize the selection of initial cluster centers, thereby improving the stability and convergence speed of the clustering results. The iterative convergence condition is set so that the cluster center position moves less than 0.001 units of 'a' value in five consecutive iterations to ensure clustering accuracy. After clustering is completed, the system automatically determines the eggshell color type of the current egg based on the specific numerical range of each cluster center on the 'a' axis.

[0043] The specific criteria are as follows: If the cluster center value of cluster a falls within the range [15, 30], it is determined to be a red shell; If the cluster center value of cluster a falls within the range [-5, 5], it is determined to be a white shell; If the cluster center value of cluster a falls within the range [5,15], it is determined to be a pink shell.

[0044] The judgment result will be assigned as a color label to the current egg product to be inspected and stored in the egg product information database in real time for subsequent defect detection and grading decisions, so as to achieve adaptive processing for different eggshell colors.

[0045] In the egg product information database, the system maintains a list of known eggshell color types and their corresponding defect discrimination parameter library. When the current eggshell color type (i.e., the color label) determined in step S2 does not match any item in the list of known color types, the system has identified a newly appearing eggshell color type. At this time, the system will automatically trigger the small sample meta-learning parameter generation process. Specifically, 5 to 10 eggs with the newly appearing color label are automatically selected from the current batch. These selected eggs need to undergo preliminary screening by a preset general coarse defect detection model to ensure that they are samples with "no visible defects," thereby ensuring the quality of the support set. The general coarse defect detection model is built on a convolutional neural network (CNN) extensively trained on multiple eggshell colors. Its main function is to quickly identify obvious dirt or cracks with low accuracy, thereby excluding samples with significant defects from the support set.

[0046] Five to ten RGB images of eggs with "no visible defects" form a support set, which is input into a pre-trained meta-learner. The meta-learner employs a Prototypical Networks architecture, with its core consisting of a deep encoder and a distance metric module. The deep encoder is a modified version of the ResNet-18 model, removing the top classification layer to focus on extracting general high-dimensional features from the images. The deep encoder has been meta-trained offline on a large-scale, multi-domain egg dataset to learn a metric space where eggs of the same category (e.g., "normal") are close in the feature space, while eggs of different categories are far apart, and it possesses the ability to quickly adapt to new categories.

[0047] When the support set images are input into the deep encoder of the meta-learner, the system extracts a high-dimensional feature vector for each image. Then, the feature vectors of all images in the support set are averaged to obtain the "normal egg prototype" feature vector for the first appearance of this eggshell color type. Based on the "normal egg prototype" feature vector, the system further calculates the visual feature normalization parameters (α, β) for this color type of egg. Specifically, parameter α is defined as the arithmetic mean (μ) of the dirtiness index of all "no visible defects" eggs in the support set, while parameter β is defined as the standard deviation (σ) of the dirtiness index of all "no visible defects" eggs in the support set. The calculation method for the dirtiness index will be detailed in subsequent step S4.2. The α and β parameters are considered as statistical representations of the normal appearance characteristics of this new color type of egg, effectively characterizing the central tendency and dispersion of the dirtiness index of this color egg in the "no defects" state. The α and β parameters are then stored in the parameter library in real time and associated with the new color label. If the color type corresponding to the color label already exists in the list of known color types, the system directly loads the pre-stored parameters α and β associated with the color label from the parameter library, without having to execute the small sample meta-learning parameter generation process again, thereby greatly improving the detection efficiency.

[0048] After that, the system enters the core step of variety adaptive defect detection, which performs surface anomaly detection and dirt level assessment in parallel, and dynamically adjusts the discrimination threshold based on the eggshell color.

[0049] In the identification of surface anomaly regions (S4.1), based on the warm-colored light reflection image obtained in step S1, the image processing unit first performs a series of preprocessing operations. Specifically, Gaussian filtering is applied, for example, using a 5x5 kernel with a standard deviation of 1.0, to smooth the image and effectively remove random noise introduced during image acquisition, while retaining important edge information. Subsequently, an adaptive threshold segmentation algorithm (e.g., Niblack or Sauvola algorithm) is used, with the local window size set to 15x15 pixels, to identify reflection anomalies or local color difference regions in the image. The adaptive threshold algorithm can dynamically adjust the threshold according to local image characteristics, effectively addressing local illumination differences caused by factors such as eggshell curvature and uneven light reflection, thereby accurately separating dark spots. After segmentation, morphological closing operations are applied, for example, using 3x3 circular structuring elements, to connect the segmented small fracture areas and fill tiny holes, making the dark spot region more complete. Furthermore, through connected component analysis, the system identifies independent surface anomaly regions. For each identified connected component, the system accurately calculates its pixel area, perimeter, equivalent diameter, and average gray value. If the total pixel area of ​​the abnormal surface region exceeds a preset threshold (e.g., 50 pixels), or the equivalent diameter of any single dark spot exceeds a preset threshold (e.g., 3 mm), or its average gray value is lower than the average gray value of the surrounding local background by a preset percentage (e.g., 15%), then the egg product is determined to have a surface abnormality. These quantitative indicators comprehensively consider the size, shape, and contrast with the background of the dark spot, ensuring high-precision dark spot detection.

[0050] Regarding the calculation of the dirtiness index (S4.2), based on the RGB image obtained in step S1, the system first converts it into a grayscale image. This conversion strictly follows a weighted average method, i.e. The weighting coefficient reflects the differences in human eye sensitivity to the three primary colors of red, green, and blue, enabling the converted grayscale image to better represent the brightness perceived by human vision. Subsequently, the local grayscale standard deviation and texture entropy are calculated on the grayscale image, which together constitute the core components of the dirtiness index.

[0051] The local grayscale standard deviation is calculated by taking each pixel in the image and calculating the standard deviation of all pixel grayscale values ​​within a local window (e.g., a 7x7 pixel square kernel) centered on that pixel. The formula for calculating the standard deviation is: ,in, The pixel grayscale value within a local window. It is the average value of the pixel grayscale values ​​within the local window. This is the number of pixels within a local window. This metric accurately reflects the degree of grayscale change in a local area, i.e., the richness of image details or texture, and is highly sensitive to surface anomalies such as dirt and cracks.

[0052] The texture entropy is calculated using the Gray-Level Co-occurrence Matrix (GLCM) method to quantize the local texture complexity and disorder of the image. For the grayscale image, a 32x32 GLCM is constructed by setting the gray levels to 32 levels and calculating the GLCM at four directions: 0°, 45°, 90°, and 135°, with a pixel distance of 1. Then, the GLCMs in these four directions are averaged to obtain a comprehensive GLCM, thus obtaining orientation-insensitive texture features. Based on this comprehensive GLCM, the texture entropy of the image is calculated using the following formula: ,in, is the normalized frequency value at position (i,j) in GLCM, representing the probability of pixel pair (i,j) occurring. Higher texture entropy indicates greater complexity and disorder in the local texture of the image, which is typically associated with dirt, roughness, or abnormal textures on the surface of eggs.

[0053] The local grayscale standard deviation map and the texture entropy map are each normalized, for example, by using a linear mapping method to uniformly map their pixel values ​​to between 0 and 1. Then, a weighted sum is performed to obtain the final dirtiness index. The weighting coefficients for the weighted summation are optimized based on extensive experimental data and experience. For example, the dirtiness index is calculated as: Dirtiness Index = 0.6 × (Normalized Local Grayscale Standard Deviation) + 0.4 × (Normalized Texture Entropy). The dirtiness index ranges from 0 to 1; a higher value indicates a higher degree of dirtiness or texture abnormalities on the egg surface.

[0054] Regarding the dynamic adjustment of the dirtiness index discrimination threshold (S4.3), this invention utilizes the parameters α and β obtained in step S3 for the current egg color label to dynamically adjust the discrimination threshold of the dirtiness index. Specifically, for any color type of egg, its dirtiness index discrimination threshold (hereinafter referred to as "dirtiness threshold") is dynamically determined according to the following formula: Where α and β are the mean (μ) and standard deviation (σ) of the current egg color type calculated or loaded in step S3, respectively, and K is a preset sensitivity coefficient. The value of K is determined through offline experiments, for example, set to 2.5, which represents 2.5 standard deviations outward from the average dirtiness level of normal eggs, serving as the boundary for distinguishing between normal and defective products. This setting statistically typically covers the vast majority of normal samples, thus effectively distinguishing abnormal dirtiness.

[0055] The parameters α and β described in this invention, as statistical quantities describing the distribution of normal surface features of eggs of specific colors, enable the dirt threshold to be adaptively adjusted according to the inherent optical properties and texture differences of different eggshell colors. Specifically, for a known red-shelled egg, the corresponding α and β parameters, after being substituted into the above formula and combined with K=2.5, result in a dirt threshold of 0.5. For a white-shelled egg, the corresponding α and β parameters, after being substituted into the above formula, result in a dirt threshold of 0.3. For a pink-shelled egg, the corresponding α and β parameters, after being substituted into the above formula, result in a dirt threshold of 0.4. These specific values ​​reflect the differences in image feature representation caused by the differences in the depth of the base color and the coarseness of the texture of different eggshell colors, resulting in the same degree of dirtiness. For example, red-shelled eggs, due to their darker base color, may have higher local grayscale standard deviation and texture entropy due to their texture and pigment deposition. This results in lower contrast for minor dirt, so the dirt threshold for red-shelled eggs is set higher than that for white-shelled and pink-shelled eggs. This avoids misjudging normal dark textures or slight uneven lighting as dirt. This dynamic adjustment mechanism can adaptively process different eggshell colors without manual intervention or retraining, significantly improving the system's robustness and generalization ability.

[0056] Finally, the present invention proceeds to the multi-dimensional hierarchical decision-making step. Based on the surface anomaly detection results obtained in step S4.1 and the dirtiness index and dynamically adjusted dirtiness threshold obtained in steps S4.2 and S4.3, the system performs a multi-dimensional hierarchical determination. The hierarchical decision-making logic is as follows: If the dirtiness index of the current egg product is less than or equal to its corresponding dynamically adjusted dirtiness threshold, and no surface abnormalities are detected in step S4.1 (i.e., the total pixel area of ​​the abnormal area is less than 50 pixels and no single abnormal area has an equivalent diameter exceeding 3 mm), then the egg product is determined to be Grade A. Grade A eggs represent high-quality eggs with clean surfaces and no obvious defects, meeting the highest market standards.

[0057] If the dirtiness index of the current egg product is greater than its corresponding dynamically adjusted dirtiness threshold, but less than or equal to 0.7, and no surface abnormalities are detected in step S4.1, then the egg product is determined to be Grade B. Grade B eggs represent qualified eggs with slight surface dirt but no obvious structural defects, and can be circulated in the market under certain conditions.

[0058] If any surface abnormality is found in the egg product (regardless of size or grayscale deviation, i.e., the detection result in step S4.1 shows the presence of dark spots) or its dirtiness index is greater than 0.7, the egg product is classified as Grade C or directly rejected. Grade C or rejected eggs represent defective eggs with significant surface dirt that may affect food safety or seriously affect appearance, requiring further processing or disposal.

[0059] The grading decision results are sent to the sorting execution mechanism on the production line via standard digital signals (such as Modbus TCP / IP protocol or EtherCAT protocol). This mechanism can be a PLC-controlled robotic arm or a pneumatic sorting device to physically sort the eggs, guiding eggs of different grades to different collection paths. Simultaneously, all detection data, including grading results, eggshell color labels, dirtiness index, and surface anomaly detection details (including location, size, grayscale value, etc.), are stored in real-time in a central database for production batch tracking, product quality traceability, and subsequent data analysis and system optimization, thus forming a closed-loop quality management system.

[0060] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.

Claims

1. A method for machine vision based egg defect detection and grading, characterized in that, Comprising the following steps: S1. Multi-spectral image acquisition: on an industrial detection pipeline, the egg to be detected is sent to the image acquisition station by the conveying mechanism, and the image acquisition module acquires the surface RGB image and the warm light reflection image of the egg to be detected under the condition that the white light illumination unit and the warm light illumination unit alternately flash, and transmits the image data to the image processing unit; S2. Eggshell color type identification: the image processing unit performs background segmentation on the RGB image to obtain the egg body region, extracts the region of interest from the egg body region for color feature extraction, and converts the RGB pixel value of the region of interest to the CIELab color space, performs K-means clustering analysis on the a channel value in the Lab color space, automatically determines the eggshell color type of the egg to be detected according to the clustering result, and generates a color label of the eggshell color type; S3. Small sample meta-learning parameter generation: if the color label identifies a first appearing eggshell color type, a preset number of "no visible defects" eggs to be detected are selected from the current batch as a support set, the support set image is input into a pre-trained meta-learner, the meta-learner generates a "normal egg prototype" feature vector of the color type, and calculates visual feature normalization parameters a and β of the color type based on the feature vector, the parameters a and β are stored and associated with the color label; if the color label corresponds to an existing color type, the pre-stored parameters a and β are directly loaded; S4. Variety adaptive defect detection: surface abnormal region identification, dirtiness index calculation and dirtiness index discrimination threshold dynamic adjustment are performed in parallel; S4.1 Dark spot region identification: based on the warm light reflection image, local color difference or reflection abnormal region in the warm light reflection image is identified through Gaussian filtering, adaptive threshold segmentation, morphological closing operation and connected domain analysis, and whether the egg to be detected has surface abnormalities is judged according to the characteristics of the region; S4.2 Dirtiness index calculation: based on the RGB image, the RGB image is converted into a gray image, the local gray standard deviation and texture entropy of the gray image are calculated, and weighted sum is performed to obtain the dirtiness index of the egg to be detected; S4.3 The dirty index discriminant threshold dynamic adjustment: using the parameters α and β obtained in step S3 for the current egg color label, according to the formula: dynamically determine the discriminant threshold of the dirty index, wherein K is a preset sensitivity coefficient; S5. Multi-dimensional grading decision: according to the surface abnormality identification result of step S4.1, the dirtiness index of step S4.2 and the dynamically adjusted dirtiness threshold of step S4.3, multi-dimensional grading judgment is performed on the egg to be detected, and the grading result is output to the sorting execution mechanism.

2. The egg defect detection and grading method based on machine vision according to claim 1, wherein: In the S1 multi-spectral image acquisition step, the image acquisition module comprises one or more industrial-grade area array cameras, one white light illumination unit and one warm light illumination unit; The industrial-grade area array camera adopts a global shutter CMOS sensor, has an effective pixel resolution of no less than 2448x2048 pixels, a pixel size of no more than 3.45μm x 3.45μm, an image acquisition frame rate of at least 100fps, and transmits data to the image processing unit through a GigabitEthernet interface; The white light illumination unit is composed of an array of high-brightness white light LEDs arranged in a ring, with a color temperature of 5000K and a luminous intensity of 25000 candela, and the brightness thereof is precisely controlled by a pulse width modulation (PWM) signal; The warm light illumination unit is composed of an array of warm color LEDs arranged in a ring, with a color temperature of 3000K, a spectral center of about 620nm, a spectral half-width of ±20nm, and a luminous intensity of 18000 candela, and the brightness thereof is also controlled by a PWM signal; The white light illumination unit and the warm light illumination unit adopt an alternate flashing mode, and are precisely time-controlled by a hardware trigger unit synchronized with the shutter of the industrial-grade area array camera, so as to sequentially acquire the RGB image and the warm light reflection image within one detection cycle of an egg product.

3. The machine vision based egg defect detection and grading method as claimed in claim 1 wherein, In the S2 eggshell color type identification step: The image processing unit first applies an Otsu threshold segmentation algorithm on the brightness channel of the RGB image, eliminates small noise points by morphological opening operation, and selects the largest area as the egg main region by connected component analysis, so as to separate the egg from the background; From the egg main region, a central region is extracted as the region of interest, the size of the region of interest is 50% of the short axis length of the largest circumscribed ellipse of the egg main region, and the geometric center of the egg is taken as the center; The RGB pixel values of the region of interest are converted to the CIELab color space, and the conversion follows the international commission on illumination standard with D65 standard light source as the reference white point; The a channel values of all pixel points in the region of interest are statistically analyzed to construct a gray histogram of a values.

4. The machine vision-based egg defect detection and grading method according to claim 3, wherein: In the S2 eggshell color type identification step, based on the gray histogram, a K-means clustering algorithm is used for clustering analysis of the a channel values, wherein K is set to the preset number of egg color types, and a K-means++ initialization strategy is used to optimize the selection of cluster centers, and the iterative convergence condition is set to a distance of less than 0.001 unit a value moved by the cluster center position in five consecutive iterations; After clustering is completed, the specific numerical range of each cluster center on the a axis is used to determine the eggshell color type of the current egg product, and the specific identification criteria are as follows: If the a cluster center value falls within the range [15, 30], it is determined to be red shell; If the a cluster center value falls within the range [-5, 5], it is determined to be white shell; If the a cluster center value falls within the range [5, 15], it is determined to be pink shell.

5. The machine vision based egg defect detection and grading method as claimed in claim 1 wherein, In the S3 small sample meta-learning parameter generation step: When the current eggshell color type determined by the S2 does not match any of the known eggshell color types in the egg product information database, a first-occurrence eggshell color type is identified, and the system triggers the small sample meta-learning parameter generation process; The pre-set number of "no visible defects" eggs to be inspected is 5 to 10, and the egg products need to be preliminarily screened as "no visible defects" samples by a pre-set general coarse defect detection model to ensure the quality of the support set, and the general coarse defect detection model is based on a convolutional neural network widely trained on multiple eggshell colors.

6. The machine vision-based egg defect detection and grading method according to claim 5, characterized in that, In the S3 small sample meta-learning parameter generation step: The pre-trained meta-learner adopts a prototype network architecture, the core of which consists of a deep encoder and a distance metric module, the deep encoder is a modified version based on the ResNet-18 model, and the top classification layer is removed, and the meta-learner has been meta-trained on a large-scale, multi-domain egg product dataset in the offline stage; When the support set image is input into the deep encoder of the meta-learner, a high-dimensional feature vector is extracted for each image, and the feature vectors of all images in the support set are averaged to obtain the "normal egg product prototype" feature vector of the first-occurrence eggshell color type; The parameter α is defined as the arithmetic mean of the dirtiness indexes of all "no visible defects" egg products in the support set, and the parameter β is defined as the standard deviation of the dirtiness indexes of all "no visible defects" egg products in the support set.

7. The machine vision-based egg defect detection and grading method according to claim 1, characterized in that, In the S4.1 dark spot area identification step: The image processing unit first applies Gaussian filtering to the warm light reflection image, the Gaussian filtering uses a 5x5 kernel and a Gaussian kernel with a standard deviation of 1.0; Through an adaptive threshold segmentation algorithm, the adaptive threshold segmentation algorithm is Niblack or Sauvola algorithm, and the local window size is set to 15x15 pixels, to identify the surface reflection abnormalities or color difference areas in the image; After segmentation, morphological closing operation is applied, the morphological closing operation uses a 3x3 circular structural element to connect the segmented small broken areas and fill small holes, and then identifies independent abnormal areas through connected component analysis; For each identified connected domain, calculate its pixel area, perimeter, equivalent diameter, and average gray value, if the total pixel area of the abnormal area exceeds 50 pixels, or the equivalent diameter of any area exceeds 3 millimeters, or the average gray value is lower than the average gray value of the surrounding local background by 15%, it is determined that the egg to be inspected has surface abnormalities.

8. The method of claim 1, wherein, In the S4.2 dirtiness index calculation step: The conversion of the RGB image into a grayscale image follows the weighted average method: ; The local gray scale standard deviation is calculated by calculating the standard deviation of all pixel gray scale values in a local window centered on the pixel in the gray scale image, the local window being a square kernel of 7x7 pixels, the standard deviation calculation formula being: wherein, is the pixel gray scale value in the local window, is the average value of the pixel gray scale values in the local window, is the number of pixels in the local window.

9. The method of claim 8, wherein, In the S4.2 dirtiness index calculation step: The calculation of the texture entropy adopts the gray level co-occurrence matrix method, for the gray image, a gray level co-occurrence matrix with a size of 32x32 is constructed, the gray level is quantized to 32 levels, and the calculation is performed in four directions of 0 degrees, 45 degrees, 90 degrees, and 135 degrees with a pixel distance of 1, and the GLCM in the four directions is averaged to obtain a comprehensive GLCM; Based on the comprehensive GLCM, the texture entropy of the image is calculated, and the formula is: wherein, is the normalized frequency value of the (i, j) position in the GLCM. The local gray scale standard deviation map and the texture entropy map are weighted and summed after being normalized respectively to obtain a final dirtiness index, and a calculation formula of the dirtiness index is: dirtiness index=0.6X(normalized local gray scale standard deviation)+0.4X(normalized texture entropy).

10. The method of claim 1, wherein, In the S4.3 step of dynamically adjusting the dirtiness index threshold, the K value is set to 2.5; In the S5 step of multi-dimension hierarchical decision, the hierarchical decision logic is as follows: If the dirtiness index of the current egg product is less than or equal to the corresponding dynamically adjusted dirtiness threshold, and no surface abnormal area is detected in the S4.1 step (i.e. the total pixel area of the abnormal area is less than 50 pixels and no single abnormal area has an equivalent diameter exceeding 3 mm), the egg product is determined as A grade; If the dirtiness index of the current egg product is greater than the corresponding dynamically adjusted dirtiness threshold but less than or equal to 0.7, and no abnormal area is detected in the S4.1 step, the egg product is determined as B grade; If the current egg product has any form of surface abnormality or the dirtiness index is greater than 0.7, the egg product is determined as C grade or a direct rejection operation is performed.