[Defect Recognition] Classification and measurement of metal surface defects based on matlab GUI SVM [Include Matlab source code 682]

[Defect Recognition] Classification and measurement of metal surface defects based on matlab GUI SVM [Include Matlab source code 682]

1. Introduction

1.1 Subject content
Metal plates are widely used in all aspects of industrial production and production and life. Due to the influence of multiple factors such as equipment and technology involved in the metal plate manufacturing process, the surface of the metal plate is prone to defects of various types and shapes, which affect the wear resistance, corrosion resistance, electromagnetic properties and aesthetics of the metal plate. The properties will have varying degrees of impact, and ultimately affect the electromagnetic properties and coating effects of the metal plate. Therefore, for companies that produce metal plates, surface defect detection is an indispensable process. On the one hand, defective products can be detected in time through surface defect detection to ensure the quality of the metal plates produced and maintain the reputation of the enterprise. On the other hand, It is also possible to discover the problems in the production process in time by analyzing the test results and solve them in time [1]. We will use the metal surface defect photo data set provided on the Internet as the source of the pictures, and construct the corresponding algorithm to detect, identify, classify and measure the metal surface defects.
1.2 Subject requirements The
identification and classification of metal surface defects have the following specific requirements:
(1) According to the characteristics of the metal surface defect pictures, the picture is subjected to appropriate gray-scale transformation (contrast enhancement and filtering processing);
(2) The metal surface defect pictures are processed Global optimization threshold segmentation;
(3) Extracting the boundary coordinates of the binary image area;
(4) Extracting features of metal defects;
(5) Performing supervised training on the classification of metal defects;
(6) Completing the judgment and location of metal defects Calculation of size;
(7) Complete GUI design for metal defect detection.
2. Subject analysis.
We checked the relevant information and learned that the common metal surface defects are mainly as follows, which are: crazing, surface impurities (inclusion), patches, pitted surface, rolling Into the rolled-in scale and scratches. They correspond to Figure (a)-Figure (f) respectively.

We only consider three types of them: surface spots (figure b), surface rolling into oxide scale (figure (e)) and surface scratches (figure (f)).
The main steps of image processing are: contrast enhancement, filter processing, threshold segmentation, morphological processing, feature extraction, classification training, defect location size calculation and GUI integrated operation display.
Step.1: Contrast enhancement.
Due to the lighting method and other reasons, we found that the gray scale difference between the metal surface defect in the original image and the entire metal surface background is small, and the gray range is small, so the contrast is low, and the metal defects are not. Not obvious, this is not conducive to the post-processing of the image, we need to use some methods to increase the contrast of the image. Common contrast enhancement methods include logarithmic transformation, power-law transformation, grayscale layering, grayscale normalization, contrast stretching, histogram equalization, and histogram specification. Considering that the background light intensity of different photos is not the same, so the method of constant parameters (such as logarithmic transformation, power law transformation, grayscale layering, contrast stretching) is not applicable to all images, so we consider using grayscale normalization. Unified and histogram equalization.
Step.2: Filter processing When
taking photos, various noises will inevitably be introduced during the collection process, including Gaussian noise and salt and pepper noise. At the same time, the metal surface itself has a certain texture. Noise and texture will conceal metal surface defects to a certain extent, leading to errors in extraction. We need to filter the image first to reduce subsequent false detections. Filtering methods are mainly divided into spatial filtering and frequency domain filtering, and the relationship between the two satisfies the convolution theorem. Among them, spatial filtering mainly includes mean filtering, median filtering, Gaussian filtering and bilateral filtering; frequency filtering pays attention to Fourier transform filtering, discrete cosine transform filtering and wavelet transform filtering. Although these algorithms can reduce noise, they can also filter out some detailed information, leading to the loss of some defect details. All we need to find new filtering algorithms to achieve.
Step.3: Threshold segmentation Step.4: Morphological processing
we get the filtered image, we also need to perform binarization threshold processing to properly extract defect information. Traditional threshold segmentation needs to determine the threshold, but for different complex metal defect images, we cannot use the same threshold for all images. We plan to use global/local optimization threshold segmentation to automatically determine the best threshold.

Limited by the quality of the original image and the image threshold segmentation algorithm, some areas in the binary image that should be connected may be segmented by the threshold, such as scratch defects due to somewhere The gray value is low and is incorrectly segmented, resulting in a scratch may be separated into multiple line segments. Therefore, we need to perform a closed operation to connect adjacent objects. At the same time, some defects in the picture are large (for example, the width of the scratch is large). After the subsequent edge detection, the two edges of a straight line are far apart. The straight line extraction algorithm will mistake the same line as two parallel scratches. . Therefore, we also need to refine the defects.
Step.5: Feature extraction
This part can use two methods, one is based on general feature extraction algorithms (such as HOG, LBP, Haar algorithm, etc.); the other is based on the specific metal defects we want to detect specific features To classify scratches directly. I will test these two methods separately.
Step.6: Classification training
If we use a general feature extraction algorithm, we will perform classification supervision training on it. Commonly used supervised learning algorithms include: K proximity algorithm (KNN), naive Bayes algorithm, decision tree algorithm, support Vector machine (svm), logistic regression, etc. Since the support vector machine has excellent generalization ability and can get much better results than other algorithms in the small sample training set [6], we plan to choose the support vector machine algorithm.
Step.7: Defect location size calculation After
we extract the defect, we need different features to characterize the size and location of the defect. For "rolled into scale", we use the number of "rolled into scale" to characterize the size of the defect, and its centroid to characterize its location; for spots, we use the ratio of the area in the spot to the entire picture to characterize the size of the defect. For scratches, we use the two end points of the scratch to characterize the defect location, and use the length of the scratch to standardize the size of the defect.
Step.8: GUI design
For the convenience of users, we have designed a GUI interface, which can select pictures in the computer, and can display the extracted defect information (type and location size) after clicking "image processing". For the length of the scratch, we can allow the user to input The focal length of the camera and the length of the object distance are used to calculate the actual scratch size based on the knowledge of geometric optics, and the image size and actual size of each scratch are displayed. At the same time, the GUI allows the user to go to the download site of the data set by pressing the button.

2. the source code

//Overview of works: The main function of this code is to realize the detection, classification and measurement of three kinds of defects on the metal surface, and use GUI to encapsulate it for the convenience of users. The GUI can import grayscale images in the computer, and then process them to determine the types of defects and mark the detected defects on the original image. And can measure the size of different defects (different defect measurement standards are different). -------------------------------------------------- ---------------------------------------- //Introduction to important functions: #pushbutton1_Callback(hObject, eventdata,handles):The callback function of button 1 (ie the'image processing' button), including all image processing algorithms #pushbutton2_Callback(hObject, eventdata,handles):The callback function of button 2 (ie the'image import' button) is mainly for importing images #Ga(theta,num): Initialization of the Garbor convolution kernel of the image #filterimage (a6, Gaborfiter): Garbor filtering algorithm #thres (image): maximum entropy threshold gray image segmentation algorithm #location (): 'rolled into the scale' positioning defects and display #houghtran (): For 'zoned Hough circle detection of traces' defects #lbp(varargin): 'local binary pattern' feature extraction algorithm on the Internet #svm.m: independent m file, used for LBP feature extraction and SVM (support vector machine) of all metal defect photos ) Training and testing of supervised learning algorithms -------------------------------------------------- ------------------------------------------ %} %% function varargout = app(varargin) % GUI page main function, provided by MATLAB, no need to modify gui_Singleton = 1 ; gui_State = struct( 'gui_Name' , mfilename, ... 'gui_Singleton' , gui_Singleton, ... 'gui_OpeningFcn' , @app_OpeningFcn, ... 'gui_OutputFcn' , @app_OutputFcn, ... 'gui_LayoutFcn' , [] ,. .. 'gui_Callback' , []); if nargin && ischar (varargin{ 1 }) gui_State.gui_Callback = str2func(varargin{ 1 }); end if nargout [varargout{ 1 :nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end %% function app_OpeningFcn (hObject, eventdata, handles, varargin) % GUI initialization function %Modify setting background %modified by:Faust.Cao ha =axes( 'units' , 'normalized' , 'pos' ,[ 0 0 1 1 ]); uistack(ha, 'down' ); % Set the background image of the program as back.jpg ii=imread( 'C:\Users\lenovo\Desktop\Metal surface defect classification and measurement GUI system\Metal surface defect classification and measurement system\back.jpg' ); image(ii); colormap gray set (ha, 'handlevisibility' , 'off' , 'visible' , 'off' ) ; handles.output = hObject; guidata(hObject, handles); %% function varargout = app_OutputFcn(hObject, eventdata, handles) % GUI output function, provided by MATLAB, no need to modify varargout{ 1 } = handles.output ; %% function pushbutton1_Callback (hObject, eventdata, handles) % The callback function of button 1 (ie the'image processing' button) , including all image processing algorithms global a5 ;% global variables global svmModel;% global variables, called from the svm.m file axis off;% remove the axes coordinates and display the picture completely a6=a5; a=str2double(get(handles.edit1, 'string' ));% read the focal length entered by the user b=str2double(get(handles.edit2, 'string' ));% read the object distance entered by the user d=a/(ba);% The vertical axis magnification is calculated by the Gauss formula ma=max(max(a6)); mi=min(min(a6)); % Contrast enhancement, the gray level is mapped to [ 0 , 255 ] for ii = 1 : 200 for jj = 1 : 200 a6(ii,jj)= 255 * double (a6(ii,jj)-mi)./double ( ma-mi); end end %Image filtering ab=filterimage(a6); % Image maximum entropy threshold segmentation ss=thres(ab); ab1=im2bw(ab,ss/255 ); [B,L]=bwboundaries(ab1);% Binary graph edge coordinate extraction [x,y]=size(B); mm = 0 ; %Find the largest extraction area for i= 1 :x ll=length(B{i}); if ll>mm mm=ll; end end % Judge whether it is a defect of'rolled into scale' if mm< 120 set (handles.text8, 'string' , 'defect is rolled into scale' of'rolled );% output the defect type in the text8 text box [xc,yc]=findcenter(B,ab);% Find the centroid position of each extracted area Axes(handles.axes2);% is set to display at axes2 imshow(a6); hold on; plot(yc+ 12 ,xc+ 12 , '*' );% plus the boundary lost during filtering [dd,bb]=size(xc); tex=[ 'A total of' ,num2str(dd), 'more than one point' ]; set (handles.text10, 'string' ,tex);% output defect information in the text10 text box % Is not a defect of'rolling into oxide scale' , continue to judge else % The original image and the binary image are masked. Note that the sizes of the two are different, so the edges of the original image must be removed first a6( 1 : 12 ,:)=[]; a6( 177 : 188 ,:)=[]; a6(:, 1 : 12 )=[]; a6(:, 177 : 188 )=[]; mu1=uint8(ab1).*a6; mu2=uint8( 1 -ab1).*a6; t0=mu1(mu1~= 0 ); m1=mean(t0); t1=mu2(mu2~= 0 ); m2=mean(t1); % Determine whether the defect is a spot or a scratch: the spot is darker than the background, and the scratch is cooler than the background; compare the mask with the remaining average of the background % You can also first extract the LBP (local binary pattern) features of the image, and then use the results of the SVM training for classification if m1<m2+ 20 set (handles.text8, 'string' , 'defects are spots' ); all=bwarea(ab1);% calculate spot area = Pro 100 * All/176/176 ; percentage calculated% tex=[ 'The area ratio of spots is' ,num2str(pro), '%' ]; set (handles.text10, 'string' ,tex); axes(handles.axes2); imshow(a6); hold on; % Mark out the spot boundary for k = 1 :length(B) boundary = B{k}; plot(boundary(:, 2 ), boundary(:, 1 ), 'r' , 'LineWidth' , 2 ) end % Defects are scratches else a6=adapthisteq(a6);% adaptive histogram equalization enhances the contrast again ss=thres(a6); ab1=im2bw(a6,ss/255 ); ab1=imclose(ab1,strel( 'square' , 6 ));% closed operation on the binary graph, which will disconnect part of the connection ab1=bwmorph(ab1, 'thin' , 5 );% further refine the binary graph ab1=edge(ab1, 'Canny' );% use Canny operator edge extraction set (handles.text8, 'string' , 'defect is scratch' ); axes(handles.axes2); imshow(a6); hold on; % Down is the subsequent processing and display of scratch extraction [point,ang]=houghtran( ab1 );% Hough line detection on the processed binary image cen=zeros(length(ang), 2 ); po=zeros(length(ang), 4 ); an=zeros(length(ang), 1 ); thm=mean( abs (ang)); % Determine whether the scratch is a horizontal line or a vertical line (the angle between the straight line and the y-axis) if abs (thm) <45% vertical bar thm = 40 ; else % horizontal line thm = 60 ; end le=length(cen); %Find the midpoint position of each straight line for kk = 1 :le cen(kk, 1 )=(point(kk, 1 )+point(kk, 3 ))/2 ; cen(kk, 2 )=(point(kk, 2 )+point(kk, 4 ))/2 ; point(kk, 5 )=cen(kk, 1 ); point(kk, 6 )=cen(kk, 2 ); end % Sort straight lines by midpoint if thm== 40 %If it is a vertical line, sort by horizontal coordinate point=sortrows(point, 5 ); end Copy code

3. running results

4. remarks

Version: 2014a