Magnetic core surface defect detection method based on machine vision
Technical Field
The invention relates to the technical field of defect detection, in particular to a magnetic core surface defect detection method based on machine vision.
Background
The magnetic core is produced in the production process due to the production process or the production environment, such as: surface defects such as scratches, and gaps. These core surface defects can cause the product to have poor strength, reduced electrical characteristics, and even serious safety hazards. Currently, quality inspection of magnetic cores generally relies on manual inspection of surface defects through glasses.
The traditional manual detection mainly has the following defects: 1. the human eye has limited spatial resolution and is difficult to resolve fine cracks. 2. The manual detection is easily affected by subjective consciousness, and the accuracy of the detection is difficult to guarantee. 3. The manual detection efficiency is low, and the product quality information management cannot be realized.
Disclosure of Invention
The invention aims to provide a magnetic core surface defect detection method based on machine vision, and aims to solve the problems of large manual operation error and low efficiency in the prior art.
In order to achieve the purpose, the invention provides the following technical scheme: a magnetic core surface defect detection method based on machine vision comprises the following steps:
1) collecting ten pictures of the magnetic core to be detected by a color CCD industrial camera;
2) carrying out gray level processing on the acquired image, and converting the image into a single-channel gray level image;
3) evaluating the image after the gray processing by using an image quality evaluation function to select an optimal test image;
4) performing denoising processing on the selected best test picture by using median filtering of 3x 3;
5) carrying out contrast enhancement processing on the image by using gamma conversion;
6) obtaining a binary image by using an OSTU algorithm;
7) extracting the outline of the defect by using a 4-connected region method; (ii) a
8) Drawing the outline of the defect by using the minimum circumscribed rectangle and calculating the area of the defect;
9) comparing the calculated area of the rectangle with a set threshold, if the calculated area of the rectangle is larger than the set threshold, recording the vertex coordinates of the rectangle, otherwise, neglecting;
10) drawing a minimum bounding rectangle of the defect in the original image;
11) and uploading the defect information and the picture to a database.
Preferably, the CCD industrial camera is a color industrial camera.
Preferably, according to steps 1) to 3), 10 pictures (I) are taken each time0…I9) Performing gray level processing, and selecting the best test picture I by using the image quality evaluation functionbest。
Preferably, the image quality evaluation function is:
IGray=0.30*R+0.59*G+0.11*B (1)
f=∑x∑y(|f(x,y)-f(x-1,y)|+|f(x,y)-f(x+1,y)|+|f(t,y)-
f(x,y-1)|+|f(x,y)-f(x,y+1)|) (2)
equation 1: formula for single-channel graying IGrayThe image after graying, three channel values of R, G, B color image;
equation 2: and f (x, y) is a gray value of the image at the point (x, y), f (x, y-1), f (x, y +1), f (x-1, y) and f (x +1, y) are gray values of upper, lower, left and right points of the point (x, y), respectively, f is an image quality value, and the larger the f value, the better the image quality.
Preferably, the selected best test picture is subjected to median filtering of 3x3 to remove noise according to the steps 4) to 5), and then contrast enhancement processing is performed on the image by gamma conversion to obtain an image I'best。
Preferably, the image I 'after the image enhancement according to the steps 6) to 9)'bestObtaining a binary image I' by using an OTSU algorithmbestExtracting the outer contour of the defect by using a 4-connected domain method, determining the outer contour by using a minimum external rectangular frame, and calculating the area S of the outer contour1…nFinally, the coordinate of the vertex of the rectangle meeting the condition p is recorded by comparing the coordinate with a set threshold value delta1..m。
Preferably, the calculation formula is:
u=w0*u0+w1*u1 (3)
g=w0*(u0-u)*(u0-u)+w1*(u1-u)*(u1-u) (4)
S1...n=L*W (6)
equation 3: w is a0,u0Average gray value of foreground under current threshold; w is a1,u1The average gray value of the background under the current threshold value; u is the total average gray scale of the image;
equation 4: g is the variance of the foreground and background images, and the gray value th is the optimal threshold when the variance g is maximum;
equation 5: i ″)bestThe method is a binary image, wherein A is an image pixel gray value, and th is an optimal threshold;
equation 6: s1…nIs of minimum rectangular area, L is rectangularLong, W is the width of the rectangle.
Preferably, the position of each defect is drawn by the minimum bounding rectangle in the original image according to the steps 10) to 11), and the number, the area and the picture information of the detected defects are uploaded to a database.
Compared with the prior art, the invention has the beneficial effects that: compared with traditional manual detection, mechanical detection and the like, the detection method is higher in detection precision, better in flexibility and more intelligent, comprises two parts of magnetic core surface defect detection and defect information and picture storage to a database, firstly, magnetic core pictures to be detected are collected (ten pictures are collected at each time), the collected pictures are converted into single-channel gray level images, then, image quality evaluation is carried out, and the best test pictures are selected. Carrying out 3X3 median filtering on the selected picture, carrying out image enhancement processing, obtaining a binary image through an OTSU algorithm (optimal inter-class variance), extracting the outline of the defect through a 4-connected region method, defining the outline by using a minimum circumscribed rectangle frame, calculating the area of the minimum circumscribed rectangle frame, recording the vertex coordinates of the external minimum rectangle if the area is larger than a threshold value (not fixed and can be flexibly changed), drawing the minimum circumscribed rectangle of the defect in the original image, and indicating the position of the defect. Finally, uploading the defect information and the picture to a database;
1. the position of the defect can be accurately and quickly positioned, and the detection precision and efficiency are improved;
2. the detection precision can be flexibly controlled, the production requirement is convenient, and the intelligent detection device is more intelligent;
3. the defect information and the pictures can be uploaded to a database, so that the product quality can be conveniently monitored and inquired;
4. the interference caused by human factors is avoided, and the production cost is reduced.
Drawings
FIG. 1 is a flow chart of the present invention;
FIG. 2 is a selected best test gray scale picture of the present invention;
FIG. 3 is the result of the 3X3 median filtering of the present invention;
FIG. 4 is the result of the gamma conversion of the present invention;
FIG. 5 is the OSTU binarization result of the present invention;
FIG. 6 is a profile of the present invention obtained using the 4-connected domain method;
FIG. 7 is detected defect information of the present invention;
FIG. 8 shows the results of the surface defect inspection according to the present invention.
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.
Referring to fig. 1 to 8, the present invention provides a technical solution: a magnetic core surface defect detection method based on machine vision comprises the following steps:
1) collecting ten pictures of the magnetic core to be detected by a color CCD industrial camera;
2) carrying out gray level processing on the acquired image, and converting the image into a single-channel gray level image;
3) evaluating the image after the gray processing by using an image quality evaluation function to select an optimal test image;
4) performing denoising processing on the selected best test picture by using median filtering of 3x 3;
5) carrying out contrast enhancement processing on the image by using gamma conversion;
6) obtaining a binary image by using an OSTU algorithm;
7) extracting the outline of the defect by using a 4-connected region method; (ii) a
8) Drawing the outline of the defect by using the minimum circumscribed rectangle and calculating the area of the defect;
9) comparing the calculated area of the rectangle with a set threshold, if the calculated area of the rectangle is larger than the set threshold, recording the vertex coordinates of the rectangle, otherwise, neglecting;
10) drawing a minimum bounding rectangle of the defect in the original image;
11) and uploading the defect information and the picture to a database.
The CCD industrial camera is a color industrial camera.
According to steps 1) to 3), 10 pictures (I) are taken each time0…I9) Performing gray level processing, and selecting the best test picture I by using the image quality evaluation functionbest。
The image quality evaluation function is as follows:
IGray=0.30*R+0.59*G+0.11*B (1)
=∑x∑y)|f(x,y)-f(x-1,y)|+|f(x,y)-f(x+1,y)|+|f(x,y)-
f(x,y-1)|+|f(x,y)-f(x,y+1)|) (2)
equation 1: formula for single-channel graying IGrayThe image after graying, three channel values of R, G, B color image;
equation 2: and f (x, y) is a gray value of the image at the point (x, y), f (x, y-1), f (x, y +1), f (x-1, y) and f (x +1, y) are gray values of upper, lower, left and right points of the point (x, y), respectively, f is an image quality value, and the larger the f value, the better the image quality.
Filtering noise of the selected optimal test picture by using a median filter of 3x3 according to the steps 4) to 5), and performing contrast enhancement processing on the image by using gamma conversion to obtain an image I'best。
Enhancing the image I 'according to the steps 6) to 9)'bestObtaining a binary image I' by using an OTSU algorithmbestExtracting the outer contour of the defect by using a 4-connected domain method, determining the outer contour by using a minimum external rectangular frame, and calculating the area S of the outer contour1…nFinally, the coordinate of the vertex of the rectangle meeting the condition p is recorded by comparing the coordinate with a set threshold value delta1..m. The calculation formula is as follows:
u=w0*u0+w1*u1 (3)
g=w0*(u0-u)*(u0-u)+w1*(u1-u)*(u1-u) (4)
S1...n=L*W (6)
equation 3: w is a0,u0Average gray value of foreground under current threshold; w is a1,u1The average gray value of the background under the current threshold value; u is the total average gray scale of the image;
equation 4: g is the variance of the foreground and background images, and the gray value th is the optimal threshold when the variance g is maximum;
equation 5: i ″)bestThe value graph is obtained, A is the gray value of the image pixel, and th is the optimal threshold;
equation 6: s1…nIs the smallest rectangular area, L is the length of the rectangle, and W is the width of the rectangle.
Drawing the position of each defect in the original image by using the minimum bounding rectangle according to the steps 10) to 11), and uploading the number, the area and the picture information of the detected defects to a database.
According to the technical scheme, 1) ten pictures of the magnetic core to be detected are collected through a color CCD industrial camera;
2) carrying out gray level processing on the acquired image, and converting the image into a single-channel gray level image;
3) evaluating the image after the gray processing by using an image quality evaluation function, and selecting an optimal test image shown in fig. 2;
4) denoising the selected best test picture by using median filtering of 3X3 to obtain a filtered image as shown in FIG. 3;
5) carrying out contrast enhancement processing on the image by using gamma conversion to obtain an enhanced image shown in FIG. 4;
6) obtaining a binary map as shown in fig. 5 using the OSTU algorithm, wherein white areas are defects;
7) extracting the outline of the defect by using a 4-connected region method, wherein the obtained result is shown in FIG. 6;
8) drawing the outline of the defect by using the minimum circumscribed rectangle and calculating the area of the defect;
9) comparing the calculated area of the rectangle with a set threshold, recording the vertex coordinates of the rectangle if the calculated area of the rectangle is larger than the threshold, otherwise, neglecting, and obtaining a result as shown in FIG. 7;
10) drawing a minimum bounding rectangle of the defect in the original image, indicating the position of the defect, and obtaining a result as shown in FIG. 8;
11) and uploading the defect information and the picture to a database.
Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that changes, modifications, substitutions and alterations can be made in these embodiments without departing from the principles and spirit of the invention, the scope of which is defined in the appended claims and their equivalents.