Disclosure of Invention
The invention provides a metal surface defect detection method, which solves the problem that detection precision is low due to errors in gray value division, and adopts the following technical scheme:
acquiring a suspected abnormal gray value and a suspected normal gray value in an image of the metal surface to be detected;
acquiring a difference value between each suspected abnormal gray value and the average value of the suspected normal gray values, and selecting a pixel point corresponding to the suspected abnormal gray value with the largest difference value as a suspected abnormal pixel point;
taking each suspected abnormal pixel point as a center, and acquiring the number and the gray value of the suspected abnormal pixel points of the abnormal pixel point in a window area with each size;
obtaining the abnormal degree of the central suspected abnormal pixel points according to the number and the gray value of the suspected abnormal pixel points in the window area with each size;
obtaining a suspected abnormal area in the metal surface image to be detected according to the abnormal degree and the abnormal degree threshold of each suspected abnormal pixel point;
correcting the suspected abnormal gray value in the metal surface image to be detected by using the suspected abnormal area to obtain the accurate abnormal gray value in the metal surface image to be detected;
and performing characteristic enhancement on the accurate abnormal gray value in the metal surface image to be detected.
The method for acquiring the suspected abnormal gray value and the suspected normal gray value in the metal surface image to be detected comprises the following steps:
acquiring a gray level histogram of a metal surface image to be detected;
acquiring the occurrence probability of each gray value in the gray histogram, and performing K-means clustering on the occurrence probability of each gray value in the gray histogram;
and taking the gray value class with the small sum of the probability values obtained after clustering as a suspected abnormal gray value, and taking the gray value class with the large sum of the probability values as a suspected normal gray value.
The method for acquiring the abnormal degree of each suspected abnormal pixel point comprises the following steps:
selecting a suspected abnormal pixel point;
taking the suspected abnormal pixel points as the center, and sequentially counting the sizes as
、
、
The number of suspected abnormal pixel points contained in the window, wherein,
;
the abnormal degree of each suspected abnormal pixel point is as follows:
in the formula (I), the compound is shown in the specification,
for the position in the image of the metal surface
Is suspected to be abnormal pixel point
The degree of abnormality of (a) is,
is as follows
The number of rows is such that,
is as follows
The columns of the image data are,
is of the size of
The number of suspected abnormal pixel points in the window of (1),
is of size
The number of suspected abnormal pixel points in the window of (1),
is of size
The number of suspected abnormal pixel points in the window of (1),
is of size
In the window of
Each suspected abnormal pixel point is selected from the group of abnormal pixel points,
the total number of suspected abnormal pixel points in the window,
is of size
In the window of
The number of the suspected abnormal pixel points is,
the total number of suspected abnormal pixel points in the window,
is of size
In the window of (1)
The number of the suspected abnormal pixel points is,
the total number of suspected abnormal pixel points in the window,
is the average of the suspected normal gray-scale values,
is composed of
The weight of the window is calculated based on the weight,
is composed of
The weight of the window is calculated based on the weight,
is composed of
The weight of the window.
The method for obtaining the suspected abnormal area in the metal surface image to be detected according to the abnormal degree and the abnormal degree threshold value of each suspected abnormal pixel point comprises the following steps:
if the abnormal degree of the suspected abnormal pixel point is larger than the abnormal degree threshold value, the suspected abnormal pixel point is taken as the center, and the size is
The window area is a suspected abnormal area in the metal surface image to be detected; otherwise it is not
The window area of (a) is a suspected normal area in the metal surface image.
In the suspected abnormal areas in the metal surface image to be detected, if the distance between two adjacent suspected abnormal areas is smaller than a distance threshold, combining the two adjacent suspected abnormal areas with the distance smaller than the distance threshold.
The distance threshold value obtaining method comprises the following steps:
if the size of two adjacent suspected abnormal areas is
If the distance threshold of two adjacent suspected abnormal areas is
。
The method for correcting the suspected abnormal gray value in the metal surface image to be detected by using the suspected abnormal area to obtain the accurate abnormal gray value in the metal surface image to be detected comprises the following steps:
acquiring a suspected abnormal gray value set P in a metal surface image to be detected;
acquiring a gray value set Q in a suspected abnormal area in a metal surface image to be detected;
and taking the gray value belonging to the gray value set Q in the gray value P as an accurate abnormal gray value in the metal surface image to be detected, and taking other gray values as accurate normal gray values.
The method for performing feature enhancement on the accurate abnormal gray value in the metal surface image to be detected comprises the following steps:
constructing a piecewise linear transformation function according to the minimum gray value in the accurate normal gray values, the maximum gray value in the accurate abnormal gray values, the accurate normal gray value mean value and the accurate abnormal gray value mean value:
the piecewise linear transformation function for constructing the abnormal gray value is as follows:
in the formula (I), the compound is shown in the specification,
for the exact transformed gray values of the abnormal gray values,
as a function of the number of the coefficients,
for an accurate average of the abnormal gray values,
the gray scale value is the original abnormal gray scale value,
has a value of
,
The minimum gray value among the accurate normal gray values,
the maximum gray value in the accurate abnormal gray values;
the piecewise linear transformation function for constructing the normal gray value is:
in the formula (I), the compound is shown in the specification,
for an accurate normal gray value transformed gray value,
as a function of the number of the coefficients,
is an accurate average of the normal gray values,
is the original normal gray-scale value of the image,
has a value of
;
And enhancing the accurate abnormal gray value in the metal surface image to be detected by using the segmented linear transformation function of the abnormal gray value, and inhibiting the accurate normal gray value in the metal surface image to be detected by using the segmented linear transformation function of the normal gray value.
The beneficial effects of the invention are:
(1) The method realizes the preliminary division of the gray value by analyzing the gray distribution probability in the gray histogram of the metal surface image and clustering the distribution probability to divide the gray value into a suspected normal gray value and a suspected abnormal gray value;
(2) The method comprises the steps of obtaining the abnormal degree of each suspected abnormal pixel position through the number and the gray of suspected abnormal pixel points around the suspected abnormal gray pixel points, obtaining abnormal areas and final abnormal gray values and normal gray values according to the abnormal degree, and carrying out secondary analysis on gray results of primary division further based on the pixel points of each suspected abnormal gray value in consideration of errors possibly caused by illumination or clustering in the primary division to obtain accurate gray value classification, so that the judgment accuracy of the normal gray values and the abnormal gray values is improved;
(3) The accurate defect region is obtained according to the accurate abnormal gray value and the normal gray value, and the characteristic enhancement is carried out on the accurate defect region.
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 obtained by a person skilled in the art without making any creative effort based on the embodiments in the present invention, belong to the protection scope of the present invention.
An embodiment of a method for detecting defects on a metal surface according to the present invention is shown in fig. 1:
the method comprises the following steps: acquiring a suspected abnormal gray value and a suspected normal gray value in an image of the metal surface to be detected;
the method comprises the steps of collecting an image of the metal surface to be detected, carrying out gray processing on the image to obtain a gray histogram of the metal surface to be detected, obtaining the probability of each gray value according to the gray values and the number of pixel points in the gray histogram, and dividing the gray values according to the probability to obtain suspected abnormal gray values and suspected normal gray values.
In the embodiment, a camera is firstly arranged to collect the image of the metal surface to be detected, then the image of the metal surface is grayed to obtain the gray level image of the metal surface to be detected, and finally the gray level histogram corresponding to the gray level image of the metal surface to be detected is obtained.
The method for acquiring the suspected abnormal gray value and the suspected normal gray value in the metal surface image to be detected comprises the following steps:
(1) Acquiring a gray level histogram of a metal surface image;
the gray histogram is the probability of occurrence of each gray value in the statistical gray map, and due to the particularity of the metal image, the gray values in the image are very close and the number of pixels under each gray value, namely the probability, is similar, so that the spatial quotient presents an aggregation state on data. If there is a defect in the image, the defect appears as a gray value with a small occurrence probability on the gray histogram, because the range of the defect is generally small, and therefore the corresponding probability value is small.
(2) Acquiring the occurrence probability of each gray value in the gray histogram, and performing K-means clustering on the occurrence probability of each gray value in the gray histogram;
according to the analysis, the probability value corresponding to the normal gray level of the metal surface image is very large, and the probability value corresponding to the gray level of the defect part is very small, so that the final histogram data can be segmented through a K-means clustering algorithm to obtain two groups of data;
(3) Taking a class of gray values with small sum of the probability values obtained after clustering as suspected abnormal gray values, and taking a class of gray values with large sum of the probability values as suspected normal gray values;
the K-means clustering algorithm can only segment data but cannot distinguish data types, because the probability value of normal data is far greater than that of abnormal data, a gray value with a high probability value sum is used as a suspected normal pixel point, a gray value with a low probability value sum is used as a suspected abnormal gray value in two groups of clustered data, and because the probability value of the normal data is greater than that of the abnormal data, the types of the gray values can be distinguished through comparison of the two groups of data.
Step two: acquiring a difference value between each suspected abnormal gray value and the average value of the suspected normal gray values, and selecting a pixel point corresponding to the suspected abnormal gray value with the largest difference value as a suspected abnormal pixel point; taking each suspected abnormal pixel point as a center, and acquiring the number and the gray value of the suspected abnormal pixel points of the abnormal pixel point in a window area with each size; obtaining the abnormal degree of the central suspected abnormal pixel point according to the number and the gray value of the suspected abnormal pixel points in the window area with each size;
the step aims to analyze a gray value which is possibly abnormal, namely a suspected abnormal gray value, select the suspected abnormal gray value with the largest difference according to the difference between the abnormal gray value and the average value of normal gray values, and calculate the abnormal degree of each suspected abnormal pixel point by using the number and the gray value of the suspected abnormal pixel points in different windows;
the method for obtaining the difference value between each suspected abnormal gray value and the average value of the suspected normal gray values and selecting the pixel point corresponding to the suspected abnormal gray value with the largest difference value as the suspected abnormal pixel point comprises the following steps:
(1) Calculating the average value of all suspected normal gray values
;
(2) Set the abnormal gray value as
Denotes the first
Each suspected abnormal gray value;
(3) Calculating the sum of each abnormal gray value
Difference of (2)
;
Is shown as
The difference between the average value of the suspected abnormal gray value and the average value of the suspected normal gray value is larger, the larger the difference is, the larger the abnormal degree of the gray value is, the more likely the corresponding gray value is to be a defect gray value, and the more likely the pixel point corresponding to the gray value is to be a defect pixel point;
(4) Selecting and
the suspected abnormal gray value with the maximum difference is compared with
And taking the pixel point corresponding to the suspected abnormal gray value with the largest difference value as a suspected abnormal pixel point, and performing subsequent abnormal judgment.
The meaning of the method is to determine the degree of abnormality of each gray value in the combination of abnormal gray values by calculating the difference between the abnormal gray value and the normal gray value.
It should be noted that, in the step one, the suspected normal pixel points and the suspected abnormal pixel points in the image are distinguished according to the clustering characteristics, and in the clustering result, the determination of the abnormal pixel points cannot determine that the image is abnormal, which may be caused by illumination or clustering errors, and if the image has defects, the clustered abnormal pixel points present an aggregation state in space, and the suspected abnormal gray value obtained by clustering is further analyzed and determined based on the above analysis, so as to determine whether the suspected abnormal gray value is an abnormal gray value.
The method for obtaining the abnormal degree of the central suspected abnormal pixel point according to the number and the gray value of the suspected abnormal pixel points in the window area with each size comprises the following steps:
(1) Selecting each suspected abnormal pixel point; in the embodiment, the gray value of the suspected abnormality
Corresponding toSelecting abnormal pixel points from all suspected abnormal pixel points
I.e. the position in the image of the metal surface
The suspected abnormal pixel point of (c) is detected,
is a line of the image that is,
is a column of the image;
(2) Taking the suspected abnormal pixel points as the center, and sequentially counting the sizes as
、
、
The number of suspected abnormal pixel points contained in the window, wherein,
;
for the selection of three windows, a small area such as a window may be formed due to the wide variety of metal defects
Is normal in the window of (A), and
or
Is abnormal, and therefore only judges
The abnormal degree of the window size is not reasonable, but the window size is too large, so that certain noise is counted and judgment is wrong, and therefore the embodiment selects the windows with three sizes for counting, namely
、
、
And to ensure accuracy, when the final result is obtained, it will be right
The window is subjected to abnormity verification;
after the window size is selected, the step is firstly counted
Counting the number of abnormal pixel points in the window
Counting the number of abnormal pixel points in the window, and finally counting
The number of suspected abnormal pixel points in the window;
(3) The abnormal degree of each suspected abnormal pixel point is as follows:
in the formula (I), the compound is shown in the specification,
for the position in the image of the metal surface
Suspected abnormal pixel point of
The degree of abnormality of (a) is,
is as follows
The rows of the image data are, in turn,
is a first
The columns of the image data are,
is of the size of
The number of suspected abnormal pixel points in the window of (1),
is of size
The number of suspected abnormal pixel points in the window of (1),
is of the size of
The number of suspected abnormal pixel points in the window of (1),
is of the size of
In the window of
The number of the suspected abnormal pixel points is,
the total number of suspected abnormal pixel points in the window,
is of size
In the window of
The number of the suspected abnormal pixel points is,
the total number of suspected abnormal pixel points in the window,
is of size
In the window of
The number of the suspected abnormal pixel points is,
the total number of suspected abnormal pixel points in the window,
is the average of the normal gray-scale values,
to represent
The weight of (a) is calculated,
represent
The weight of (a) is calculated,
is composed of
The weight of the window;
in the present embodiment, the first and second electrodes are,
the value of (A) is 1/2,
the value of (A) is 1/5,
the value of (1) is 3/10, the sum of the weights is 1, three windows in a calculation formula of the abnormal degree of each suspected abnormal pixel point are different in size, if the window is smaller, the number of the abnormal pixel points is more, the abnormal degree is larger, a larger weight is required to be given, and the formula has the meaning that the current pixel point is used as the center
、
、
Judging the abnormal condition of the position of the current suspected abnormal pixel point according to the number of the abnormal pixel points in the window and the difference of the gray values, if only one suspected abnormal pixel point is present, namely the selected central suspected abnormal pixel point, judging that the position of the current suspected abnormal pixel point is abnormal
0, the abnormal degree of the suspected abnormal pixel point
。
Step three: obtaining a suspected abnormal area in the metal surface image to be detected according to the abnormal degree and the abnormal degree threshold of each suspected abnormal pixel point;
the method comprises the steps of determining a suspected abnormal area and a suspected normal area in a gray level image of the metal surface according to the abnormal degree of suspected abnormal pixel points, and combining adjacent suspected abnormal areas according to the distance between the adjacent suspected abnormal areas to obtain a combined suspected abnormal area.
The method for obtaining the suspected abnormal area and the suspected normal area in the metal surface image to be detected according to the abnormal degree and the abnormal degree threshold of each suspected abnormal pixel point comprises the following steps:
if the abnormal degree of the suspected abnormal pixel point is larger than the abnormal degree threshold value, the suspected abnormal pixel point is taken as the center, and the size is
The window area of (2) is a suspected abnormal area in the metal surface image; otherwise it is not
The window area of (2) is a suspected normal area in the metal surface image, and the abnormal degree threshold value in the embodiment
Is 10.
In the present embodiment, according to each
Obtained with pixel points as centres
And (4) calculating the abnormity of the window to judge whether the area is abnormal, and if all the areas are normal, indicating that the image is not abnormal. If the abnormality exists in a certain area, the metal is indicated to have defects, and subsequent processing is carried out;
further, calculating the distance between the center points of any two adjacent suspected abnormal areas, and if the distance is smaller than a distance threshold, merging the two adjacent suspected abnormal areas to obtain a merged suspected abnormal area;
the distance threshold value obtaining method comprises the following steps: if the size of two adjacent suspected abnormal areas is
If the distance threshold of two adjacent suspected abnormal areas is
。
The specific combination method is as follows:
since the plurality of suspected abnormal areas are close in spatial position, the plurality of suspected abnormal areas need to be merged to determine the position of the defect area, and the suspected abnormal area determined by the suspected abnormal pixel point with the maximum abnormal degree is taken as a reference area, which is assumed to be
And the suspected abnormal area determined by the residual suspected abnormal pixel points is assumed to be
I.e. the position in the image of the metal surface
Suspected abnormal pixel points are located;
computing
And
spatial positional relationship between the regions, i.e., distance therebetween:
in the above-mentioned formula, the compound has the following structure,
representing the spatial distance between two suspected abnormality areas,
and
respectively represent
And
when the two are close to each other, the two suspected abnormal areas jointly form an abnormal area, the distance between every two suspected abnormal areas is sequentially and circularly calculated, and the distance between every two suspected abnormal areas is set as
It indicates that the two suspected abnormal areas are close,
is a distance threshold;
the distance threshold value obtaining method comprises the following steps:
if the size of two adjacent suspected abnormal areas is
If the distance threshold of two adjacent suspected abnormal areas is
Since the size of each suspected abnormal area in the present embodiment is the size
Window size of (i.e.
So that the size of each suspected abnormal area is
If the distance threshold of two adjacent suspected abnormal areas is
;
Fig. 2 shows two adjacent suspected abnormal areas with a distance smaller than the distance threshold, fig. 3 shows two adjacent suspected abnormal areas with a distance larger than the distance threshold, and fig. 4 shows a merged suspected abnormal area of the two adjacent suspected abnormal areas with a distance smaller than the distance threshold in fig. 2;
when an overlapping area exists between two suspected abnormal areas, the two suspected abnormal areas are considered as the same suspected abnormal area, and the maximum distance (distance threshold) between the two windows when the two suspected abnormal areas are overlapped is as follows:
in the above formula, the first and second carbon atoms are,
in order to judge the space distance threshold between two suspected abnormal areas, the maximum distance of the overlapping area is obtained according to the size of the window
Then when
Think of bothThe fused suspected abnormal area in fig. 4 is obtained for the same suspected abnormal area.
Step four: correcting the suspected abnormal gray value in the metal surface image to be detected by using the suspected abnormal area to obtain the accurate abnormal gray value in the metal surface image to be detected; and performing characteristic enhancement on the accurate abnormal gray value in the metal surface image to be detected.
The step aims to correct the suspected abnormal gray value in the metal surface image to be detected to obtain the accurate abnormal gray value in the metal surface image to be detected, restrain the normal gray value in the metal surface image by utilizing the piecewise linear transformation function, and perform characteristic enhancement on the abnormal gray value in the metal surface image to enable the defect to be more obvious.
The method for correcting the suspected abnormal gray value in the metal surface image to be detected by using the suspected abnormal area to obtain the accurate abnormal gray value in the metal surface image to be detected comprises the following steps:
acquiring a suspected abnormal gray value set P in a metal surface image to be detected;
acquiring a gray value set Q in a suspected abnormal area in a metal surface image to be detected;
and taking the gray value belonging to the gray value set Q in the gray value P as an accurate abnormal gray value in the metal surface image to be detected, and taking the other gray values as accurate normal gray values.
In the embodiment, the suspected abnormal gray value contained in the merged suspected abnormal area in the metal surface image to be detected is used as the accurate abnormal gray value, other gray values are used as the accurate normal gray value, and connected domain analysis can be performed according to the pixel point corresponding to the accurate abnormal gray value and the pixel point corresponding to the accurate normal gray value in the metal surface image to be detected to obtain the defect area and the normal area.
The piecewise linear transformation can perform different gray enhancement or suppression on different segments according to needs, and the embodiment suppresses normal gray values and enhances abnormal gray values according to the two gray data obtained in the above steps, so that the gray values of the two are greatly different, and the defect characteristics are more obvious.
The method for performing feature enhancement on the accurate abnormal gray value in the metal surface image to be detected comprises the following steps:
it should be noted that the abnormal gray value refers to an accurate abnormal gray value obtained, and the normal gray value refers to an accurate normal gray value obtained;
(1) The piecewise linear transformation function for constructing the abnormal gray value is as follows:
in the formula (I), the compound is shown in the specification,
is the gray value after the abnormal gray value transformation,
as a function of the number of the coefficients,
is the average value of the abnormal gray-scale values,
the gray scale value is the original abnormal gray scale value,
has a value of
,
Is the minimum gray value among the normal gray values,
is the maximum gray value among the abnormal gray values;
(2) The piecewise linear transformation function for constructing the normal gray value is:
in the formula (I), the compound is shown in the specification,
is a gray value after the normal gray value is transformed,
as a function of the number of the coefficients,
is the average value of the normal gray-scale values,
the gray level value is the original normal gray level value,
has a value of
;
The final purpose of the linear gray-scale transformation is to make the gray-scale values of two sets of data have large difference and highlight the characteristics of the defect part, the coefficients a and m of the linear transformation determine the magnitude of the linear transformation range, and the expressions of a and m are taken to adjust the variation range of the linear transformation
Is to satisfy
Get it
Is to satisfy
And the intercept of the linear gray scale transformation is represented by the gray scale mean value of the two types of gray scales,
represents the difference between the normal gradation value and the abnormal gradation value, and is therefore calculated by
Adjusting the amplitude of the gray level enhancement;
(3) And enhancing the abnormal gray value in the metal surface image to be detected by using the segmented linear transformation function of the abnormal gray value, and inhibiting the normal gray value in the metal surface image to be detected by using the segmented linear transformation function of the normal gray value to realize the characteristic enhancement of the defect region.
Furthermore, the enhanced metal surface image can be compressed and transmitted in different modes, and different coding and compression modes are adopted for different areas in the defect enhanced metal surface image for compression and transmission, so that the defect characteristics are ensured, and the compression efficiency is improved.
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.