以下代码是一个图像处理脚本,使用了HALCON软件的命令来实现对图像的一系列处理操作,主要目的是检测图像中的缺陷
dev_update_off ()
dev_close_window ()
read_image (Image, ‘plastics/plastics_01’)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
dev_set_line_width (3)
dev_set_color (‘red’)
*
- Optimize the fft speed for the specific image size
optimize_rft_speed (Width, Height, ‘standard’) - Construct a suitable filter by combining two gaussian
- filters
Sigma1 := 10.0
Sigma2 := 3.0
gen_gauss_filter (GaussFilter1, Sigma1, Sigma1, 0.0, ‘none’, ‘rft’, Width, Height)
gen_gauss_filter (GaussFilter2, Sigma2, Sigma2, 0.0, ‘none’, ‘rft’, Width, Height)
sub_image (GaussFilter1, GaussFilter2, Filter, 1, 0) - Process the images iteratively
NumImages := 11
for Index := 1 to NumImages by 1
*- Read an image and convert it to gray values
read_image (Image, ‘plastics/plastics_’ + Index$‘02’)
rgb1_to_gray (Image, Image) - 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) - Process the filtered image
gray_range_rect (ImageFiltered, ImageResult, 10, 10)
min_max_gray (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) - Display the results
dev_display (Image)
Number := |Area|
if (Number)
gen_circle_contour_xld (ContCircle, Row, Column, gen_tuple_const(Number,30), gen_tuple_const(Number,0), gen_tuple_const(Number,rad(360)), ‘positive’, 1)
ResultMessage := [‘Not OK’,Number + ’ defect(s) found’]
Color := [‘red’,‘black’]
dev_display (ContCircle)
else
ResultMessage := ‘OK’
Color := ‘forest green’
endif
disp_message (WindowHandle, ResultMessage, ‘window’, 12, 12, Color, ‘true’)
if (Index != NumImages)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
- Read an image and convert it to gray values
以下是代码的逐行解释:
dev_update_off():关闭图像更新,以提高处理速度。
dev_close_window():关闭当前打开的窗口。
read_image (Image, ‘plastics/plastics_1’):读取名为’plastics_1’的图像。
get_image_size (Image, Width, Height):获取图像的宽度和高度。
dev_open_window (, , Width, Height, ‘black’, WindowHandle):以图像的尺寸打开一个新窗口,背景颜色为黑色。
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’):设置窗口中显示的字体样式。
dev_set_draw (‘margin’):设置绘图边界。
dev_set_line_width (3):设置线条宽度。
dev_set_color (‘red’):设置绘图颜色为红色。
接下来的部分是优化快速傅里叶变换(FFT)的速度,并构建一个适合的滤波器,通过两个高斯滤波器的差值来实现。
optimize_rft_speed (Width, Height, ‘standard’):优化FFT速度。
gen_gauss_filter:生成两个不同标准差的高斯滤波器。
sub_image:将两个高斯滤波器相减,得到最终的滤波器。
接下来的循环部分对一系列图像进行处理:
for Index := 1 to NumImages by 1:开始一个循环,处理11张图像。
read_image:读取每张图像。
rgb1_to_gray:将图像转换为灰度图像。
rft_generic:将图像转换到频域。
convol_fft:使用之前生成的滤波器对图像进行卷积。
rft_generic:将卷积后的图像从频域转换回空间域。
gray_range_rect:在图像中定义一个感兴趣区域。
min_max_gray:计算感兴趣区域的灰度范围。
threshold:对图像进行阈值处理,以分割出感兴趣的区域。
connection:连接图像中的区域。
select_shape:根据区域的面积选择特定的区域。
union1:将选定的区域合并。
closing_circle:对合并后的区域进行闭运算,以消除小的空洞。
connection:再次连接图像中的区域。
select_shape:再次选择特定区域。
area_center:计算选定区域的面积和中心位置。
最后,根据处理结果在窗口中显示图像和消息:
dev_display:显示原始图像。
根据检测到的缺陷数量,显示不同的消息和颜色。
disp_message:在窗口中显示消息。
if (Index != NumImages):如果不是最后一张图像,显示继续消息并暂停。
整个脚本的目的是自动化地检测图像中的缺陷,并通过可视化的方式报告检测结果。这对于质量控制和自动化检测非常有用。
以下是代码的逐行解释:
dev_update_off():关闭图像更新,以提高处理速度。
dev_close_window():关闭当前打开的窗口。
read_image (Image, ‘plastics/plastics_1’):读取名为’plastics_1’的图像。
get_image_size (Image, Width, Height):获取图像的宽度和高度。
dev_open_window (, , Width, Height, ‘black’, WindowHandle):以图像的尺寸打开一个新窗口,背景颜色为黑色。
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’):设置窗口中显示的字体样式。
dev_set_draw (‘margin’):设置绘图边界。
dev_set_line_width (3):设置线条宽度。
dev_set_color (‘red’):设置绘图颜色为红色。
接下来的部分是优化快速傅里叶变换(FFT)的速度,并构建一个适合的滤波器,通过两个高斯滤波器的差值来实现。
optimize_rft_speed (Width, Height, ‘standard’):优化FFT速度。
gen_gauss_filter:生成两个不同标准差的高斯滤波器。
sub_image:将两个高斯滤波器相减,得到最终的滤波器。
接下来的循环部分对一系列图像进行处理:
for Index := 1 to NumImages by 1:开始一个循环,处理11张图像。
read_image:读取每张图像。
rgb1_to_gray:将图像转换为灰度图像。
rft_generic:将图像转换到频域。
convol_fft:使用之前生成的滤波器对图像进行卷积。
rft_generic:将卷积后的图像从频域转换回空间域。
gray_range_rect:在图像中定义一个感兴趣区域。
min_max_gray:计算感兴趣区域的灰度范围。
threshold:对图像进行阈值处理,以分割出感兴趣的区域。
connection:连接图像中的区域。
select_shape:根据区域的面积选择特定的区域。
union1:将选定的区域合并。
closing_circle:对合并后的区域进行闭运算,以消除小的空洞。
connection:再次连接图像中的区域。
select_shape:再次选择特定区域。
area_center:计算选定区域的面积和中心位置。
最后,根据处理结果在窗口中显示图像和消息:
dev_display:显示原始图像。
根据检测到的缺陷数量,显示不同的消息和颜色。
disp_message:在窗口中显示消息。
if (Index != NumImages):如果不是最后一张图像,显示继续消息并暂停。
整个脚本的目的是自动化地检测图像中的缺陷,并通过可视化的方式报告检测结果。这对于质量控制和自动化检测非常有用。