Disclosure of Invention
In order to solve the problems, the invention provides a defect detection method for a bottle mouth of a bottle, which has the advantages of high speed and high accuracy for detecting the defects of the bottle mouth and can meet the requirements on a production line.
Based on this, the invention provides a defect detection method for a bottle opening, which comprises the following steps:
performing threshold segmentation and feature extraction on the bottleneck image, and extracting a template area to be established;
generating a region to be cut according to the center coordinate of the region of the template to be established and the vertical coordinate of the highest point of the outline;
cutting the region to be cut out from the bottleneck image, and performing threshold segmentation on the region to be cut out to obtain the region area;
when the bottleneck image is a complete image of the bottleneck, the area of the obtained area is used as an area standard;
and subtracting the area standard from the area obtained by the bottleneck image, comparing the absolute value of the difference obtained by subtracting with a preset value, and if the absolute value of the difference is greater than the preset value, determining that the bottleneck is unqualified.
And denoising the complete image of the bottle opening before reading the image to be detected.
The threshold segmentation and feature extraction of the bottle opening image and the extraction of the template area to be established comprise the following steps:
performing global threshold segmentation on the bottleneck image, and extracting pixel points with gray values within a preset gray range;
performing connected region combination on the pixels, and extracting a maximum area region as an image to be processed according to the shape area characteristics of the connected regions;
and performing gray level closed operation on the image to be processed to be used as a template area to be established.
The method for global threshold segmentation comprises the following steps: histogram bimodal method.
And averaging the row coordinates and the vertical coordinates of the pixel points to obtain the area center coordinates.
And the point with the minimum vertical coordinate of the pixel point is the highest point of the contour.
The generating of the area to be cut according to the center coordinate of the area to be built and the vertical coordinate of the highest point of the outline comprises the following steps:
and generating a rectangular region to be cut by taking the central coordinate as a center, wherein the region to be cut is symmetrical by taking the bottle mouth as the center.
The threshold segmentation is used for segmenting the bottleneck image into a target area and a background area with different gray levels according to the gray level difference between the bottleneck image and the background image, selecting a threshold and determining that each pixel point in the bottleneck image belongs to the target area or the background area to generate a corresponding binary image.
And the global threshold is realized by calculating the gray value of the peak in the region to be cut minus a preset gray value.
If the absolute value of the difference is smaller than a preset value, the bottleneck is qualified, if the absolute value of the difference is smaller than one tenth of the preset value, the bottleneck is a first-class bottleneck, and if the absolute value of the difference is larger than one tenth of the preset value and smaller than the preset value, the bottleneck is a second-class bottleneck.
The bottle mouth defect detection method has the advantages of high speed and high accuracy, and can meet the requirements on production lines.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
S101, performing threshold segmentation and feature extraction on the bottleneck image, and extracting a template area to be established;
the method comprises the steps of carrying out threshold segmentation and feature extraction on a bottle opening image, and carrying out denoising processing on the bottle opening image before extracting a template region to be established.
Image noise is a luminance distribution that interferes with the reception of a planar two-dimensional luminance distribution if the luminance distribution is visually received when information is transmitted from a subject or an information source to a viewer by some method. Image noise is typically quantified or described in terms of a signal-to-noise ratio.
The bottle opening image is subjected to denoising processing, so that the bottle opening image is improved, and the problem that the image quality is reduced due to noise interference of the bottle opening image is solved. The image quality can be effectively improved through denoising processing, the signal to noise ratio is increased, and information carried by the bottleneck image is better embodied.
And denoising the bottleneck image by adopting methods such as Gaussian filtering, median filtering and the like.
The threshold segmentation and feature extraction of the bottleneck image and the extraction of the template area to be established comprise the following steps:
performing global threshold segmentation on the bottleneck image, and extracting pixel points with gray values within a preset gray range;
performing connected region combination on the pixels, and extracting a maximum area region as an image to be processed according to the shape area characteristics of the connected regions;
and performing gray level closed operation on the image to be processed to be used as a template area to be established.
Image segmentation is an image segmentation algorithm that divides a digital image into non-overlapping regions and extracts an object of interest, and is generally based on one of two properties of gray scale: discontinuities and similarities. A first application of the property is to segment images based on discrete changes in gray scale. The main application of the second property is to segment images into similar regions according to a predetermined criterion.
Image segmentation is one of the most basic and important fields in image processing and low-level vision in the field of computer vision, and is a basic premise for performing visual analysis and pattern recognition on images. Image threshold segmentation is one of the methods, and image segmentation can also be understood as extracting meaningful feature regions or feature regions to be applied in an image, where the feature regions may be gray values of pixels, object contour curves, texture features, and the like, or may be a threshold segmentation technique such as spatial spectrum or histogram features. The basic principle of thresholding an image is described as follows: the difference of the gray characteristics of the target object to be extracted and the background thereof in the image is utilized, the image is regarded as the combination of two types of areas (target and background) with different gray levels, and a proper threshold value is selected to determine whether each pixel point in the image belongs to the target area or the background area, so that a corresponding binary image is generated.
The threshold segmentation is a simple and effective image segmentation method, and is particularly effective for image segmentation with strong contrast between an object and a background, all pixels with gray levels larger than or equal to a predetermined value are judged to belong to the object, the gray level value is 255 to represent the foreground, otherwise, the pixel points are excluded from the object area, and the gray level value is 0 to represent the background.
Wherein, the threshold segmentation of the bottleneck image includes but is not limited to: and carrying out global threshold segmentation on the bottleneck image, wherein the global threshold is realized by calculating the gray value of the wave crest in the bottleneck image and subtracting a preset gray value.
The method for carrying out threshold segmentation on the bottleneck image comprises a global threshold segmentation method and a threshold segmentation method such as self-adaptive threshold segmentation.
The global threshold segmentation method comprises a histogram bimodal method, and a maximum inter-class variance method and the like.
The communication area is an area G on the complex plane, and if a simple closed curve is made in any area G, the interior of the closed curve always belongs to G, and the G is called as a single communication area. A region is referred to as a multiply connected region if it is not a singly connected region.
Connectivity is an important concept describing areas and boundaries, including: requirements for 4-pass, 8-pass, m-pass, and two-pixel pass: 1. whether the two pixel positions are adjacent; 2. whether the two pixel gray values satisfy a certain similarity criterion.
(1)4, communication: two pixels p and q, if p is in the 4 neighborhood of q, the two pixels are said to be 4 connected;
(2)8, communication: two pixels p and q, if p is in the 8 neighborhood of q, the two pixels are said to be 8 connected;
(3) m is connected with: 1. two pixels p and q, p being in the 4 neighborhood of q, or p being in the D neighborhood of q, 2, and the intersection of the 4 neighborhoods of p and q being empty, i.e. m connectivity is a mixed (mixture) connectivity of 4 connectivity and D connectivity.
Each connected set in the bottleneck image forms a region of the bottleneck image, and a maximum area region is extracted as an image to be processed according to the shape area characteristics of the connected region;
and performing gray level closed operation on the image to be processed to be used as a template area to be established.
The process of expansion followed by erosion is called closed-loop operation, and is used to fill small voids in objects, connect neighboring objects, smooth their boundaries, and not significantly change their areas.
Erosion is a process by which boundary points are eliminated and the boundaries are shrunk inward. Can be used to eliminate small and meaningless objects. The algorithm of corrosion: with a 3 × 3 structure element, each pixel of the scanned image is anded with the structure element and its overlaid binary image if both are 1, resulting in that pixel of the image being 1. Otherwise it is 0. As a result: the binary image is reduced by one turn.
Dilation is the process of merging all background points in contact with an object into the object, expanding the boundary outward. Can be used to fill in voids in objects. The algorithm of inflation: with a 3 × 3 structuring element, each pixel of the scanned image is anded with the structuring element and the binary image it overlays if both are 0, resulting in that pixel of the image being 0. Otherwise it is 1. As a result: the binary image is enlarged by one turn.
And performing gray level closed operation on the image to be processed to be used as a template area to be established.
S102, generating a region to be cut according to the center coordinate of the region of the template to be established and the vertical coordinate of the highest point of the outline;
and averaging the row coordinates and the vertical coordinates of the pixel points to obtain the area center coordinates. And the point with the minimum vertical coordinate of the pixel point, namely the highest point of the outline, takes the central coordinate as the center to generate a rectangular area which is symmetrical by taking the bottle mouth as the center, namely the area to be cut.
S103, cutting the region to be cut out from the bottleneck image, and performing threshold segmentation on the region to be cut out to obtain a region area;
cutting the region to be cut out from the bottleneck image, namely separating the region to be cut out from the bottleneck image, so that the proportion of the part of the bottleneck in the image, which accounts for the image, becomes larger, and performing threshold segmentation on the region to be cut out to obtain the region area.
S104, when the bottle mouth image is a complete bottle mouth image, the area of the obtained area is used as an area standard;
fig. 2 is an image of a complete bottle mouth, and the area of the region obtained after the operations from S101 to S103 are performed on the image of the complete bottle mouth is used as an area standard, and the area standard is a constant for judging whether the bottle mouth is qualified.
S105, subtracting the area standard from the area of the area obtained by the bottleneck image, and comparing the absolute value of the difference obtained by subtracting with a preset value;
the bottleneck image comprises a complete bottleneck image and a damaged bottleneck image, the damaged bottleneck image is subjected to operations from S101 to S103 to obtain an area, the area of the area is subtracted from an area standard to obtain a difference value, and the difference value may be a negative number, so that an absolute value of the difference value is compared with a preset value.
S106, judging whether the absolute value of the difference value is larger than a preset value or not;
s107, if the absolute value of the difference is larger than a preset value, the bottle mouth is unqualified;
and S108, if the absolute value of the difference is not greater than a preset value, the bottle mouth is qualified.
The difference is not greater than a preset value, namely, under the condition that the bottleneck is qualified, the bottleneck can be divided into a plurality of grades, if the absolute value of the difference is smaller than the preset value, the bottleneck is qualified, if the absolute value of the difference is smaller than one tenth of the preset value, the bottleneck is a first-class bottleneck, and if the absolute value of the difference is larger than one tenth of the preset value and smaller than the preset value, the bottleneck is a second-class bottleneck.
The invention has the advantages of high speed and high accuracy for detecting the defects of the bottle mouth and can meet the requirements on the production line.
The above description is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, various modifications and substitutions can be made without departing from the technical principle of the present invention, and these modifications and substitutions should also be regarded as the protection scope of the present invention.