Disclosure of Invention
In order to solve the technical problems, the invention aims to provide a mobile phone shell production quality detection method based on machine vision, and the adopted technical scheme is as follows:
one embodiment of the invention provides a mobile phone shell production quality detection method based on machine vision, which comprises the following steps: preprocessing an image on the surface of the mobile phone shell to obtain a gray scale image, wherein the mobile phone shell comprises a camera hole; dividing the gray scale image into pixel blocks with preset sizes, and obtaining the contrast, entropy, energy and average value, variance, maximum value and minimum value of gray values of each pixel block; obtaining the comprehensive difference of every two pixel blocks by using the contrast, entropy and energy of every two pixel blocks and the average value, variance, maximum value and minimum value of the gray values;
classifying based on the comprehensive difference of every two pixel blocks to obtain pixel blocks of different classes; acquiring the number of pixel blocks contained in a camera hole area in a gray scale image; taking any pixel block as a target pixel block, wherein the category of the target pixel block is a first category of the target pixel block; obtaining a second category of the target pixel block according to the average value of the comprehensive differences of the pixel blocks in each category except the first category and the target pixel block; obtaining the suppression factor of the target pixel block according to the number of the pixel blocks in the first category, the number of the pixel blocks contained in the camera hole area and the number of all the pixel blocks; acquiring the first significance of the target pixel block by utilizing the comprehensive difference between the target pixel block and the pixel blocks in the first and second categories, and the suppression factor and the category number of the target pixel block;
the target pixel block and the pixel block in the neighborhood form a combined pixel block, and the second significance of the target pixel block is obtained according to the contrast, entropy and energy of the combined pixel block and the pixel block in the neighborhood; obtaining the significance of the target pixel block based on the first and second significances of the target pixel block; obtaining the significance of all pixel blocks, wherein the significance of the pixel blocks is the significance of each pixel point in the pixel blocks; obtaining the final significance of each pixel point in the gray-scale image by utilizing the significance of each pixel point under different preset sizes; and performing linear enhancement on the gray level image based on the final saliency of each pixel point, and detecting the gray level image subjected to linear enhancement to obtain the defect of the mobile phone shell.
Preferably, the obtaining of the comprehensive difference of each two pixel blocks by using the average, variance, maximum and minimum values of the contrast, entropy, energy and gray value of each two pixel blocks comprises: calculating the Euclidean distance of the contrast, entropy and energy of every two pixel blocks to obtain the texture difference of every two pixel blocks; calculating the difference value of the maximum value and the minimum value of the gray values of every two pixel blocks as the range of every two pixel blocks; calculating the mean value of the gray values of every two pixel blocks, the variance of the gray values and the Euclidean distance of the extreme difference to obtain the gray difference of every two pixel blocks; the average value of the sum of the gray difference and the texture difference of every two pixel blocks is the comprehensive difference of every two pixel blocks.
Preferably, obtaining the number of pixel blocks contained in the camera hole area in the grayscale map includes: carrying out edge detection on the gray-scale image by using a canny operator to obtain a closed edge in the gray-scale image, wherein the area included by the closed edge is a closed edge area, and the closed edge area with the largest area is a camera hole area; the number of pixel blocks contained within the camera hole area includes the number of pixel blocks entirely within the camera hole area and the number of pixel blocks of partial area within the camera hole area.
Preferably, the second category of the target pixel block is obtained according to an average value of the comprehensive differences of the pixel blocks in each category except the first category and the target pixel block; and obtaining the minimum value of the average value of the comprehensive differences between the pixel blocks in each category except the first category and the target pixel block, wherein the pixel block category in each category except the first category corresponding to the minimum value of the average value of the comprehensive differences is the second category of the target pixel block.
Preferably, obtaining the suppression factor of the target pixel block according to the number of pixel blocks in the first category, the number of pixel blocks contained in the camera hole area and the number of all pixel blocks comprises: obtaining a difference value between the number of pixel blocks in the first category and the number of pixel blocks contained in the camera hole area; and the ratio of the difference value to the number of all pixel blocks is a suppression factor of the target pixel block.
Preferably, the first significance is:
wherein,
representing a first saliency of a target block of pixels;
a number of classes representing a classification of the pixel block;
representing the number of pixel blocks in a first category of the target pixel block,
representing the integrated difference of the ith pixel block and the target pixel block in the first category;
representing the number of pixel blocks in the second category of the target pixel block,
representing the integrated difference of the jth pixel block and the target pixel block in the second category;
a suppression factor representing a target pixel block;
represents an adjustment coefficient;
an exponential function with a natural constant e as the base is shown.
Preferably, the step of obtaining the second significance of the target pixel block according to the contrast, entropy and energy of the combined pixel block and the pixel block in the neighborhood comprises: respectively combining the target pixel block and pixel blocks in four neighborhoods to obtain four combined pixel blocks, and obtaining the contrast, entropy and energy of each combined pixel block based on the gray level co-occurrence matrix; the second significance of the target pixel block is:
wherein,
representing a second saliency of the target block of pixels;
representing a target pixelThe entropy of the corresponding a-th block of combined pixels of the block,
expressing the entropy of the a-th pixel block in the four neighborhoods corresponding to the target pixel block;
representing the contrast of the a-th combined pixel block corresponding to the target pixel block,
representing the contrast of the a-th pixel block in the four neighborhoods corresponding to the target pixel block;
representing the energy of the a-th combined pixel block corresponding to the target pixel block,
and the energy of the a-th pixel block in the four neighborhoods corresponding to the target pixel block is represented.
Preferably, the saliency of the target pixel block is:
wherein,
representing the saliency of a target block of pixels;
representing a first saliency of a target block of pixels;
representing a second saliency of the target block of pixels;
is expressed with a natural constant e as the baseIs used as the exponential function of (1).
Preferably, the final saliency of each pixel point is as follows:
wherein N represents the final significance of the pixel points;
indicating the number of preset size changes, i.e. in common
Each preset size;
and representing the significance of the pixel point under the mth preset size.
Preferably, the linear enhancement of the gray scale image based on the final saliency of each pixel point, and the detection of the gray scale image after the linear enhancement to obtain the defect of the mobile phone shell comprises: for a pixel point in the gray-scale image, adding the final significance of the pixel point and a first preset value and multiplying the result by the gray value of the pixel point to obtain the gray value of the pixel point after linear enhancement; obtaining gray values of all pixel points in the gray image after linear enhancement, thereby obtaining a gray image after linear enhancement; and analyzing the linearly enhanced gray level image by utilizing edge detection and Hough circle detection, wherein the detected edge line which is not the edge of the mobile phone shell is a scratch defect, and the detected circle is a pit defect.
The embodiment of the invention at least has the following beneficial effects: obtaining a gray scale image of the surface image of the mobile phone shell, dividing the gray scale image into a plurality of pixel blocks, and obtaining the comprehensive difference of every two pixel blocks based on the texture information of each pixel block and the gray scale information; the information of the pixel blocks is comprehensively considered, so that the obtained comprehensive difference is more accurate; the improved CA significance analysis model is further utilized to combine the texture change condition of the combined pixel block after the target pixel block is combined with the surrounding pixel blocks to obtain the significance of the target pixel block, so that the obtained significance is more accurate; and finally, the final significance of each pixel point of the gray scale image is obtained according to the significance of each pixel point of the gray scale image under different preset sizes, and the gray scale image is linearly enhanced based on the final significance of each pixel point, so that the defect part of the gray scale image is more significant, the difficulty in defect detection of the opponent shell is reduced, and the accuracy in defect detection of the opponent shell is improved.
Detailed Description
In order to further illustrate the technical means and effects of the present invention adopted to achieve the predetermined objects, the following detailed description of the method for detecting the production quality of the mobile phone shell based on the machine vision, the specific implementation manner, the structure, the features and the effects thereof according to the present invention are provided with the accompanying drawings and the preferred embodiments. In the following description, different "one embodiment" or "another embodiment" refers to not necessarily the same embodiment. Furthermore, the particular features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs.
The following describes a specific scheme of the mobile phone shell production quality detection method based on machine vision in detail with reference to the accompanying drawings.
Example (b):
the main application scenarios of the invention are as follows: in order to prevent the defects of scratches and pits of the produced mobile phone shells, the mobile phone shells produced on the production line are detected, the mobile phone shells are arranged on a conveyor belt, a camera is arranged right above the conveyor belt, meanwhile, the shooting frequency is related to the speed of the conveyor belt, each mobile phone shell is required to be arranged in the middle of an image, and it needs to be noted that the mobile phone shells do not have textures and colors.
Referring to fig. 1, a flowchart of a method for detecting a production quality of a mobile phone case based on machine vision according to an embodiment of the present invention is shown, where the method includes the following steps:
s1, preprocessing an image of the surface of a mobile phone shell to obtain a gray scale image, wherein the mobile phone shell comprises a camera hole; dividing the gray scale image into pixel blocks with preset sizes, and obtaining the contrast, entropy, energy and average value, variance, maximum value and minimum value of gray values of each pixel block; and obtaining the comprehensive difference of every two pixel blocks by using the contrast, entropy and energy of every two pixel blocks and the average value, variance, maximum value and minimum value of the gray values.
Firstly, it can be known a priori that some defects on the surface of the mobile phone shell have obvious characteristics, while some defects with small scratches and pits have unobvious characteristics, and the defects with unobvious characteristics are not easy to detect, so that the problem of missed detection can occur, and therefore, when the defects of the mobile phone shell are identified, the image on the surface of the mobile phone shell needs to be optimized and enhanced; in the embodiment, a saliency analysis algorithm is utilized to highlight the defect area in the surface image of the mobile phone shell so as to more accurately identify the defect.
In a plurality of significance detection models, the CA significance model considers low-level factors such as gray values, contrast and the like, also considers occurrence frequency and spatial distribution of pixel blocks, and is very consistent with the scene aimed by the invention.
Further, a surface image of the mobile phone shell is obtained, wherein the mobile phone shell is located in the middle of the surface image of the mobile phone shell, the surface image of the mobile phone shell is preprocessed by a Gaussian denoising method, the preprocessed surface image of the mobile phone shell is grayed, and a gray level image is obtained, wherein the graying uses a weighted average value method.
For more convenient analysis and better extraction of local information, the grayscale image needs to be subjected to blocking processing, wherein the image blocking method uses a superpixel segmentation method to divide the grayscale image into pixel blocks of a preset size, wherein the preset size of the first division of the grayscale image is M × M, preferably, the value of M in this embodiment is 20, and it needs to be noted that the value of M needs to be determined by an implementer according to an actual situation; it should be further explained that when the saliency of the pixel point is obtained, the gray level map needs to be divided under different preset sizes, the saliency of each pixel point under each preset size is obtained, and then the saliency of the pixel point is comprehensively obtained.
Then, the main role of the saliency analysis is to highlight the most salient regions in the image, i.e. the regions that are least similar to other regions, so that the similarity between the acquired pixel blocks and other pixel blocks needs to be found.
Obtaining a gray level co-occurrence matrix of each pixel block under a preset size, and obtaining the contrast of each pixel block according to the gray level co-occurrence matrix of each pixel block
Entropy of
And energy
Wherein the contrast ratio
Groove depth and entropy reflecting definition and texture of pixel block
Reflects the complexity of the pixel blockMeasurement of
The uniformity degree of the gray distribution of the pixel blocks is reflected; calculating Euclidean distances of contrast, entropy and energy of every two pixel blocks to obtain texture difference of every two pixel blocks:
wherein,
、
、
and
、
、
the contrast, entropy and energy of two pixel blocks are respectively, when the contrast, entropy and energy of two pixel blocks are more close, namely the texture difference
Smaller values of (a) indicate more similarity of two pixel blocks, whereas the difference is larger.
Secondly, the difference degree of the two pixel blocks is judged according to the information reflected by the gray values of the pixel blocks, and the average value, the variance, the maximum value and the minimum value of the gray values of each pixel block are obtained, wherein the difference value of the maximum value and the minimum value of the gray values of the pixel blocks is the extreme difference of the pixel blocks; calculating the mean value of the gray values of every two pixel blocks, the variance of the gray values and the Euclidean distance of the range difference to obtain the gray difference of every two pixel blocks:
wherein,
representing the difference in gray levels for every two blocks of pixels,
、
、
and
、
、
the mean value of the gray values of the two pixel blocks, the variance of the gray values and the range are respectively. The smaller the gray difference w of the two pixel blocks is, the more similar the two pixel blocks are, otherwise, the larger the difference of the two pixel blocks is.
And finally, comprehensively analyzing the texture difference and the gray difference of every two pixel blocks to obtain the comprehensive difference of every two pixel blocks:
the larger the comprehensive difference is, the larger the difference between the two pixel blocks is, and here, the difference between the pixel blocks is analyzed from the texture information and the gray information of the pixel blocks to obtain the comprehensive difference between the two pixel blocks, so that the accuracy of the two pixel blocks can be more accurately obtained. Whereby an integrated difference of every two pixel blocks can be obtained.
S2, classifying based on the comprehensive difference of every two pixel blocks to obtain pixel blocks of different categories; acquiring the number of pixel blocks contained in a camera hole area in a gray scale image; taking any pixel block as a target pixel block, wherein the category of the target pixel block is a first category of the target pixel block; obtaining a second category of the target pixel block according to the average value of the comprehensive differences of the pixel blocks in each category except the first category and the target pixel block; obtaining the suppression factor of the target pixel block according to the number of the pixel blocks in the first category, the number of the pixel blocks contained in the camera hole area and the number of all the pixel blocks; and acquiring the first significance of the target pixel block by utilizing the comprehensive difference between the target pixel block and the pixel blocks in the first and second categories, the suppression factor of the target pixel block and the number of the categories.
Firstly, the pixel blocks are classified according to the comprehensive difference between every two pixel blocks, in this embodiment, the DBSCAN clustering algorithm is used for classifying, pixel blocks of different categories are obtained, the number of the classified categories is obtained, and when the number of the categories is more, the significance of the pixel blocks in the gray map is more.
Meanwhile, a camera hole area in the gray-scale image is required to be obtained, edge detection is carried out on the gray-scale image by using a canny operator to obtain a closed edge in the gray-scale image, the area included by the closed edge is a closed edge area, and the closed edge area with the largest area is the camera hole area; the method comprises the steps of obtaining the number of pixel blocks contained in a camera hole area, wherein the number of the pixel blocks contained in the camera hole area comprises the number of the pixel blocks completely in the camera hole area and the number of the pixel blocks in a partial area in the camera hole area, the camera hole is more prominent than the mobile phone shell, and when significance analysis is carried out, the camera hole may acquire larger significance.
Further, any pixel block is selected as a target pixel block, when the difference between the target pixel block and the most similar pixel block is larger, the significance of the target pixel block is very large, the category to which the target pixel block belongs is obtained, and the category is marked as the first category of the target pixel block; the method comprises the steps of obtaining the minimum value of the average value of the comprehensive difference between a pixel block in each category except the first category and a target pixel block, wherein the pixel block category in each category except the first category corresponding to the minimum value of the average value of the comprehensive difference is the second category of the target pixel block, selecting the target pixel blocks of two categories with larger similarity degree with the target pixel block to analyze the significance of the target pixel block, and an implementer can select different numbers of categories to analyze according to actual conditions during implementation.
Obtaining the suppression factor of the target pixel block according to the number of the pixel blocks in the first category, the number of pixel blocks contained in the camera hole area and the number of all the pixel blocks:
wherein,
the suppression factors of the target pixel blocks are represented, when different pixel blocks are used as the target pixel blocks, the suppression factors are different, but when the pixel blocks belonging to the same class are used as the suppression factors, the values of the pixel blocks are the same;
representing the number of pixel blocks contained in the camera aperture region;
representing a number of pixel blocks within a first category of the target pixel block;
representing the number of pixel blocks in the grayscale map.
And finally, acquiring the first significance of the target pixel block by utilizing the comprehensive difference between the target pixel block and the pixel blocks in the first and second categories, the suppression factor of the target pixel block and the number of the categories:
wherein,
representing a first saliency of a target block of pixels;
a number of classes representing a classification of the pixel block;
representing the number of pixel blocks in a first category of the target pixel block,
representing the integrated difference of the ith pixel block and the target pixel block in the first category;
representing the number of pixel blocks in the second category of the target pixel block,
representing the integrated difference of the jth pixel block and the target pixel block in the second category;
when the target pixel block is a pixel block in the camera hole area, the majority of the pixel blocks in the first category to which the target pixel block belongs are the pixel blocks in the camera hole area, and the suppression factor is a number which is wireless and close to 0, so that the saliency of the target pixel block is reduced, the effect of suppressing the saliency of the camera hole area is achieved, and otherwise, the saliency of the pixel block is not suppressed, for example, the target pixel block is a pixel block in the camera hole areaWhen the target pixel block is a normal pixel block without defects, the number of the pixel blocks in the first category to which the target pixel block belongs is relatively large, and the significance of the target pixel block is also inhibited when the suppression factor is a negative value;
represents an adjustment coefficient whose value is a value infinitely close to 0 but not 0;
representing an exponential function with a natural constant e as the base.
Indicating that the target pixel block is more significant when the pixel blocks in the first and second categories of the target pixel block have greater comprehensive difference with the target pixel block; meanwhile, a larger value of the first saliency indicates a larger saliency of the target pixel block. The first saliency of each block of pixels can be obtained so far.
S3, forming a combined pixel block by the target pixel block and the pixel block in the neighborhood, and obtaining the second significance of the target pixel block according to the contrast, entropy and energy of the combined pixel block and the pixel block in the neighborhood; obtaining the significance of the target pixel block based on the first and second significances of the target pixel block; obtaining the significance of all pixel blocks, wherein the significance of the pixel blocks is the significance of each pixel point in the pixel blocks; obtaining the final significance of each pixel point in the gray-scale image by utilizing the significance of each pixel point under different preset sizes; and performing linear enhancement on the gray level image based on the final saliency of each pixel point, and detecting the gray level image subjected to linear enhancement to obtain the defect of the mobile phone shell.
First, in step S2, the saliency of the pixel block is analyzed in combination with the texture information and the grayscale information of the pixel block, and the saliency of the target pixel block needs to be analyzed by the pixel blocks in the neighborhood of the target pixel block. Obtaining pixel blocks in the four neighborhoods of the target pixel block, combining the pixel blocks with the target pixel block to form a combined pixel block, obtaining the significance of the target pixel block according to the combined pixel block corresponding to the four neighborhoods of the target pixel block and the corresponding pixel block, and marking as the second significance of the target pixel block:
wherein,
representing a second saliency of the target block of pixels;
representing the entropy of the a-th combined pixel block corresponding to the target pixel block,
expressing the entropy of the a-th pixel block in the four neighborhoods corresponding to the target pixel block;
representing the contrast of the a-th combined pixel block corresponding to the target pixel block,
representing the contrast of the a-th pixel block in the four neighborhoods corresponding to the target pixel block;
representing the energy of the a-th combined pixel block corresponding to the target pixel block,
and the energy of the a-th pixel block in the four neighborhoods corresponding to the target pixel block is represented.
The significance of the target pixel block is relatively large when the contrast, entropy and energy of the combined pixel block in the four-neighborhood of the target pixel block and the pixel blocks in the four-neighborhood are obviously changed.
Further, the saliency of the target pixel block is obtained:
the significance of the target pixel block is:
wherein,
representing the saliency of a target block of pixels;
representing a first saliency of a target block of pixels;
representing a second saliency of the target block of pixels;
an exponential function with a natural constant e as the base is shown.
Therefore, the significance of each pixel block with the preset size of M × M can be obtained, wherein it needs to be described that the significance of each pixel block is also the significance of a pixel point in the pixel block, in order to more accurately obtain the significance of each pixel point in the gray-scale map, the significance of the pixel point in the gray-scale map needs to be comprehensively analyzed by using a plurality of pixel blocks with the preset size, in this embodiment, the gray-scale map is divided five times in total, that is, five different preset sizes are provided, the size of the pixel block obtained by dividing each time is M/M, wherein M represents the number of dividing times, therefore, the preset size of the pixel block obtained by dividing the first time is 20 × 20, the preset size of the pixel block obtained by dividing the second time is 10 × 10, and by analogy, the preset size of the pixel block obtained by dividing the fifth time is 4*4. Obtaining the final significance of the gray image pixel points according to the significance of the pixel points under each preset size:
wherein N represents the final significance of the pixel points;
the number of times of the preset size conversion is shown, in the embodiment, the value is 5, and in the specific implementation process, the implementer adjusts according to the actual situation;
and representing the significance of the pixel point under the mth preset size.
Therefore, the final significance of each pixel point in the gray-scale image can be obtained.
Finally, after the final saliency of each pixel point in the gray-scale image is obtained, the gray-scale image is subjected to linear enhancement based on the final saliency of the pixel point so that the defect part can be more prominently displayed, and the detection is facilitated; the linear enhancement is formulated as:
wherein g represents the gray value of the pixel point before linear enhancement, XX represents the gray value of the pixel point after linear enhancement, and N represents the final significance of the pixel point.
And analyzing the linearly enhanced gray level image by utilizing edge detection and Hough circle detection, wherein the detected edge line which is not the edge of the mobile phone shell is a scratch defect, and the detected circle is a pit defect.
It should be noted that: the precedence order of the above embodiments of the present invention is only for description, and does not represent the merits of the embodiments. And specific embodiments thereof have been described above. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing may also be possible or may be advantageous.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like that fall within the spirit and principle of the present invention are intended to be included therein.