目录
一、场景需求
1、检测物体表面划痕、划伤等线状缺陷。
二、源码解析
* 显示初始化
dev_update_off ()
dev_close_window ()
read_image (Image, 'surface_scratch')
* 图像反色
invert_image (Image, ImageInverted)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
* runtime时图像分辨率固定可使用rft加速优化
* Message := 'Optimize the speed of the fast fourier transform.'
* Message[1] := 'Please wait...'
* disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
* optimize_rft_speed (Width, Height, 'standard')
* disp_continue_message (WindowHandle, 'black', 'true')
* stop ()
*
* 生成带通滤波器,用于频域卷积凸显缺陷
gen_sin_bandpass (ImageBandpass, 0.4, 'none', 'rft', Width, Height)
* rft转换为频域
rft_generic (ImageInverted, ImageFFT, 'to_freq', 'none', 'complex', Width)
* 使用带通滤波器对频域进行卷积运算
convol_fft (ImageFFT, ImageBandpass, ImageConvol)
* 将卷积后的图像还原到时域
rft_generic (ImageConvol, Lines, 'from_freq', 'n', 'byte', Width)
* 阈值分割
threshold (Lines, Region, 5, 255)
* 区域连通
connection (Region, ConnectedRegions)
* 面积筛选过滤掉小于5的干扰区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 5000)
* 区域形态学膨胀
dilation_circle (SelectedRegions, RegionDilation, 5.5)
* 对膨胀后的区域进行合并连通
union1 (RegionDilation, RegionUnion)
* 将膨胀连通后的区域作为缺陷提取ROI区域
reduce_domain (Image, RegionUnion, ImageReduced)
* 使用lines_gauss提取线状缺陷(注:该算子参数可通过calculate_lines_gauss_parameters算子工具辅助计算参数)
lines_gauss (ImageReduced, LinesXLD, 0.8, 3, 5, 'dark', 'false', 'bar-shaped', 'false')
* 按参数拟合合并xld
union_collinear_contours_xld (LinesXLD, UnionContours, 40, 3, 3, 0.2, 'attr_keep')
* 按xld长度筛选满足条件的xld
select_shape_xld (UnionContours, SelectedXLD, 'contlength', 'and', 15, 1000)
* xld转换为region,region膨胀后用于可视化显示
gen_region_contour_xld (SelectedXLD, RegionXLD, 'filled')
union1 (RegionXLD, RegionUnion)
dilation_circle (RegionUnion, RegionScratches, 10.5)
*
* Display the results
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_colored (12)
dev_display (Image)
dev_display (RegionScratches)
三、核心算子
1、gen_sin_bandpass:生成设定频率参数的带通滤波器。
2、rft_generic:实数域FFT,实现从时域→频域,从频域→时域转换。
3、convol_fft:FFT频域卷积。
4、lines_gauss:检测线状特征并计算线宽。
四、技巧思路
1、使用带通滤波器对图像频域进行卷积运算,凸显频率参数附近的特征。
2、使用Blob分析确定ROI区域,筛选目标缺陷。
3、使用lines_gauss算子进行线状缺陷检测。