这个例子展示了如何在模糊图像中检测mura缺陷
总代码:
*频域+差分+空间域
dev_close_window ()
dev_update_off ()
Path := 'lcd/mura_defects_blur_'
read_image (Image, Path + '01')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, 640, 480, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
ScaleFactor := 0.4
*根据要提取的线条的最大宽度和对比度,计算线条的Sigma、Low和High参数。
calculate_lines_gauss_parameters (17, [25,3], Sigma, Low, High)
for f := 1 to 3 by 1
*1采集图像
read_image (Image, Path + f$'.2i')
*转换为3通道图像
decompose3 (Image, R, G, B)
*2预处理之频域变换(建立背景+高斯滤波)
* correct side illumination
*正确的侧面照明
*快速傅里叶变换
rft_generic (B, ImageFFT, 'to_freq', 'none', 'complex', Width)
*生成一个高斯滤波器
gen_gauss_filter (ImageGauss, 100, 100, 0, 'n', 'rft', Width, Height)
*在频域中用滤波器卷积图像。
convol_fft (ImageFFT, ImageGauss, ImageConvol)
*傅里叶逆变换
rft_generic (ImageConvol, ImageFFT1, 'from_freq', 'none', 'byte', Width)
*差分在空间域--lines_gauss
sub_image (B, ImageFFT1, ImageSub, 2, 100)
* perform the actual inspection
*按给定因子缩放图像。
zoom_image_factor (ImageSub, ImageZoomed, ScaleFactor, ScaleFactor, 'constant')
* avoid border effects when using lines_gauss()
*使用时lines_gauss()避免边界效果
*获取图像的区域。
get_domain (ImageZoomed, Domain)
*腐蚀
erosion_rectangle1 (Domain, RegionErosion, 7, 7)
*抠图
reduce_domain (ImageZoomed, RegionErosion, ImageReduced)
*检测线条及其宽度。
lines_gauss (ImageReduced, Lines, Sigma, Low, High, 'dark', 'true', 'gaussian', 'true')
*进行仿射变换
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale_local (HomMat2DIdentity, 1 / ScaleFactor, 1 / ScaleFactor, HomMat2DScale)
affine_trans_contour_xld (Lines, Defects, HomMat2DScale)
*
dev_display (Image)
dev_display (Defects)
if (f < 3)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
逐段分析:
*频域+差分+空间域
dev_close_window ()
dev_update_off ()
Path := 'lcd/mura_defects_blur_'
read_image (Image, Path + '01')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, 640, 480, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
ScaleFactor := 0.4
*根据要提取的线条的最大宽度和对比度,计算线条的Sigma、Low和High参数。
calculate_lines_gauss_parameters (17, [25,3], Sigma, Low, High)
for f := 1 to 3 by 1
*1采集图像
read_image (Image, Path + f$'.2i')
*转换为3通道图像
decompose3 (Image, R, G, B)
*快速傅里叶变换
rft_generic (B, ImageFFT, 'to_freq', 'none', 'complex', Width)
*生成一个高斯滤波器
gen_gauss_filter (ImageGauss, 100, 100, 0, 'n', 'rft', Width, Height)
*在频域中用滤波器卷积图像。
convol_fft (ImageFFT, ImageGauss, ImageConvol)
*傅里叶逆变换
rft_generic (ImageConvol, ImageFFT1, 'from_freq', 'none', 'byte', Width)
*差分在空间域--lines_gauss
sub_image (B, ImageFFT1, ImageSub, 2, 100)
*按给定因子缩放图像。
zoom_image_factor (ImageSub, ImageZoomed, ScaleFactor, ScaleFactor, 'constant')
*使用时lines_gauss()避免边界效果
*获取图像的区域。
get_domain (ImageZoomed, Domain)
*腐蚀
erosion_rectangle1 (Domain, RegionErosion, 7, 7)
*抠图
reduce_domain (ImageZoomed, RegionErosion, ImageReduced)
*检测线条及其宽度。
lines_gauss (ImageReduced, Lines, Sigma, Low, High, 'dark', 'true', 'gaussian', 'true')
*进行仿射变换
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale_local (HomMat2DIdentity, 1 / ScaleFactor, 1 / ScaleFactor, HomMat2DScale)
affine_trans_contour_xld (Lines, Defects, HomMat2DScale)
*
dev_display (Image)
dev_display (Defects)
if (f < 3)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
主要算子:
lines_gauss(Image : Lines : Sigma, Low, High, LightDark, ExtractWidth, LineModel, CompleteJunctions :
功能:检测线条及其宽度
Image:输入图像
Lines:提取出的亚像素精度线条
Sigma:应用的高斯平滑的系数
Low:后滞阈值分割的低值
High:后滞阈值分割的高值
LightDark:提取图像中的亮色或者暗色线条
ExtractWidth:是否提取线条的宽度
LineModel:用于校正线条位置和宽度的线条模型
CompleteJunctions:是否添加能够提取的接合点