A method and system for visual inspection of packaging defects
By combining multispectral imaging with polarization decomposition, multi-scale feature enhancement, and deep learning models, the problems of high false detection rate and high false negative rate in visual inspection of packaging parts are solved, and high-precision defect detection is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- JIANGSU HONGMENG PACKAGING CO LTD
- Filing Date
- 2026-05-25
- Publication Date
- 2026-07-14
AI Technical Summary
In existing visual inspection methods for packaging, label or reflective interference leads to a high false detection rate, and the weak features of minor defects lead to a high missed detection rate. Furthermore, the independent operation of each link makes it difficult for the overall inspection performance to meet the stringent requirements of high-end packaging.
Multispectral imaging technology is used to separate the packaging body and the label area. Polarization decomposition is used to remove reflective interference. Multi-scale Top-Hat transformation and adaptive threshold segmentation are combined to extract candidate defect regions. A deep learning model combining spatial-channel attention is constructed for defect identification, and the detection results are optimized by multi-frame fusion.
It enables accurate detection of packaging defects, reduces false detection and false negative rates, and improves the accuracy and stability of detection.
Smart Images

Figure CN122391192A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of machine vision inspection technology, specifically to a method and system for visual inspection of defects in packaging. Background Technology
[0002] As the core packaging carrier in the food and pharmaceutical industries, the surface defects of packaging directly affect product safety and market acceptance, thus requiring full-scale quality control through visual inspection. Existing inspection methods suffer from two major problems: First, false detections due to interference are severe. Light reflections on the packaging surface easily create bright areas, which can be confused with minute defect features. Simultaneously, edge textures of labels or printed patterns in non-label areas are often misjudged as scratches, leading to a high false detection rate. Second, the rate of missed detection for minute defects is high. Defects such as scratches with a diameter less than 0.5mm have low grayscale contrast, making them difficult to effectively capture using traditional fixed-threshold segmentation and simple feature extraction methods, resulting in a high missed detection rate. More importantly, existing technologies operate independently at each stage. Preprocessing does not specifically optimize the input quality for subsequent feature extraction, and the classification model does not fully utilize the results of previous feature enhancements, causing the overall detection performance to fail to meet the stringent requirements of high-end packaging. Summary of the Invention
[0003] To address the aforementioned technical shortcomings, this invention provides a visual inspection method and system for packaging defects, thereby solving the problems of high false detection rates caused by label or reflective interference and high missed detection rates caused by weak features of minute defects in the prior art.
[0004] This invention is achieved through the following technical solution: A method for visual inspection of defects in packaging is provided, the method comprising the following steps: Step S10: Acquire multispectral images of the packaging at three viewing angles: 0°, 45°, and 90°. Separate the packaging body and packaging label area based on spectral differences. Remove reflective interference through polarization decomposition and output an interference-free packaging defect feature image that retains spectral characteristics, providing clean input for subsequent feature extraction. Step S20: Extract candidate defect regions based on feature enhancement. Perform multi-scale Top-Hat transformation on the package defect feature image output in step S10 to obtain the enhanced defect feature image. Combine adaptive threshold segmentation to extract candidate defect regions and output the coordinate information of candidate regions through morphological filtering. Step S30: Construct a packaging defect recognition model that combines spatial-channel attention. Input the interference-free packaging defect feature image obtained in step S10 and the defect candidate region extracted in step S20 into the model. The model outputs the packaging defect classification result with confidence. Step S40: Based on the candidate region coordinate information output in step S20, obtain the spatial location correlation, and combine it with the confidence level output in step S30 to filter duplicate annotations and false detections, and output the final packaging defect detection result.
[0005] Preferably, the multispectral image in step S10 includes a visible light image, a near-infrared image, and a polarized image. The label area is masked based on the grayscale threshold of the near-infrared image. The threshold is dynamically determined by the difference between the visible light image and the near-infrared image. The de-reflection processing adopts the specular reflection component removal algorithm of the polarized image. The grayscale standard deviation of the processed image is reduced by more than 30%.
[0006] Preferably, the steps in step S10 include: Multispectral acquisition: A 3-channel camera is used to simultaneously acquire visible light images, near-infrared images, and polarization images, providing multi-dimensional data for subsequent steps; Tag masking: Analyze the differences between the tagged and non-tagged areas in the near-infrared image, where the gray value of the tagged area is <50 and the gray value of the non-tagged area is >150. Combine the visible light image to calibrate the tag edge and generate a tag mask to mark the non-detection area and avoid tag interference in subsequent processes. De-reflection processing: The polarized image is decomposed into reflection components, the specular reflection component (i.e., the reflection component) is removed, and the diffuse reflection component is retained, which contains the real defect features of the packaging. The processed polarized image is used as the input for step S20 to ensure that the subsequent feature extraction focuses on the real defects.
[0007] Preferably, the step of extracting candidate defect regions based on feature enhancement in step S20 includes: Multi-scale enhancement: For the feature distribution of minute defects in the interference-free packaging defect feature image output in step S10, Top-Hat transformation of 3×3, 5×5 and 7×7 structuring elements is used. 3×3 is used for fine scratch defects, 5×5 is used for small wrinkle defects and 7×7 is used for slightly larger defects. This enhances the grayscale contrast between packaging defects and image background, improving the contrast by 2-3 times. Adaptive segmentation: Based on the local gray mean μ and standard deviation σ of the image after removing reflective interference through polarization decomposition in step S10, the segmentation threshold T=μ-0.6σ is dynamically set. The threshold parameter is optimized based on the noise level of the preprocessed image to segment out potential defect areas. Morphological screening: The segmentation results are subjected to erosion and dilation. Erosion refers to removing noise that was not completely eliminated in step S10, and dilation refers to restoring the defect morphology. Regions with an area > 0.005 mm² are extracted as candidate regions, and candidate regions with coordinate information are output.
[0008] Preferably, step S30, which involves constructing a packaging defect identification model incorporating spatial-channel attention and outputting a packaging defect classification result with confidence, includes: Model Construction: Based on ResNet50, a spatial attention module and a channel attention module are embedded. The spatial attention module is used for the enhanced defect feature image in step S20, and the channel attention module is used for weight allocation of the preprocessed visible light image, near-infrared image and polarization image in step S10. The weights of the spectral channels that are more critical to defect identification are automatically strengthened, so as to achieve accurate utilization of the spectral features of the image in step S10. Sample association: Construct training samples, including the interference-free packaging defect feature image with preserved spectral features output after preprocessing in step S10 and the defect candidate region extracted in step S20. The defect candidate region extracted in step S20 contains the enhanced defect features, enabling the model to learn the complete defect representation from the preprocessed features to the enhanced features. Classification Reasoning: The model's output layer classifies the input defect candidate regions, outputting defect categories such as scratches and wrinkles or pseudo-defects. The Softmax activation function is used. The output layer contains neurons with an equal number of defect categories. Each neuron corresponds to a probability output for a defect category. The sum of the probability outputs for all defect categories is 1. For the input defect candidate regions, the model calculates the probability value of each defect category through forward propagation. The category with the highest probability is the detection category of the region, and the corresponding probability value is the confidence level of the classification result. Regions with a confidence level > 0.8 are marked as suspected defects, and the result is passed to step S40.
[0009] Preferably, the step of outputting the final packaging defect detection result in step S40 includes: Summary of detection results: Collect all detection results of the same package after processing in steps S10 to S30 at three viewing angles of 0°, 45° and 90°, including the spatial coordinate information, classification category and classification confidence of each defect candidate area, to form a multi-frame candidate area set; Duplicate annotation merging based on spatial correlation: For the aggregated candidate defect regions, duplicate annotations are filtered by calculating the spatial overlap. For any two candidate regions, the intersection-union ratio (IoU) of their bounding boxes is calculated, and a threshold is set. The threshold is dynamically set based on the average size of the candidate regions in step S20. When the IoU is greater than the set threshold, it is determined to be a duplicate annotation of the same defect in different frames. For regions with duplicate annotations, the annotation result with the highest classification confidence is retained, and the remaining redundant annotations are removed to avoid the same defect being counted multiple times. Rule-based low-confidence false detection filtering includes label region filtering, size-confidence dual filtering, and multi-frame consistency verification. Label region filtering uses the label mask generated in step S10 to remove candidate regions located within the label mask area. Even if the confidence level is high, it is still judged as label interference. Size-confidence dual filtering refers to filtering small regions with an area ≤0.005mm² and a confidence level ≤0.8. The size threshold is based on the morphological screening in step S20, and the confidence threshold is based on the model's verification accuracy setting in step S30. Multi-frame consistency verification refers to retaining candidate regions that appear repeatedly in ≥2 frames of images, i.e., the features of the same defect under different viewpoints, and removing low-confidence regions that appear only in a single frame, which are usually noise. Final output: After merging and filtering in the above steps, the remaining defect categories, precise spatial locations (boundary box coordinates), size parameters (area, length / diameter) and final confidence level (the highest confidence value retained) are output, resulting in the final detection results and generating a complete defect detection report.
[0010] Furthermore, to achieve the above objectives, the present invention also proposes a visual inspection system for packaging defects, the visual inspection system for packaging defects comprising: Spectral image acquisition and image preprocessing module: used to acquire multispectral images of the packaging at three viewing angles of 0°, 45° and 90°, separate the packaging body and packaging label area based on spectral differences, remove reflective interference through polarization decomposition, and output an interference-free packaging defect feature image that retains spectral characteristics, providing a clean input for subsequent feature extraction; Defect Candidate Region Extraction Module: This module is used to extract defect candidate regions based on feature enhancement. It performs multi-scale Top-Hat transformation on the packaging defect feature image output from the spectral image acquisition and image preprocessing module to obtain an enhanced defect feature image. It then combines adaptive threshold segmentation to extract defect candidate regions and outputs the coordinate information of the candidate regions through morphological screening. Attention Mechanism Classification and Recognition Module: Used to build a packaging defect recognition model that combines spatial-channel attention. The model inputs the interference-free packaging defect feature image obtained from the spectral image acquisition and image preprocessing module and the defect candidate region extracted from the defect candidate region extraction module. The model outputs packaging defect classification results with confidence. Packaging defect detection output module: It is used to obtain the spatial location correlation based on the candidate region coordinate information output by the defect candidate region extraction module, and filter duplicate annotations and false detections by combining the confidence score output by the attention mechanism classification and recognition module, and output the final packaging defect detection result.
[0011] Furthermore, to achieve the above objectives, the present invention also proposes a visual inspection device for packaging defects. The device includes: a memory, a processor, and programs such as a visual inspection algorithm for packaging defects stored in the memory and executable on the processor. The visual inspection algorithm for packaging defects is a set of steps for implementing the visual inspection method for packaging defects as described above.
[0012] In addition, to achieve the above objectives, the present invention also provides a computer program product, which includes programs such as a visual detection algorithm for packaging defects. When the visual detection algorithm for packaging defects is executed by a processor, it implements a visual detection method for packaging defects as described above.
[0013] The advantages and effects of this invention are: This invention proposes a visual inspection method and system for packaging defects. Through a progressive process of multispectral imaging and interference suppression, enhanced feature-guided candidate region extraction, deep learning classification verification, and multi-frame fusion optimization, it achieves accurate defect detection. At the same time, each step is closely linked through data flow and feature transfer. Pre-processing provides high-quality input for subsequent steps, and subsequent steps deepen the analysis based on the results of the pre-processing, thereby improving the overall detection accuracy and stability. Attached Figure Description
[0014] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0015] Figure 1 This is a flowchart of a visual inspection method for packaging defects according to the present invention.
[0016] Figure 2 This is a schematic diagram of the structure of a visual inspection system for packaging defects according to the present invention. Detailed Implementation
[0017] 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.
[0018] like Figure 1 As shown, in one embodiment of the present invention, a visual inspection method for packaging defects includes the following steps: Step S10: Acquire multispectral images of the packaging at three viewing angles: 0°, 45°, and 90°. Separate the packaging body and packaging label area based on spectral differences. Remove reflective interference through polarization decomposition and output an interference-free packaging defect feature image that retains spectral characteristics, providing clean input for subsequent feature extraction.
[0019] Specifically, the multispectral images in step S10 include visible light images (450-650nm), near-infrared images (900-1000nm), and polarization images (550nm±20nm). The label area masking is based on the grayscale threshold of the near-infrared image. The threshold is dynamically determined by the difference between the visible light image and the near-infrared image. The de-reflection processing adopts the specular reflection component removal algorithm of the polarization image. The grayscale standard deviation of the processed image is reduced by more than 30%.
[0020] Specifically, the steps in step S10 include: Multispectral acquisition: A 3-channel camera is used to simultaneously acquire visible light images (preserving defect details), near-infrared images (significant differences between labeled and unlabeled areas), and polarization images (distinguishing between reflections and defects), providing multi-dimensional data for subsequent steps; Tag masking: Analyze the differences between the tagged and non-tagged areas in the near-infrared image, where the gray value of the tagged area is <50 and the gray value of the non-tagged area is >150. Combine the visible light image to calibrate the tag edge and generate a tag mask to mark the non-detection area and avoid tag interference in subsequent processes. De-reflection processing: The polarized image is decomposed into reflection components, the specular reflection component (i.e., the reflection component) is removed, and the diffuse reflection component is retained, which contains the real defect features of the packaging. The processed polarized image is used as the input for step S20 to ensure that the subsequent feature extraction focuses on the real defects.
[0021] Step S20: Extract candidate defect regions based on feature enhancement. Perform multi-scale Top-Hat transformation on the package defect feature image output in step S10 to obtain the enhanced defect feature image. Combine adaptive threshold segmentation to extract candidate defect regions and output the coordinate information of candidate regions through morphological screening.
[0022] Specifically, step S20, which involves extracting candidate defect regions based on feature enhancement, includes: Multi-scale enhancement: For the feature distribution of small defects (0.2-2mm) in the interference-free packaging defect feature image output in step S10, Top-Hat transformation of 3×3, 5×5 and 7×7 structuring elements is adopted, where 3×3 is used for fine scratch defects, 5×5 is used for small wrinkle defects and 7×7 is used for slightly larger defects, to enhance the grayscale contrast between packaging defects and image background, with a contrast improvement of 2-3 times; Adaptive segmentation: Based on the local gray mean μ and standard deviation σ of the image after removing reflective interference through polarization decomposition in step S10, the segmentation threshold T=μ-0.6σ is dynamically set. The threshold parameter is optimized based on the noise level of the preprocessed image to segment out potential defect areas. Morphological screening: The segmentation results are subjected to erosion and dilation. Erosion refers to removing noise that was not completely eliminated in step S10, and dilation refers to restoring the defect morphology. Regions with an area > 0.005 mm² are extracted as candidate regions, and candidate regions with coordinate information are output.
[0023] Step S30: Construct a packaging defect recognition model that combines spatial-channel attention. Input the interference-free packaging defect feature image obtained in step S10 and the defect candidate region extracted in step S20 into the model. The model outputs the packaging defect classification result with confidence.
[0024] Specifically, step S30, which involves constructing a packaging defect identification model that combines spatial-channel attention and outputting packaging defect classification results with confidence scores, includes the following steps: Model Construction: Based on ResNet50, a spatial attention module and a channel attention module are embedded. The spatial attention module is used for the enhanced defect feature image in step S20, and the channel attention module is used for weight allocation of the preprocessed visible light image, near-infrared image and polarization image in step S10. The weights of the spectral channels that are more critical to defect identification are automatically strengthened, so as to achieve accurate utilization of the spectral features of the image in step S10. Sample association: Construct training samples, including the interference-free packaging defect feature image with preserved spectral features output after preprocessing in step S10 and the defect candidate region extracted in step S20. The defect candidate region extracted in step S20 contains the enhanced defect features, enabling the model to learn the complete defect representation from the preprocessed features to the enhanced features. Classification Reasoning: The model's output layer classifies the input defect candidate regions, outputting defect categories such as scratches and wrinkles, or pseudo-defects. A Softmax activation function is used. The output layer contains neurons with an equal number of defect categories, each neuron corresponding to a probability output for one defect category. The sum of the probability outputs for all defect categories is 1. For the input defect candidate regions, the model calculates the probability value of each defect category through forward propagation. The category with the highest probability is the detection category for that region, and the corresponding probability value is the confidence level of the classification result. For example, when the probability of a candidate region being predicted as a minor scratch is 0.62, which is higher than the probabilities of other categories, such as a pseudo-defect with a probability of 0.05, the output classification result is minor scratch with a confidence level of 0.62. Regions with a confidence level > 0.8 are marked as suspected defects, and the result is passed to step S40.
[0025] Step S40: Based on the candidate region coordinate information output in step S20, obtain the spatial location correlation, and combine it with the confidence level output in step S30 to filter duplicate annotations and false detections, and output the final packaging defect detection result.
[0026] Specifically, step S40, which outputs the final packaging defect detection results, includes: Summary of detection results: Collect all detection results of the same package after processing in steps S10 to S30 from three perspectives of 0°, 45° and 90°, including the spatial coordinate information of each defect candidate area, such as center point coordinates, bounding box coordinates, classification category, such as scratches, wrinkles and false defects, and classification confidence, to form a multi-frame candidate area set. Duplicate annotation merging based on spatial correlation: For the aggregated defect candidate regions, duplicate annotations are filtered by calculating the spatial overlap. For any two candidate regions, the intersection-union ratio (IoU) of their bounding boxes is calculated, and a threshold is set. The threshold is dynamically set based on the average size of the candidate regions in step S20. When the IoU is greater than the set threshold, such as when the threshold is set to 50%, it is determined that the same defect is a duplicate annotation in different frames. For regions with duplicate annotations, the annotation result with the highest classification confidence is retained, and the remaining redundant annotations are removed to avoid the same defect being counted multiple times. Rule-based low-confidence false detection filtering includes label region filtering, size-confidence dual filtering, and multi-frame consistency verification. Label region filtering uses the label mask generated in step S10 to remove candidate regions located within the label mask area. Even if the confidence level is high, it is still judged as label interference. Size-confidence dual filtering refers to filtering small regions with an area ≤0.005mm² and a confidence level ≤0.8. The size threshold is based on the morphological screening in step S20, and the confidence threshold is based on the model's verification accuracy setting in step S30. Multi-frame consistency verification refers to retaining candidate regions that appear repeatedly in ≥2 frames of images, i.e., the features of the same defect under different viewpoints, and removing low-confidence regions that appear only in a single frame, which are usually noise. Final output: After merging and filtering in the above steps, the remaining defect categories, precise spatial locations (boundary box coordinates), size parameters (area, length / diameter) and final confidence level (the highest confidence value retained) are output, resulting in the final detection results and generating a complete defect detection report.
[0027] In addition, such as Figure 2 As shown, in one embodiment of the present invention, a visual inspection system for packaging defects is proposed, the system comprising: Spectral image acquisition and image preprocessing module: used to acquire multispectral images of the packaging at three viewing angles of 0°, 45° and 90°, separate the packaging body and packaging label area based on spectral differences, remove reflective interference through polarization decomposition, and output an interference-free packaging defect feature image that retains spectral characteristics, providing a clean input for subsequent feature extraction; Defect Candidate Region Extraction Module: This module is used to extract defect candidate regions based on feature enhancement. It performs multi-scale Top-Hat transformation on the packaging defect feature image output from the spectral image acquisition and image preprocessing module to obtain an enhanced defect feature image. It then combines adaptive threshold segmentation to extract defect candidate regions and outputs the coordinate information of the candidate regions through morphological screening. Attention Mechanism Classification and Recognition Module: Used to build a packaging defect recognition model that combines spatial-channel attention. The model inputs the interference-free packaging defect feature image obtained from the spectral image acquisition and image preprocessing module and the defect candidate region extracted from the defect candidate region extraction module. The model outputs packaging defect classification results with confidence. Packaging defect detection output module: It is used to obtain the spatial location correlation based on the candidate region coordinate information output by the defect candidate region extraction module, and filter duplicate annotations and false detections by combining the confidence score output by the attention mechanism classification and recognition module, and output the final packaging defect detection result.
[0028] This application provides a visual inspection system for packaging defects, employing a visual inspection method for packaging defects as described in the above embodiments. This system addresses the technical problems of low accuracy and low efficiency in traditional visual inspection methods for packaging defects. Compared to the prior art, the beneficial effects of the visual inspection system for packaging defects provided in this application are the same as those of the visual inspection method for packaging defects provided in the above embodiments. Furthermore, other technical features of the visual inspection system for packaging defects are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.
[0029] This application provides a visual inspection device for packaging defects, the visual inspection device for packaging defects includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform a visual inspection method for packaging defects as described in Embodiment 1 above.
[0030] In one embodiment of the present invention, a visual inspection device for packaging defects may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Description), PMPs (Portable Media Players), etc., and fixed terminals such as digital TVs, desktop computers, etc. This visual inspection device for packaging defects is merely an example and should not be construed as limiting the functionality and scope of the embodiments in this application.
[0031] The packaging defect visual inspection device may include a processor (e.g., a central processing unit, a graphics processing unit, etc.) that can perform various appropriate actions and processes according to a program stored in a machine-readable storage medium (ROM) or loaded from a storage system into random access memory (RAM). The RAM also stores various programs and data required for the operation of the packaging defect visual inspection device. The processing system, ROM, and RAM are interconnected via a bus. Input / output (I / O) interfaces are also connected to the bus. Typically, the following systems can be connected to the I / O interface: input devices including, for example, touchscreens, touchpads, keyboards, mice, image sensors, microphones, accelerometers, gyroscopes, etc.; output devices including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage systems including, for example, magnetic tape, hard disks, etc.; and a communication unit. The communication unit allows the packaging defect visual inspection device to communicate wirelessly or wiredly with other devices to exchange data. Although the figures show a packaging defect visual inspection device with various systems, it should be understood that it is not required to implement or possess all the systems shown. It can be implemented alternatively or with more or fewer systems.
[0032] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication unit, or installed from a storage device, or installed from a read-only memory. When the computer program is executed by a processor, it performs the functions defined in the methods of the embodiments disclosed in this application.
[0033] This application provides a visual inspection device for packaging defects, employing a visual inspection method for packaging defects as described in the above embodiments. This method addresses the technical problems of low accuracy and low efficiency in traditional visual inspection methods for packaging defects. Compared with the prior art, the beneficial effects of the visual inspection device for packaging defects provided in this application are the same as those of the visual inspection method for packaging defects provided in the above embodiments. Furthermore, other technical features of this visual inspection device for packaging defects are the same as those disclosed in the previous embodiment method, and will not be repeated here.
[0034] The various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.
[0035] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the above-described visual inspection method for packaging defects.
[0036] The computer program product provided in this application can solve the technical problems of low accuracy and low efficiency in traditional visual inspection methods for packaging defects. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as those of the visual inspection method for packaging defects provided in the above embodiments, and will not be repeated here.
[0037] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, this invention also intends to include these modifications and variations.
Claims
1. A visual inspection method for defects in packaging, characterized in that, The method includes the following steps: Step S10: Acquire multispectral images of the packaging at three viewing angles: 0°, 45°, and 90°. Separate the packaging body and packaging label area based on spectral differences. Remove reflective interference through polarization decomposition and output an interference-free image of the packaging defect features that retains spectral characteristics. Step S20: Extract candidate defect regions based on feature enhancement. Perform multi-scale Top-Hat transformation on the package defect feature image output in step S10 to obtain the enhanced defect feature image. Combine adaptive threshold segmentation to extract candidate defect regions and output the coordinate information of candidate regions through morphological filtering. Step S30: Construct a packaging defect recognition model that combines spatial-channel attention. Input the interference-free packaging defect feature image obtained in step S10 and the defect candidate region extracted in step S20 into the model. The model outputs the packaging defect classification result with confidence. Step S40: Based on the candidate region coordinate information output in step S20, obtain the spatial location correlation, and combine it with the confidence level output in step S30 to filter duplicate annotations and false detections, and output the final packaging defect detection result.
2. The method for visual inspection of packaging defects according to claim 1, characterized in that, The multispectral image in step S10 includes a visible light image, a near-infrared image, and a polarization image. The label area masking is based on the grayscale threshold of the near-infrared image. The threshold is dynamically determined by the difference between the visible light image and the near-infrared image. The de-reflection processing adopts the specular reflection component removal algorithm of the polarization image.
3. The method for visual inspection of packaging defects according to claim 1, characterized in that, The steps in step S10 include: Multispectral acquisition: A 3-channel camera is used to simultaneously acquire visible light images, near-infrared images, and polarization images, providing multi-dimensional data for subsequent steps; Tag masking: Analyze the differences between the tagged and non-tagged areas in the near-infrared image, where the gray value of the tagged area is <50 and the gray value of the non-tagged area is >150. Combine the visible light image to calibrate the tag edge and generate a tag mask to mark the non-detection area. De-reflection processing: The polarized image is decomposed into reflection components, the specular reflection component (i.e., the reflection component) is removed, and the diffuse reflection component is retained, which contains the true defect features of the packaging. The processed polarized image is used as the input for step S20.
4. The method for visual inspection of packaging defects according to claim 1, characterized in that, The step S20, which involves extracting candidate defect regions based on feature enhancement, includes: Multi-scale enhancement: For the feature distribution of defects in the interference-free packaging defect feature image output in step S10, Top-Hat transformation of 3×3, 5×5 and 7×7 structuring elements is used to enhance the grayscale contrast between packaging defects and image background. Adaptive segmentation: Based on the local gray-scale mean μ and standard deviation σ of the image after removing reflective interference through polarization decomposition in step S10, the segmentation threshold T=μ-0.6σ is dynamically set. The threshold parameter is optimized based on the noise level of the preprocessed image to segment out the defect area. Morphological screening: The segmentation results are subjected to erosion and dilation. Erosion refers to removing noise that was not completely eliminated in step S10, and dilation refers to restoring the defect morphology. Regions with an area > 0.005 mm² are extracted as candidate regions, and candidate regions with coordinate information are output.
5. The method for visual inspection of packaging defects according to claim 1, characterized in that, The step S30, which involves constructing a packaging defect identification model that combines spatial-channel attention and outputting packaging defect classification results with confidence scores, includes the following steps: Model construction: Based on ResNet50, a spatial attention module and a channel attention module are embedded. The spatial attention module is used for the enhanced defect feature image in step S20, and the channel attention module is used for weight allocation for the preprocessed visible light image, near-infrared image and polarization image in step S10. Sample association: Construct training samples, including the interference-free packaging defect feature image with preserved spectral features output after preprocessing in step S10 and the defect candidate region extracted in step S20. The defect candidate region extracted in step S20 contains the enhanced defect features, enabling the model to learn the complete defect representation from the preprocessed features to the enhanced features. Classification Reasoning: The model's output layer classifies the input defect candidate regions, outputting scratch and wrinkle defect categories or pseudo-defects. The Softmax activation function is used. The output layer contains neurons with an equal number of defect categories. Each neuron corresponds to a probability output for a defect category. The sum of the probability outputs for all defect categories is 1. For the input defect candidate regions, the model calculates the probability value of each defect category through forward propagation. The category with the highest probability is the detection category of the region, and the corresponding probability value is the confidence level of the classification result. Regions with a confidence level > 0.8 are marked as suspected defects, and the result is passed to step S40.
6. The method for visual inspection of packaging defects according to claim 1, characterized in that, The step S40, which outputs the final packaging defect detection result, includes: Summary of detection results: Collect all detection results of the same package after processing in steps S10 to S30 at three viewing angles of 0°, 45° and 90°, including the spatial coordinate information, classification category and classification confidence of each defect candidate area, to form a multi-frame candidate area set; Duplicate annotation merging based on spatial correlation: For the summarized defect candidate regions, duplicate annotations are filtered by calculating the spatial overlap. For any two candidate regions, the intersection-union ratio (IoU) of their bounding boxes is calculated, and a threshold is set. The threshold is dynamically set based on the average size of the candidate regions in step S20. When the IoU is greater than the set threshold, it is determined to be a duplicate annotation of the same defect in different frames. For regions with duplicate annotations, the annotation result with the highest classification confidence is retained, and the remaining redundant annotations are removed. Rule-based low-confidence false detection filtering includes label region filtering, size-confidence dual filtering, and multi-frame consistency verification. Label region filtering uses the label mask generated in step S10 to remove candidate regions located within the label mask area. Size-confidence dual filtering refers to filtering regions with an area ≤0.005mm² and a confidence ≤0.
8. The size threshold is based on the morphological screening in step S20, and the confidence threshold is based on the model's verification accuracy setting in step S30. Multi-frame consistency verification refers to retaining candidate regions that appear repeatedly in ≥2 frames of images, i.e., the features of the same defect under different viewpoints, and removing regions that appear only in a single frame. Final output: After merging and filtering in the above steps, the remaining defect categories, precise spatial locations, size parameters, and final confidence levels (i.e., the highest confidence value retained) are output, resulting in the final detection results and a complete defect detection report.
7. A visual inspection system for packaging defects, characterized in that, include: Spectral image acquisition and image preprocessing module: used to acquire multispectral images of the packaging at three viewing angles of 0°, 45° and 90°, separate the packaging body and packaging label area based on spectral differences, remove reflective interference through polarization decomposition, and output an interference-free packaging defect feature image that retains spectral characteristics; Defect Candidate Region Extraction Module: This module is used to extract defect candidate regions based on feature enhancement. It performs multi-scale Top-Hat transformation on the packaging defect feature image output from the spectral image acquisition and image preprocessing module to obtain an enhanced defect feature image. It then combines adaptive threshold segmentation to extract defect candidate regions and outputs the coordinate information of the candidate regions through morphological screening. Attention Mechanism Classification and Recognition Module: Used to build a packaging defect recognition model that combines spatial-channel attention. The model inputs the interference-free packaging defect feature image obtained from the spectral image acquisition and image preprocessing module and the defect candidate region extracted from the defect candidate region extraction module. The model outputs packaging defect classification results with confidence. Packaging defect detection output module: It is used to obtain the spatial location correlation based on the candidate region coordinate information output by the defect candidate region extraction module, and filter duplicate annotations and false detections by combining the confidence score output by the attention mechanism classification and recognition module, and output the final packaging defect detection result.
8. A visual inspection device for packaging defects, characterized in that, include: A memory, a processor, and a packaging defect visual inspection program stored in the memory and executable on the processor, wherein the packaging defect visual inspection program, when executed by the processor, implements a packaging defect visual inspection method as described in any one of claims 1 to 6.
9. A computer program product, characterized in that, The computer program product includes a packaging defect visual inspection program, which, when executed by a processor, implements a packaging defect visual inspection method as described in any one of claims 1 to 6.