Reprinted from https://www.cnblogs.com/sleepwalker/p/3625449.html
*Initialization(Initialization) dev_updata_off() //This sentence contains the following three operators: //Dev? Updata? PC ('off ') turns off the updater counter //Dev? Updata? Var ('off ') closes the update variables window //Dev? Updata? Window ('off ') closes the update image window (i.e. the image you want to display in the image window is displayed through the command) dev_close_window() //Close the active image window read_image(Image,'plastics / plastics_01') //Load Images //Parameter Description: name the read in picture (Image) // File name ('plastics / plastics' 01 ') get_image_size(Image,Width,height) //Get the length and width of the picture; //Parameter Description: previously read in or generated Image // Width of picture // Height of picture dev_open_window(0,0,Width,Height,'Black',WindowHandle) //Open a new image window //Parameter Description: starting coordinate (0,0) // Width,Height // Background color ('Black ') // Window handle set_display_font (WindowHandle,14,'mono','ture','false')//Set fonts that do not depend on the operating system //Parameter Description: WindowHandle // Font size (14) // Font type ('mono ') // Whether it is in bold ('ture ') // Is it inclined ('false ') dev_set_draw('Margin') //Define area fill mode //Parameter Description: Fill mode ('Margin 'or' Fill ') dev_set_line_width(3) //Set the lineweight of the contour line in the output area //You can modify the parameters to see the difference of the final defect area mark dev_set_color('red') //Set one or more output colors * *Optimize the fft speed for the specific image size(Based on the specified image size fft Speed optimization) optimize_rft_speed(Width,Height,'standard') //Optimize the fft speed of pictures with specified size //Parameter Description: picture size (Width,Height) // Optimization mode ('standard ') * *Construct a suitable filter by combining two Gaussian filters(Combining two Gaussian filters to construct a*Suitable filters) Sigma1 := 10.0 Sigma2 := 3.0 //Define two constants gen_gauss_filter(GaussFilter1,Sigma1,Sigma1,0.0,'none','rft',Width,Height) gen_gauss_filter(GaussFilter2,Sigma2,Sigma2,0.0,'none','rft',Width,Height) //Generating two Gaussian filters in frequency domain //Parameter Description: Gaussian filter generated // Standard deviation (Sigma) of Gauss in main direction in airspace // The standard deviation (Sigma) of Gauss in the direction orthogonal to the main direction in the airspace // Angle of filter main direction (0.0) // Specification of filter ('none ') // Position of dc term in frequency domain ('rft ') // Picture size (Width,Height) sub_image(GaussFilter1,GaussFilter2,Filter,1,0) //Subtraction of two pictures (grayscale) //sub_image(ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : ) //g' := (g1 - g2) * Mult + Add //The above is the function prototype and operation formula * *Process the images iteratively(Iterative operation on image) NumImages := 11 For Index := 1 to NumImages by 1 //for loop from 1 to NumImages in steps of 1 * *Read an image and convert it to gray values read_image(Image,'plastics/plastics_'+Index$'02') rgb1_to_gray(Image,Image) //Convert the original image to grayscale image, and the first parameter is the original image *Perform the convolution in the frequency domain rft_generic(Image,ImageFFT,'to_freq','none','complex',Width)//Fast Fourier transform for calculating the real part of a picture //Parameter Description: input Image // Image output after Fourier transform (ImageFFT) // Change direction ('to ﹣ to ﹣ or' from ﹣) // Specification of transformation factor ('none ') // Data type of output picture ('complex ') // Width of picture convol_fft(ImageFFT,Filter,ImageConvol) //Convolution of images in frequency domain with a filter //Parameter Description: entered image (ImageFFT) // Frequency domain Filter // Output after operation rft_generic(ImageConcol,ImageFiltered,'from_freq','n','real',Width) //Inverse Fourier transform the filtered image * *Process the filtered image gray_range_rect(ImageFiltered,ImageResult,10,10)//Using a rectangular mask to calculate the gray range of pixels //Parameter Description: entered image (ImageFiltered) // Output grayscale range (ImageResult) // Rectangular mask size (10,10) min_max_rect(ImageResult,ImageResult,0,Min,Max,Range)//Determine the maximum and minimum gray values in the region //Parameter Description: image result to be analyzed // Image result // Pixel points on both sides of the removed histogram // % of total pixels (0) // Min,Max,Range threshold(ImageResult,RegionDynThresh,max([5.55,Max*0.8]),255)//Image segmentation using global threshold //Parameter Description: entered picture (ImageResult) // Region dynthresh after segmentation // Threshold (max([5.55,Max*0.8]),255) // Formula: mingray < = g < = maxgray connection(RegionDynThresh,ConnectedRegions) //The connected part in the calculation area //Parameter Description: entered picture (RegionDynThresh) // Connected regions obtained select_shape (ConnectedRegions,SelectedRegions,'area','and',4,99999)//Select the area according to the specified morphological characteristics //Parameter Description: entered pictures (ConnectedRegions) // Selected regions // Morphological feature to be calculated ('area ') // Connection between independent features ('and ') // Minimum limit of features (4) // Maximum limit of feature (99999) union1(SelectedRegions,RegionUnion) //Returns a collection containing all regions //Parameter Description: include the pictures of all regions to be calculated (tedRegions) // All input region union closeing_circle(RegionUnion,RegionClosing,10)//To enclose an area with a circle //Parameter Description: region union to be closed // Region closing // Radius of circle (10) connection(RegionClosing,ConnectedRegions1) select_shape(ConnectedRegions1,SelectedRegions1,'area','and',10,99999) area_center(SelectedRegions1,Area,Row,Column) //Calculate area and center location of area //Parameter Description: area to be calculated (SelectedRegions1) // Area of area // Row in regional center // Column in the region center * *Display the results dev_display(Image) //Display original graph Number := |Area| //Assign area to Number for later checking for defects if(Number) gen_circle_contour_xld(ContCircle,Row,Column,gen_tuple_const(Number,30),gen_tuple_const(Number,0), gen_tuple_const(Number,rad(360)),'positive',1)//Construct a boundary consistent with the set arc or circle //Parameter Description: generated boundary (ContCircle) // Center coordinate of arc or circle (Row,Cloumn) // Radius of the arc or circle (Gen tuple const (Number, 30)) // Gen? Triple? Const (number, 0) // The end angle of an arc or circle (Gen? Tuple? Const (number, RAD (360))) // I don't know what it means // Distance between two adjacent points (1) ResultMessage := ['Not OK',Number + 'defect(s) found'] Color := ['red','black'] dev_display(ContCircle) else ResultMessage := 'OK' Color := 'forest green' endif disp_message(WindowHandle,ResultMessage,'window',12,12,Color,'ture') if(Index#NumImages) disp_continue_message(WindowHandle,'black','ture') stop() endif endfor
Algorithm explanation: in the actual surface defect detection system, many of the detected surfaces have certain texture, such as cloth, leather, plastic, etc. the detection of this kind of surface can not be completed only by frame difference or background difference, because the texture of the background cannot be exactly the same as the texture of the current image. Therefore, the algorithm of this routine transforms the image to the frequency domain for processing, extracts the defect components and reversely transforms them to the time domain to obtain the specific location of the defect.
Gauss filter: in this algorithm, two Gauss filters are constructed at the beginning. Gauss filter is a kind of linear smoothing filter, which is suitable for eliminating Gauss noise. The essence of the filter is to filter the signal, filter out the unnecessary part and get the desired part. A low-pass filter can filter out the high-frequency signal. For the image, the noise and edge are often concentrated in the high-frequency component, so the low-pass filter can reduce the noise, but it can also cause image blur.
The key of this paper: the key of this paper is to use two low-pass filters to construct a band stop filter to extract defect components after subtraction. It is necessary to ensure that the frequency range of the defect in the actual surface to be tested is significantly different from the background and noise, and the frequency selection of the band stop is appropriate. The frequency component obtained by band stop filtering should obviously suppress the texture in the background, and highlight the defect component. The reconstructed image after inverse Fourier transform is the defect image, which can be easily obtained after simple segmentation.