halcon应用范围-表面检测2一模糊壁画图像中的缺陷检测
*检测思路 先对图像进行傅里叶变换 在频域内用一个滤波器对图像进行卷积 再傅里叶逆变换 得到图像的背景色
*用分解三原色后的蓝像素图减去背景图 得到更明显的带噪点的图
*腐蚀带噪点的图,去掉小的区域 得到大的折痕图 再检测图像的线和宽度
*detect-mura-defects-blur.hdev
*模糊图像中的壁面缺陷检测
* this example shows how to detect mura defects
* in blurred images
*
dev_close_window ()
*停止刷新PC 变量 窗口
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
calculate_lines_gauss_parameters (17, [25,3], Sigma, Low, High)
for f := 1 to 3 by 1
read_image (Image, Path + f$'.2i')
*把图片分解为RGB三原色 三个图像
decompose3 (Image, R, G, B)
* 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)
*原来图片和进行滤波后的图像相减相减 凹痕区域更明显
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()
*获得图像的区域
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