案例要求:
图为HALCON中的例图“surface_scratch”,请提取出图中的划痕。
案例分析:
图中明亮程度不一,划痕颜色较淡,因此不能用灰度BLOB分析的方法提取出目标区域。因此,先构造一个带通滤波器,用于去除背景光线的干扰,再对得到的图像作BLOB分析,提取出图像中的划痕。
案例代码:
read_image (Image, 'surface_scratch')
invert_image (Image, ImageInverted)
get_image_size (Image, Width, Height)
*创建一个正弦形状的带通滤波器
gen_sin_bandpass (ImageBandpass, 0.4, 'none', 'rft', Width, Height)
*快速傅立叶变换
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)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 5000)
dilation_circle (SelectedRegions, RegionDilation, 5.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
*将划痕区域连接成线
lines_gauss (ImageReduced, LinesXLD, 0.8, 3, 5, 'dark', 'false', 'bar-shaped', 'false')
union_collinear_contours_xld (LinesXLD, UnionContours, 40, 3, 3, 0.2, 'attr_keep')
select_shape_xld (UnionContours, SelectedXLD, 'contlength', 'and', 15, 1000)
gen_region_contour_xld (SelectedXLD, RegionXLD, 'filled')
union1 (RegionXLD, RegionUnion)
dilation_circle (RegionUnion, RegionScratches, 10.5)