A toothpaste tube quality detection method and system based on machine vision
Through machine vision-based Gaussian smoothing, background removal, and adaptive threshold segmentation, the problem of large defect segmentation error in toothpaste tube quality inspection was solved, achieving higher-precision defect detection.
Patent Information
- Application Number
- CN202510949317.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-10
- Publication Date
- 2025-09-12
- Estimated Expiration
- 2045-07-10
AI Technical Summary
In the existing technology, traditional threshold segmentation methods are difficult to accurately segment the defects of toothpaste tubes, resulting in large defect segmentation errors during toothpaste tube quality inspection.
A toothpaste tube quality inspection method based on machine vision is adopted. Through steps such as Gaussian smoothing, background subtraction, multi-scale feature extraction, feature fusion and adaptive threshold segmentation, the contrast and visibility of defective areas are enhanced, thereby improving detection accuracy.
The accuracy of toothpaste tube quality inspection is improved, ensuring the effective segmentation of defects of different sizes and types, and reducing false detections and missed detections.
Smart Images

Figure CN120451161B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular to a toothpaste tube quality detection method and system based on machine vision. Background Art
[0002] In the manufacturing of daily chemical products like toothpaste, toothpaste tubes, as the direct packaging container, have a direct impact on product storage, transportation, and the consumer experience. The quality of toothpaste tubes is not only reflected in their aesthetic appearance, but more importantly, they must ensure their sealing, structural integrity, and material consistency to prevent problems such as toothpaste leakage and deterioration. Therefore, rigorous quality inspections are essential during the production process.
[0003] The existing method for inspecting the production quality of toothpaste tubes involves shining light from the tube's head toward the tube's tail. A grayscale image of the tube's end face, viewed from above, is captured. This image is then segmented using threshold segmentation to identify defects. Regions with uniform material, consistent thickness, and no defects have good light transmittance and appear as brighter areas in the grayscale image. However, regions with defects (such as foreign matter, sudden thickness changes, cracks, bubbles, or poor sealing) or inherent material inhomogeneities block, scatter, or absorb more light, reducing the intensity of light transmitted to the tube's end face. This results in darker areas, dark spots, dark lines, or unevenly lit patches in the grayscale image. However, due to the poor quality of the transmitted image, defects typically appear only as dark or bright spots, while other defects are less noticeable, making it difficult for traditional threshold segmentation methods to accurately segment defects. Summary of the Invention
[0004] In view of this, an embodiment of the present invention provides a toothpaste tube quality inspection method and system based on machine vision to solve the problem of large defect segmentation error when using traditional threshold segmentation to inspect the quality of toothpaste tubes.
[0005] In a first aspect, an embodiment of the present invention provides a method for detecting the quality of a toothpaste tube based on machine vision, the method comprising the following steps:
[0006] Photograph the tail end face of the toothpaste tube to obtain a top-down grayscale image of any toothpaste tube;
[0007] Performing background simulation on the overhead grayscale image by Gaussian smoothing to obtain a simulated background image, performing background subtraction on the overhead grayscale image using the grayscale value of each pixel in the simulated background image to obtain a background subtraction image, and dynamically correcting the background subtraction image to obtain a corrected image;
[0008] Performing multi-scale feature extraction on the corrected image through Gaussian filtering of different degrees to obtain a preset number of filtered images, performing Gaussian difference on each of the filtered images to obtain a set of bright defect response images and a set of dark defect response images, and performing multi-scale feature fusion on the set of bright defect response images and the set of dark defect response images to obtain a feature image;
[0009] According to the mean grayscale value of all pixels in the feature image, the feature image is stretched to obtain a target image, and the target image is segmented by adaptive threshold to obtain a corresponding binary image;
[0010] Connected domain analysis is performed on the binary image to obtain at least two foreground connected domains, the foreground connected domains are screened to obtain target connected domains, and the quality inspection result of any toothpaste tube is determined according to the target connected domains.
[0011] In a second aspect, an embodiment of the present invention provides a toothpaste tube quality inspection system based on machine vision, comprising a memory, a processor, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, it implements a toothpaste tube quality inspection method based on machine vision as described in the first aspect.
[0012] Compared with the prior art, the embodiments of the present invention have the following beneficial effects:
[0013] The present invention obtains a simulated background image in order to eliminate low-frequency brightness gradients in a top-view grayscale image to highlight defect areas. By simulating the background image, the background is subtracted from the original image to obtain a background-cutout image that retains high-frequency details. By dynamically correcting the background-cutout image, dark defects are made more prominent. Multi-scale feature extraction is performed on the corrected image to enhance the visibility of defects of different sizes. Furthermore, multi-scale feature fusion is performed on a set of bright defect response images and a set of dark defect response images, and the feature image is dynamically stretched to make the difference between the defect and the background in the target image more obvious, facilitating subsequent segmentation. Finally, adaptive segmentation is performed on the target image to make the final defect detection more accurate, thereby improving the accuracy of subsequent defect-based toothpaste tube quality detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0015] Figure 1This is a flow chart of a method for detecting toothpaste tube quality based on machine vision provided in Example 1 of the present invention;
[0016] Figure 2 is a schematic diagram of a toothpaste tube provided by an embodiment of the present invention;
[0017] Figure 3 This is a schematic diagram of a top-view grayscale image captured of the tail of a toothpaste tube provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0018] The embodiments of the present disclosure are described in detail below, and examples of the embodiments are shown in the accompanying drawings. The embodiments described below with reference to the accompanying drawings are exemplary and intended to be used to explain the present disclosure, but should not be understood as limiting the present disclosure.
[0019] It should be noted that the terms "first," "second," and the like in the specification of the present disclosure and the accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure.
[0020] In order to illustrate the technical solution of the present invention, specific embodiments are provided below.
[0021] See also Figure 1 , is a flow chart of a method for detecting toothpaste tube quality based on machine vision provided in the first embodiment of the present invention, such as Figure 1 As shown, the method may include:
[0022] Step S101 : photograph the tail of a toothpaste tube to obtain a top-view grayscale image of any toothpaste tube.
[0023] After the toothpaste tube is produced, it enters the quality inspection stage. At this time, the toothpaste tube is closed at the head but not at the tail, as shown below Figure 2 As shown, after the toothpaste tube passes the quality inspection, the toothpaste is injected into the toothpaste tube from the tail end, and then the tail is sealed to complete the toothpaste filling.
[0024] Among them, after the toothpaste tube is transported to the quality inspection link, the toothpaste tube is turned head down and tail up, and a uniform diffuse LED surface light source is installed on the quality inspection operating table. The light source is vertically upward, and the toothpaste tube to be inspected is sent into the light source area, so that the light source is vertically outward from the head of the toothpaste tube. At this time, the image is collected from the tail of the toothpaste tube. The camera is 20 cm above the tail of the toothpaste tube and collects a top-view image. Figure 3 shown.
[0025] The collected overhead image is denoised and grayscaled to obtain a grayscale image. Since there are multiple toothpaste tubes in the grayscale image, the grayscale image is segmented so that only one toothpaste tube exists in the segmented image. The purpose of segmentation is to simplify subsequent processing and make the subsequent processing results more accurate. Therefore, the overhead grayscale image of any toothpaste tube is taken as an example for subsequent analysis, and the overhead grayscale image of any toothpaste tube is recorded as .
[0026] Step S102, performing background simulation on the overhead grayscale image through Gaussian smoothing to obtain a simulated background image, performing background subtraction on the overhead grayscale image using the grayscale value of each pixel in the simulated background image to obtain a background subtraction map, and dynamically correcting the background subtraction map to obtain a corrected image.
[0027] Due to the natural variations in the toothpaste tube wall, such as being thick at the edges and thin in the center, low-frequency (background) brightness gradients can be generated in the top-view grayscale image obtained by transmission. However, defect signals are relatively weak, and dark defects, in particular, are easily submerged in the background. Therefore, in an embodiment of the present invention, large-scale Gaussian smoothing is performed on the top-view grayscale image. The low-pass filtering characteristics of the large-scale Gaussian kernel can filter out high-frequency components in the image, such as defects and noise, while retaining low-frequency background components. The filtered image can be used as a background image, and the high-frequency component can be separated by subtracting the original image from the background image. Because dark defects (such as bubbles and cracks) are more common and have lower contrast in transmission imaging, and are more likely to be compressed in subsequent processing, making them difficult to effectively separate, large-scale Gaussian smoothing of the top-view grayscale image significantly enhances the contrast of dark defects, facilitating subsequent defect segmentation.
[0028] Specifically, the image The length is M and the width is N. In the toothpaste tube image, the background variation is usually in the range of a few millimeters to several millimeters. The typical background variation area is usually 2-3 mm. With a spatial resolution of 0.1 mm / pixel, 25 pixels correspond to 2.5 mm. In order to obtain a simulated background image, the typical background variation needs to be smoothed out. Therefore, in the embodiment of the present invention, the standard deviation of the Gaussian kernel is set to Take 25 pixels, Gaussian smoothing and two-dimensional Gaussian kernel function are existing technologies, which will not be summarized in detail here. Perform Gaussian smoothing to obtain a smoothed image, which is recorded as the simulated background image .
[0029] The image With image The grayscale value of each pixel in the image is subtracted. At this time, the uniform area will be near 0, while the suspected defect will deviate from 0. Among them, bright defects are positive and dark defects are negative. In order to maintain the overall brightness level of the image and avoid excessive grayscale deviation to the negative area, the embodiment of the present invention further combines the image The mean gray value of the image Remove the background in the image to The high frequency part of the image is separated and the contrast of dark defects is enhanced. The mean gray value of the image The method to remove the background in is:
[0030] Calculating the mean grayscale value of all pixels in the simulated background image; for any pixel in the overhead grayscale image, obtaining a pixel in the simulated background image at the same position as the any pixel, recording it as a comparison pixel; calculating the grayscale difference between the any pixel and the comparison pixel, and taking the sum of the grayscale difference and the mean grayscale value as the new grayscale value of the any pixel;
[0031] Obtain the new grayscale value of each pixel in the overhead grayscale image, and use all the new grayscale values to replace the grayscale value of each pixel in the overhead grayscale image to obtain the background removal image. .
[0032] In one embodiment, the background subtraction map is constructed using the formula:
[0033]
[0034] in, Indicates that the position in the background removal image is The gray value of the pixel, Indicates the position in the top-down grayscale image is The gray value of the pixel, Indicates the position of the simulated background image is The gray value of the pixel, Represents the mean grayscale value of all pixels in the simulated background image.
[0035] Background removal The grayscale value of a medium-bright defect is greater than 0, meaning it is brighter than the background, while the grayscale value of a dark defect is less than 0, meaning it is darker than the background. Since dark defects (such as bubbles and cracks) are more common and have lower contrast in transmission imaging, and are more likely to be compressed in subsequent processing, making it difficult to effectively separate the defects, the background subtraction image is dynamically corrected to obtain a corrected image. This expands the grayscale value range of dark defects while maintaining the grayscale value of bright defects. The correction method is as follows:
[0036] For any pixel in the background subtraction image, if the grayscale value of any pixel is greater than or equal to 0, the grayscale value of any pixel is used as the grayscale correction value; if the grayscale value of any pixel is less than 0, the product of the grayscale value of any pixel and the preset adjustment coefficient is used as the grayscale correction value;
[0037] Obtain the grayscale correction value of each pixel in the background subtraction image, and replace the grayscale value of each pixel in the background subtraction image with all grayscale correction values to obtain a corrected image .
[0038] In one embodiment, the correction formula is:
[0039]
[0040] in, Indicates that the position in the corrected image is The gray value of the pixel, Indicates that the position in the background removal image is The grayscale value of the pixel, 2 represents the preset adjustment multiple.
[0041] It should be noted that if If the value is less than 0, it means that the pixel point may be a dark defect position, and the contrast is increased by 1 times by the preset adjustment coefficient 2, making it easier to detect in the future. For example, there is a bubble with a diameter of 0.2 mm in a toothpaste tube. The gray value is , the contrast-to-noise ratio is signal / noise standard deviation = 30 / 12.5 = 1.2; after correction, the grayscale value becomes -60, and the contrast-to-noise ratio is signal / noise standard deviation = 60 / 12.5 = 2.5 (an improvement of 108%).
[0042] In step S103, multi-scale feature extraction is performed on the corrected image through Gaussian filtering of different degrees to obtain a preset number of filtered images, Gaussian difference is performed on each of the filtered images to obtain a set of bright defect response images and a set of dark defect response images, and multi-scale feature fusion is performed on the set of bright defect response images and the set of dark defect response images to obtain a feature image.
[0043] The corrected image enhances the contrast of dark defects to a certain extent, but due to different defect types, different types of defects require different detection scales, that is, the response degree of different defects at different scales is different. Therefore, when obtaining images of different scales, a smaller Gaussian kernel will retain image details and enhance small spots, while a larger Gaussian kernel can be used to estimate the local background, and then the light and dark areas are enhanced differently by the difference method. The enhanced features are then weighted to merge the defect responses at different scales into a feature map, ensuring that defects of different sizes have high responses. For each pixel position, its maximum response value is taken. As long as a defect has a significant response at any scale, it will be retained, thereby obtaining a feature map that integrates all scales and all defect polarities (bright / dark), where the response value of the defect area is higher and the response value of the background area is lower.
[0044] First, according to the preset number of k values, Gaussian filtering is performed on the corrected image to obtain the corresponding filtered image under each k value. The calculation formula of Gaussian filtering is:
[0045]
[0046] in, Indicates the use of standard deviation The position in the corrected image is The gray value of the pixel after Gaussian filtering, represents the Gaussian kernel, represents the corrected image, Represents the coordinate value of any pixel in the corrected image, Indicates the coordinate value of the corresponding position of the Gaussian kernel;
[0047] in, , represents the standard deviation, ; preferably, ,correspond The scale selection covers the defect size from small bubbles of 0.1 mm to large foreign matter of 0.8 mm, with a resolution of 0.1 mm / pixel. The size of each Gaussian kernel is ,For example: When , the Gaussian kernel size is It should be noted that the Gaussian kernel is an existing technology and will not be described in detail here.
[0048] Then, after obtaining the four filtered images, since defects of different sizes have different response intensities at different scales, for example, small defects are clear under a small kernel, while large defects can only be fully displayed under a large kernel, the embodiment of the present invention uses Gaussian difference to perform Gaussian filtering of different degrees on each filtered image, and then uses the difference to highlight the parts that are brighter and darker than the background. The specific process is as follows:
[0049] Set the first preset standard deviation , the second preset standard deviation , wherein the first preset standard deviation is smaller than the second preset standard deviation. For any filtered image, the first preset standard deviation and the second preset standard deviation are obtained. According to the above-mentioned Gaussian filtering method, the first preset standard deviation and the second preset standard deviation are used to perform Gaussian filtering on the filtered image, respectively, to obtain the first image and the second image .
[0050] The first image Subtract the second image , get the bright defect response image , the second image Subtract the first image Then take the absolute value operation to obtain the dark defect response image .
[0051] In one embodiment, the calculation formula of the bright defect response image is:
[0052]
[0053] in, Indicates the position of the bright defect response image is The gray value of the pixel, Indicates that the position in the first image is The gray value of the pixel, Indicates that the position in the second image is The grayscale value of the pixel.
[0054] The calculation formula of the dark defect response image is:
[0055]
[0056] in, Indicates the position of the dark defect response image is The gray value of the pixel, Indicates that the position in the first image is The gray value of the pixel, Indicates that the position in the second image is The grayscale value of the pixel.
[0057] It should be noted that the small kernel retains details, the large kernel smoothes the background, and after subtraction, the areas brighter than the background are highlighted, which can reflect bright defects. For dark defects, the Gaussian kernel order is reversed, that is, the large kernel reduces the kernel, and the areas darker than the background are highlighted, that is, dark defects. Then, the absolute value of the grayscale value of the dark defect is taken to make the response of the dark defect positive, which is convenient for subsequent processing. Similarly, the bright defect response image and the dark defect response image corresponding to each of the filtered images can be obtained, and the bright defect response image and the dark defect response image corresponding to each of the filtered images are combined into a bright defect response image set and a dark defect response image set.
[0058] The bright defect response image set and the dark defect response image set represent the difference at different scales. Multi-scale feature fusion is performed on the bright defect response image set and the dark defect response image set to obtain a feature image. The specific method is as follows:
[0059] For any position in the feature image, in each bright defect response image of the bright defect response image set, the grayscale value of the pixel point at the same position as the any position is obtained to form a first grayscale value set; in each dark defect response image of the dark defect response image set, the grayscale value of the pixel point at the same position as the any position is obtained to form a second grayscale value set; the maximum value in the first grayscale value set and the maximum value in the second grayscale value set are weighted summed to obtain the grayscale value of the any position; the grayscale value of each position in the feature image is obtained to obtain the feature image .
[0060] In one embodiment, the formula for constructing the feature image is:
[0061]
[0062]
[0063]
[0064] in, represents the maximum value in the first grayscale value set, represents the maximum value in the second grayscale value set, represents the maximum function, Indicates the position of the bright defect response image is The gray value of the pixel, Indicates the position of the dark defect response image is The gray value of the pixel, Indicates the position in the feature image is The gray value of the pixel, represents the first weight, Represents the second weight.
[0065] It should be noted that, in actual production data, about 70% of defects are dark defects (such as bubbles, cracks, and foreign matter), and 30% are bright defects (such as bright lines on edges caused by poor sealing, bright spots caused by sudden changes in thickness, etc.). Dark defects should be given a higher weight. Therefore, the embodiment of the present invention sets , there is no restriction here.
[0066] Step S104 : stretching the feature image according to the mean grayscale value of all pixels in the feature image to obtain a target image, and performing adaptive threshold segmentation on the target image to obtain a corresponding binary image.
[0067] Get the feature image Afterwards, the dynamic range of the feature image is stretched to make the difference between the defect and the background more obvious, so as to facilitate subsequent segmentation. Therefore, in the embodiment of the present invention, the feature image is stretched according to the mean gray value of all pixels in the feature image, and the stretched feature image is recorded as the target image. .
[0068] Among them, the method of stretching the feature image according to the mean gray value of all pixels in the feature image is:
[0069] Obtain the mean grayscale value of all pixels in the characteristic image, and for any pixel in the characteristic image, obtain the grayscale difference between the grayscale value of any pixel and the mean grayscale value, use the product of the grayscale difference and a preset contrast stretching factor as the stretching value, and use the sum of the grayscale value mean and the stretching value as the grayscale value of the any pixel after stretching; obtain the grayscale value of each pixel in the characteristic image after stretching to obtain a target image.
[0070] In one embodiment, the grayscale value stretching calculation formula is:
[0071]
[0072] in, Indicates the position in the target image is The gray value of the pixel, Represents the mean gray value of all pixels in the feature image, Indicates the preset contrast stretching factor, Indicates the position in the feature image is The grayscale value of the pixel.
[0073] It should be noted that An empirical value of 1.8 is taken, which is determined by experiments to expand the contrast standard deviation of the defect area by 1.8 times while avoiding excessive enhancement of noise.
[0074] After obtaining the target image, the target image is segmented to obtain the corresponding segmentation results for the toothpaste tube quality inspection. Therefore, the embodiment of the present invention performs adaptive threshold segmentation on the target image. First, the Otsu method (inter-variance method) is used to obtain the segmentation threshold of the target image as the basic threshold T. Then, due to the different features of different areas in the image, the global threshold is difficult to accurately segment all foregrounds. Therefore, the target image is evenly divided to obtain at least two sub-images, respectively obtaining the grayscale mean and grayscale variance of each sub-image, and obtaining the local contrast intensity of each sub-image according to the grayscale mean and grayscale variance of each sub-image; wherein the calculation formula of the local contrast intensity is:
[0075]
[0076] in, represents the local contrast intensity of the w-th sub-image, represents the grayscale variance of the w-th sub-image, Represents the grayscale mean of the w-th sub-image, and 0.0.1 represents a hyperparameter to prevent the denominator from being 0.
[0077] Then, the local contrast intensity of each sub-image is assigned to each pixel within the sub-image. If the local contrast intensity is high, the segmentation threshold needs to be increased to avoid false detection of background noise. If the local contrast intensity is low, the segmentation threshold needs to be lowered to retain weak defect signals. Since the contrast of the defect is less than 10%, it may be ignored by the global threshold in low-contrast areas. Therefore, for any pixel in the target image, the difference between the local contrast intensity of the sub-image to which the pixel belongs and the preset basic contrast intensity is obtained, the product of the difference value and the preset adjustment factor is calculated, and the sum of the constant 1 and the product is used as the adjustment coefficient; the product of the basic threshold and the adjustment coefficient is used as the adaptive segmentation threshold of the pixel. If the grayscale value of the pixel is greater than the adaptive segmentation threshold, the grayscale value of the pixel is set to 1; if the grayscale value of the pixel is less than or equal to the adaptive segmentation threshold, the grayscale value of the pixel is set to 0; each pixel in the target image is traversed to obtain the corresponding binary image.
[0078] The calculation formula of the adaptive segmentation threshold is:
[0079]
[0080] in, Indicates the position in the target image is The adaptive segmentation threshold of the pixel point, represents the basic threshold, Indicates the preset adjustment factor, Indicates the position in the target image is The local contrast intensity of the sub-image to which the pixel belongs, Indicates the base contrast strength of the preset.
[0081] It should be noted that Take the experience value 0.5, The empirical value is 0.2. There is no restriction here and it can be set according to the implementation scenario.
[0082] Step S105 , performing connected domain analysis on the binary image to obtain at least two foreground connected domains, screening the foreground connected domains to obtain target connected domains, and determining the quality inspection result of any toothpaste tube according to the target connected domains.
[0083] After obtaining the binary image, the binary image is morphologically processed to remove pseudo-defects. After the morphological processing is completed, the binary image is subjected to a connected domain analysis to obtain at least two foreground connected domains, wherein the connected domain analysis belongs to the prior art and will not be described in detail here. The foreground connected domains are then screened: the number of pixels in each foreground connected domain is counted, and foreground connected domains with a number of pixels less than a preset threshold (preferably set to 5) are removed to obtain the remaining foreground connected domains, and the circularity and aspect ratio of each remaining foreground connected domain are obtained. At least one remaining foreground connected domain that meets the requirements in terms of circularity and aspect ratio is used as the target connected domain. Preferably, the circularity requirement is: the circularity is less than or equal to 0.6, and the aspect ratio requirement is: the remaining foreground connected domain with an aspect ratio greater than or equal to 3 is used as the target connected domain. The circularity and aspect ratio both belong to the prior art and will not be described in detail here. At this time, the target connected domain is also the defect area, and the defect position of the target connected domain is marked. When there is at least one target connected domain, it indicates that any toothpaste tube has a quality problem. Otherwise, it is determined that any toothpaste tube has passed the quality inspection.
[0084] Based on the same inventive concept as the above method, an embodiment of the present invention also provides a toothpaste tube quality inspection system based on machine vision, including a memory, a processor, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, it implements the steps of any one of the above-mentioned toothpaste tube quality inspection methods based on machine vision.
[0085] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present invention, and should all be included in the scope of protection of the present invention.
Claims
1. A toothpaste tube quality inspection method based on machine vision, characterized in that: The method comprises: Photograph the tail end face of the toothpaste tube to obtain a top-down grayscale image of any toothpaste tube; Performing background simulation on the overhead grayscale image by Gaussian smoothing to obtain a simulated background image, performing background subtraction on the overhead grayscale image using the grayscale value of each pixel in the simulated background image to obtain a background subtraction image, and dynamically correcting the background subtraction image to obtain a corrected image; Performing multi-scale feature extraction on the corrected image through Gaussian filtering of different degrees to obtain a preset number of filtered images, performing Gaussian difference on each of the filtered images to obtain a set of bright defect response images and a set of dark defect response images, and performing multi-scale feature fusion on the set of bright defect response images and the set of dark defect response images to obtain a feature image; According to the mean grayscale value of all pixels in the feature image, the feature image is stretched to obtain a target image, and the target image is segmented by adaptive threshold to obtain a corresponding binary image; Performing a connected domain analysis on the binary image to obtain at least two foreground connected domains, screening the foreground connected domains to obtain a target connected domain, and determining a quality inspection result of any toothpaste tube according to the target connected domain; Performing Gaussian difference on each of the filtered images to obtain a set of bright defect response images and a set of dark defect response images, including: Obtaining a first preset standard deviation and a second preset standard deviation, wherein the first preset standard deviation is smaller than the second preset standard deviation, and for any filtered image, performing Gaussian filtering on the filtered image using the first preset standard deviation and the second preset standard deviation, respectively, to obtain a first image and a second image, subtracting the second image from the first image to obtain a bright defect response image, and subtracting the first image from the second image and performing an absolute value operation to obtain a dark defect response image; Acquire a bright defect response image and a dark defect response image corresponding to each of the filtered images to form a bright defect response image set and a dark defect response image set; The multi-scale feature fusion of the bright defect response image set and the dark defect response image set to obtain a feature image includes: For any position in the feature image, in each bright defect response image of the bright defect response image set, the grayscale value of the pixel at the same position as the any position is obtained to form a first grayscale value set; in each dark defect response image of the dark defect response image set, the grayscale value of the pixel at the same position as the any position is obtained to form a second grayscale value set; a weighted sum of the maximum value in the first grayscale value set and the maximum value in the second grayscale value set is performed to obtain the grayscale value of the any position; Get the grayscale value of each position in the feature image to obtain the feature image.
2. The toothpaste tube quality inspection method based on machine vision according to claim 1, characterized in that: The method of performing background subtraction on the overhead grayscale image by using the grayscale value of each pixel in the simulated background image to obtain a background subtraction image includes: Calculating the mean grayscale value of all pixels in the simulated background image; for any pixel in the overhead grayscale image, obtaining a pixel in the simulated background image at the same position as the any pixel, recording it as a comparison pixel; calculating the grayscale difference between the any pixel and the comparison pixel, and taking the sum of the grayscale difference and the mean grayscale value as the new grayscale value of the any pixel; A new grayscale value of each pixel in the overhead grayscale image is obtained, and the grayscale value of each pixel in the overhead grayscale image is replaced with all the new grayscale values to obtain a background subtraction image.
3. The toothpaste tube quality inspection method based on machine vision according to claim 1, characterized in that: The dynamically correcting the background removal image to obtain a corrected image includes: For any pixel in the background subtraction image, if the grayscale value of any pixel is greater than or equal to 0, the grayscale value of any pixel is used as the grayscale correction value; if the grayscale value of any pixel is less than 0, the product of the grayscale value of any pixel and the preset adjustment coefficient is used as the grayscale correction value; Obtain the grayscale correction value of each pixel in the background subtraction image, and replace the grayscale value of each pixel in the background subtraction image with all grayscale correction values to obtain a corrected image.
4. The toothpaste tube quality inspection method based on machine vision according to claim 1, characterized in that: The multi-scale feature extraction is performed on the corrected image by Gaussian filtering of different degrees to obtain a preset number of filtered images, including: The calculation formula of Gaussian filtering is: in, Indicates the use of standard deviation The position in the corrected image is The gray value of the pixel after Gaussian filtering, represents the Gaussian kernel, represents the corrected image, Represents the coordinate value of any pixel in the corrected image, Indicates the coordinate value of the corresponding position of the Gaussian kernel; in, , represents the standard deviation, ; According to a preset number of k values, Gaussian filtering is performed on the corrected image to obtain a corresponding filtered image under each k value.
5. The toothpaste tube quality inspection method based on machine vision according to claim 1, characterized in that: The step of stretching the feature image according to the mean grayscale value of all pixels in the feature image to obtain the target image includes: Obtaining a mean grayscale value of all pixels in the feature image, obtaining, for any pixel in the feature image, a grayscale difference between the grayscale value of the any pixel and the mean grayscale value, taking the product of the grayscale difference and a preset contrast stretching factor as a stretching value, and taking the sum of the mean grayscale value and the stretching value as the grayscale value of the any pixel after stretching; The grayscale value of each pixel in the feature image after stretching is obtained to obtain a target image.
6. The toothpaste tube quality inspection method based on machine vision according to claim 1, characterized in that: The step of performing adaptive threshold segmentation on the target image to obtain a corresponding binary image includes: Obtaining a segmentation threshold of the target image as a basic threshold using the Otsu method; equally dividing the target image to obtain at least two sub-images, obtaining a grayscale mean and a grayscale variance of each sub-image, and obtaining a local contrast intensity of each sub-image based on the grayscale mean and grayscale variance of each sub-image; For any pixel in the target image, obtain the difference between the local contrast intensity of the sub-image to which the pixel belongs and a preset basic contrast intensity, calculate the product of the difference value and a preset adjustment factor, and use the sum of a constant 1 and the product as the adjustment coefficient; use the product of the basic threshold and the adjustment coefficient as the adaptive segmentation threshold of the pixel; if the grayscale value of the pixel is greater than the adaptive segmentation threshold, set the grayscale value of the pixel to 1; if the grayscale value of the pixel is less than or equal to the adaptive segmentation threshold, set the grayscale value of the pixel to 0; Traverse each pixel in the target image to obtain a corresponding binary image.
7. The toothpaste tube quality inspection method based on machine vision according to claim 1, characterized in that: The foreground connected domain is screened to obtain the target connected domain, including: Counting the number of pixels in each foreground connected domain, removing foreground connected domains having a number of pixels less than a preset threshold value to obtain remaining foreground connected domains, obtaining the circularity and aspect ratio of each remaining foreground connected domain, and selecting at least one remaining foreground connected domain that meets the requirements for circularity and aspect ratio as a target connected domain.
8. A toothpaste tube quality inspection system based on machine vision, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, the steps of the toothpaste tube quality inspection method based on machine vision as described in any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Method for detecting weak line defect under arc-shaped surface gradient background
CN112598647A
NES-photovoltaic module big data AI analysis and diagnosis system
CN119169393A