Halcon表面缺陷检测

Halcon表面缺陷检测

本文章转载自 link

1* Optimize the fft speed for the specific image size
优化特定图像大小的FFT速度
optimize_rft_speed( : : Width, Height, Mode : )

optimize_rft_speed (Width, Height, ‘standard’)

//对指定大小的图片的fft速度进行优化
//Width, Height图片大小
// Mode 优化模式
((fft(Fast Fourier Transformation),即为快速傅氏变换,是离散傅氏变换(DFT)的快速算法,它是根据离散傅氏变换的奇、偶、虚、实等特性,对离散傅立叶变换的算法进行改进获得的。)

2* Construct a suitable filter by combining two gaussian
构造两个滤波器
gen_gauss_filter( : ImageGauss : Sigma1, Sigma2, Phi, Norm, Mode, Width, Height : )
sub_image(ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : )
gen_gauss_filter (GaussFilter1, Sigma1, Sigma1, 0.0, ‘none’, ‘rft’, Width, Height)
gen_gauss_filter(GaussFilter2, Sigma2, Sigma2, 0.0, ’none’, ’rft’, Width,Height)
//在频域生成两个高斯滤波器
//参数说明: 生成的高斯滤波器(GaussFilter)
// 空域中高斯在主方向上的标准差(Sigma)
// 空域中高斯在正交于主方向的方向上的标准差(Sigma)
// 滤波器主方向的角度(0.0)
// 滤波器的规范(’none’)
// 直流项在频域的位置(’rft’)
// 图片的大小(Width,Height)

sub_image(GaussFilter1,GaussFilter2,Filter,1,0)
//两图片相减(灰度) 做减法
//sub_image(ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : )
//g’ := (g1 - g2) * Mult + Add
//以上为函数原型以及运算公式
(Enhance the scratches by filtering in the frequency domain
在频域中通过滤波增强划痕)

3* Perform the convolution in the frequency domain
//在频域执行卷积
rft_generic (Image, ImageFFT, ‘to_freq’, ‘none’, ‘complex’, Width)
convol_fft (ImageFFT, Filter, ImageConvol)
rft_generic (ImageConvol, ImageFiltered, ‘from_freq’, ‘n’, ‘real’, Width)

rft_generic(Image,ImageFFT,’to_freq’,’none’,’complex’,Width)
//对计算一幅图片实部进行快速傅里叶变换

//参数说明: 输入的图片(Image)
// 傅里叶变换后输出的图片(ImageFFT)
// 变换方向(’to_freq’或’from_freq’)
// 变换因子的规范(’none’)
// 输出图片的数据类型(’complex’)
// 图片的宽(Width)

convol_fft(ImageFFT,Filter,ImageConvol)
//对图片用一个滤波器在频域进行卷积运算

//参数说明: 输入的图片(ImageFFT)
// 频域滤波器(Filter)
// 运算后输出的结果
rft_generic(ImageConcol,ImageFiltered,’from_freq’,’n’,’real’,Width)

//对滤波后的图片进行傅里叶反变换

4* Process the filtered image

处理已过滤的图像

gray_range_rect (ImageFiltered, ImageResult, 10, 10)

min_max_gray (ImageResult, ImageResult, 0, Min, Max, Range)

gray_range_rect(ImageFiltered,ImageResult,10,10)
//用一个矩形掩膜计算像素点的灰度范围
//参数说明: 输入的图片(ImageFiltered)
// 输出的灰度范围图(ImageResult)
// 矩形掩膜大小(10,10)

min_max_rect(ImageResult,ImageResult,0,Min,Max,Range)
//判断区域内灰度值的最大和最小值

//参数说明: 待分析图片区域(ImageResult)
// 图片(ImageResult)
// 被去除的直方图两边像素点所
// 占总像素数的百分比(0)
// 得到的最小值最大值及灰度值范围(Min,Max,Range)

threshold (ImageResult, RegionDynThresh, max([5.55,Max * 0.8]), 255)
connection (RegionDynThresh, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 4, 99999)
union1 (SelectedRegions, RegionUnion)
closing_circle (RegionUnion, RegionClosing, 10)
connection (RegionClosing, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ‘area’, ‘and’, 10, 99999)
area_center (SelectedRegions1, Area, Row, Column)

例程:detect_indent_fft.hdev

说明:针对的检测表面很多是具有一定纹理的比如:布匹、皮革、塑料等的针孔瑕疵检测。

这个程序展示了如何利用快速傅里叶变换(FFT)对塑料制品的表面进行目标(缺陷)的检测,大致分为三步:

首先,我们用高斯滤波器构造一个合适的滤波器(将原图通过高斯滤波器滤波);

然后,将原图和构造的滤波器进行快速傅里叶变换;

最后,利用形态学算子将缺陷表示在滤波后的图片上(在缺陷上画圈)。

算法讲解:在实际的表面缺陷检测系统中,针对的检测表面很多是具有一定纹理的比如:布匹、皮革、塑料等,针对这一类表面的检测就不能单纯依靠帧差或者背景差来完成,因为背景的纹理不可能和当前图像的纹理完全相同。因此,本例程的算法通过将图像变换到频域进行处理,提取缺陷分量后反变换到时域,获得缺陷的具体位置。

高斯滤波器:在本算法中,在一开始就构造了两个高斯滤波器,高斯滤波器是一种线性平滑滤波器,适用于消除高斯噪声。滤波器的实质是对信号进行滤波,滤除不需要的部分,得到想要的部分。一个低通的滤波器可以滤除高频信号,对于图像来说,噪声和边缘往往集中在高频分量,因此低通能够降噪,但是也能造成图像的模糊。

本文关键:本文的关键就是使用两个低通滤波器,进行相减后构造了一个带阻滤波器来提取缺陷分量。这就需要保证在实际的待检测表面中缺陷所处的频率范围要和背景以及噪声有明显的差异,并且带阻的频率选择要合适。通过带阻滤波后获得的频率成分对背景中的纹理要有明显的抑制,并且突出缺陷成分,进行傅里叶反变换后重构的图像就是缺陷图像,经过简单的分割就能很容易得到缺陷了。

  • Enhance the scratches by filtering in the frequency domain

*在频域中通过滤波增强划痕

1*构造滤波器
gen_sin_bandpass( : ImageFilter : Frequency, Norm, Mode, Width, Height : )
产生带正弦形态的带通滤波器

gen_sin_bandpass (ImageBandpass, 0.4, ‘none’, ‘rft’, Width, Height)

//参数说明: 带通滤波器(ImageBandpass)
// 滤波器最大距离(0.4)
// 滤波器常规因子 (none)
// 频率谱中心位置(rft)
// 图像的宽、高(Width,Height)

3* Perform the convolution in the frequency domain

//在频域执行卷积

rft_generic (Image, ImageFFT, ‘to_freq’, ‘none’, ‘complex’, Width)

convol_fft (ImageFFT, Filter, ImageConvol)

rft_generic (ImageConvol, ImageFiltered, ‘from_freq’, ‘n’, ‘real’, Width)

rft_generic(Image,ImageFFT,’to_freq’,’none’,’complex’,Width)
//对计算一幅图片实部进行快速傅里叶变换

//参数说明: 输入的图片(Image)
// 傅里叶变换后输出的图片(ImageFFT)
// 变换方向(’to_freq’或’from_freq’)
// 变换因子的规范(’none’)
// 输出图片的数据类型(’complex’)
// 图片的宽(Width)

convol_fft(ImageFFT,Filter,ImageConvol)
//对图片用一个滤波器在频域进行卷积运算

//参数说明: 输入的图片(ImageFFT)
// 频域滤波器(Filter)
// 运算后输出的结果
rft_generic(ImageConcol,ImageFiltered,’from_freq’,’n’,’real’,Width)

//对滤波后的图片进行傅里叶反变换

这个例子是检查划痕的,感觉是金属表面划痕

calculate_lines_gauss_parameters( : : MaxLineWidth, Contrast : Sigma, Low, High)

1*将图片RGB图像分为GRG三个通道的图像

decompose3(MultiChannelImage : Image1, Image2, Image3 : : )

decompose3 (Image, R, G, B)

2*

gen_gauss_filter (ImageGauss, 100, 100, 0, ‘n’, ‘rft’, Width, Height)

生成高斯滤波器

‘n’=’none’

(Enhance the scratches by filtering in the frequency domain

在频域中通过滤波增强划痕)

  • Perform the convolution in the frequency domain

//在频域执行卷积

rft_generic (Image, ImageFFT, ‘to_freq’, ‘none’, ‘complex’, Width)

convol_fft (ImageFFT, Filter, ImageConvol)

rft_generic (ImageConvol, ImageFiltered, ‘from_freq’, ‘n’, ‘real’, Width)

rft_generic(Image,ImageFFT,’to_freq’,’none’,’complex’,Width)
//对计算一幅图片实部进行快速傅里叶变换

//参数说明: 输入的图片(Image)
// 傅里叶变换后输出的图片(ImageFFT)
// 变换方向(’to_freq’或’from_freq’)
// 变换因子的规范(’none’)
// 输出图片的数据类型(’complex’)
// 图片的宽(Width)

convol_fft(ImageFFT,Filter,ImageConvol)
//对图片用一个滤波器在频域进行卷积运算

//参数说明: 输入的图片(ImageFFT)
// 频域滤波器(Filter)
// 运算后输出的结果
rft_generic(ImageConcol,ImageFiltered,’from_freq’,’n’,’real’,Width)

//对滤波后的图片进行傅里叶反变换

sub_image(ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : )

sub_image (B, ImageFFT1, ImageSub, 2, 100)

//两幅图片做减法

zoom_image_factor(Image : ImageZoomed : ScaleWidth, ScaleHeight, Interpolation : )

zoom_image_factor (ImageSub, ImageZoomed, ScaleFactor, ScaleFactor, ‘constant’)

//将图片按照给定的图像比例进行缩放

在模糊图像中

印痕/水痕 一类的缺陷检测

在高纹理物体上做缺陷检测

get_image_size (Image, Width, Height)

gen_gauss_filter (ImageGauss, 50, 50, 0, ‘n’, ‘rft’, Width, Height)

rft_generic (Image, ImageFFT, ‘to_freq’, ‘none’, ‘complex’, Width)

convol_fft (ImageFFT, ImageGauss, ImageConvol)

rft_generic (ImageConvol, IlluminationImage, ‘from_freq’, ‘none’, ‘byte’, Width)

sub_image (B, ImageFFT1, ImageSub, 2, 100)

(Sng 返回返回参数的正负。语法Sgn(number), number 参数是任何有效的数值表达式。返回值如果数字大于0,则Sgn 返回1,数字等于0,则返回0,数字小于0,则返回-1,数字参数的符号决定了Sgn 函数的返回值。)

  • median filter smooths out the fine texture, simplifying the following * segmentation and final detection of defects

*中值滤波平滑的质地优良,简化如下

*缺陷的分割和最终检测

median_image (ImageSub, ImageMedian, ‘circle’, 9, ‘mirrored’)

watersheds_threshold (ImageMedian, Basins, 20)

后期寻找:

cooc_feature_image (Basins, ImageMedian, 6, 0, Energy, Correlation, Homogeneity, Contrast)

tuple_find (sgn(Energy - 0.05), -1, Indices)

select_obj (Basins, Defects, Indices + 1)

猜你喜欢

转载自blog.csdn.net/qq_45780653/article/details/106852478
今日推荐