Fourier transform for defect detection detect_indent_fft.hdev (source code and detailed analysis)

Introduction

detect_indent_fft.hdev is a sample program of halcon. It is an example of Fourier transform for defect detection, mainly Fourier transform for defect detection under complex background .

This program shows how to use Fast Fourier Transform (FFT) to detect defects on the surface of plastic products. It is roughly divided into three steps:

1. Use a Gaussian filter to construct a suitable filter (filter the original image through a Gaussian filter);

2. Perform fast Fourier transform on the original image and the constructed filter;

3. Use morphology to detect defects.

Program analysis

About display function explanation https://blog.csdn.net/cashmood/article/details/93999690
dev_updata_off()

dev_close_window() //Close the active image window and
read in the picture
read_image(Image,'plastics/plastics_01') to
get the length and width of the picture
// Parameter description: the picture read (Image); the width of the picture (Width); the picture Height (Height)
get_image_size(Image,Width,height)

dev_open_window(0,0,Width,Height,'Black',WindowHandle)
set the display font
set_display_font (WindowHandle,14,'mono','ture','false')

dev_set_draw(‘Margin’)

dev_set_line_width(3)

dev_set_color(’red’)

(Optimize the fft speed according to the image size) Optimize
the fft speed of the picture of the specified size
//Parameter description: picture size (Width, Height); optimization mode ('standard');
optimize_rft_speed (Width, Height,'standard' )

(Combine two Gaussian filters to construct a suitable filter)

Define two constants
Sigma1 := 10.0 Sigma2 := 3.0

Generate two Gaussian filters in the frequency domain
//parameter description: the generated Gaussian filter (GaussFilter); the standard deviation of the Gaussian in the main direction in the spatial domain (Sigma); the Gaussian in the spatial domain in the direction orthogonal to the main direction Standard deviation (Sigma); the angle of the main direction of the filter (0.0); the specification of the filter ('none'); the position of the DC term in the frequency domain ('rft'); the size of the picture (Width, Height)
gen_gauss_filter(GaussFilter1 ,Sigma1,Sigma1,0.0,'none','rft',Width,Height)

gen_gauss_filter(GaussFilter2,Sigma2,Sigma2,0.0,’none’,’rft’,Width,Height) //

Subtracting two images to generate a band-pass low-frequency filter
// function prototype and calculation formula: sub_image(ImageMinuend, ImageSubtrahend: ImageSub: Mult, Add:)
//g' := (g1-g2) * Mult + Add

sub_image(GaussFilter1 ,GaussFilter2,Filter,1,0) //Subtract two pictures (grayscale)

(Start image detection)
NumImages := 11
for loop from 1 to NumImages, step size is 1
for Index := 1 to NumImages by 1

read_image(Image,’plastics/plastics_’+Index$’02’)

Convert the image to a grayscale image, the first parameter is the original image
rgb1_to_gray(Image,Image)

*Perform the convolution in the frequency domain
to perform fast Fourier transform on a picture
//Parameter description: input picture (Image); Fourier transform output picture (ImageFFT); transform direction ('to_freq' or ' from_freq'); the specification of the transform factor ('none'); the data type of the output image ('complex'); the width of the image (Width)
rft_generic(Image,ImageFFT,'to_freq','none','complex',Width )

The picture uses a filter to perform convolution operation in the frequency domain.
//Parameter description: input picture (ImageFFT); frequency domain filter (Filter); output result after operation
convol_fft(ImageFFT,Filter,ImageConvol) //Use for picture A filter performs convolution in the frequency domain

Perform inverse fast Fourier transform on the filtered image
rft_generic(ImageConcol,ImageFiltered,'from_freq','n','real',Width)

Use a rectangular mask to calculate the grayscale range of the
pixel.//Parameter description: input image (ImageFiltered); output grayscale range map (ImageResult); rectangular mask size (10,10)
grayscale calculation method: rectangle Maximum gray value in the mask-minimum gray value

gray_range_rect(ImageFiltered,ImageResult,10,10)

Find the maximum and minimum values ​​and transformation range of the image gray value
//Parameter description: image area to be analyzed (ImageResult); image (ImageResult); pixels on both sides of the histogram to be removed; percentage of total pixels (0) ; Obtained minimum and maximum values ​​and range of gray values ​​(Min,Max,Range)
min_max_rect(ImageResult,ImageResult,0,Min,Max,Range)//Judging the maximum and minimum gray values ​​in the area

Use the global threshold to segment the image
//parameter description: input image (ImageResult); region obtained after segmentation (RegionDynThresh); threshold (max([5.55,Max 0.8]),255); formula: MinGray <= g < = MaxGray
threshold(ImageResult,RegionDynThresh,max([5.55,Max0.8]),255)//

Split connected domain
//Parameter description: input area (RegionDynThresh); split connected domain (ConnectedRegions)
connection(RegionDynThresh,ConnectedRegions)

Filter regions based on area
select_shape (ConnectedRegions,SelectedRegions,'area','and',4,99999)

Merged region
union1(SelectedRegions,RegionUnion)

Use circular elements to close the region
closing_circle(RegionUnion,RegionClosing,10)

Split connected domain
connection (RegionClosing, ConnectedRegions1)

Filter area according to area
select_shape(ConnectedRegions1,SelectedRegions1,'area','and',10,99999)

Calculate the area of ​​the area and the center position
area_center(SelectedRegions1,Area,Row,Column)

show result

dev_display(Image)

Define the number of variable statistical areas
//Parameter description: "||" in halcon represents the number of elements in the array
Number := |Area|

Determine whether there is a defect area
if (Number)

If there is a defective area, draw the defective area

	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)

	ResultMessage := [‘Not OK’,Number + ‘defect(s) found’]

    Color := [‘red’,’black’]

    dev_display(ContCircle) else ResultMessage := ‘OK’

    Color := ‘forest green’

If there is no defect area, display OK
endif

 	 disp_message(WindowHandle,ResultMessage,’window’,12,12,Color,’ture’) if(Index#NumImages)

     disp_continue_message(WindowHandle,’black’,’ture’)

     stop()

  endif

endfor

Processing result preview

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Algorithm explanation

在实际的表面缺陷检测系统中,针对的检测表面很多是具有一定纹理的比如:布匹、皮革、塑料等,针对这一类表面的检测就不能单纯依靠帧差或者背景差来完成,因为背景的纹理不可能和当前图像的纹理完全相同。因此,本例程的算法通过将图像变换到频域进行处理,提取缺陷分量后反变换到时域,获得缺陷的具体位置。
在本算法中,在一开始就构造了两个高斯滤波器,高斯滤波器是一种线性平滑滤波器,适用于消除高斯噪声。滤波器的实质是对信号进行滤波,滤除不需要的部分,得到想要的部分。一个低通的滤波器可以滤除高频信号,对于图像来说,噪声和边缘往往集中在高频分量,因此低通能够降噪,但是也能造成图像的模糊。
关键就是使用两个低通滤波器,进行相减后构造了一个带阻滤波器来提取缺陷分量。这就需要保证在实际的待检测表面中缺陷所处的频率范围要和背景以及噪声有明显的差异,并且带阻的频率选择要合适。通过带阻滤波后获得的频率成分对背景中的纹理要有明显的抑制,并且突出缺陷成分,进行傅里叶反变换后重构的图像就是缺陷图像,经过简单的分割就能很容易得到缺陷了。

Guess you like

Origin blog.csdn.net/cashmood/article/details/105205210