```
dev_update_off()
* 参数定义
GaussSize := 3
ScratchLengthMin := 100
StainAreaMin := 100
BlurThreshold := 25 // 模糊检测方差阈值
MissingDeviationLimit := 15 // 图案缺失灰度标准差阈值
PatternROISize := 0.3 // 中心检测区域占比
* 获取图像文件列表
ImageFolder := 'E:/毕业论文/瓶盖图/'
list_files(ImageFolder, ['files','follow_links'], ImageFiles)
tuple_regexp_select(ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
* 创建日志文件
open_file('defect_log.txt', 'output', FileHandle)
* 主循环
for ImgIndex := 0 to |ImageFiles| - 1 by 1
* 窗口管理
dev_close_window()
dev_open_window(0, 0, 600, 400, 'black', WindowHandle)
* 文件读取(带异常捕获)
try
read_image(Image, ImageFiles[ImgIndex])
catch (Exception)
write_string(FileHandle, 'Failed:'+ImageFiles[ImgIndex]+'\n')
continue
endtry
get_image_size (Image, Width, Height)
GaussSize := min([int(Width/500),int(Height/300),11])*2+1 // 动态核尺寸
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (2)
* 1. 图像预处理(保留V通道信息)
try
decompose3 (Image, R, G, B)
trans_from_rgb (R, G, B, H, S, V, 'hsv')
gauss_filter (V, VFiltered, GaussSize)
catch (Exception)
disp_message (WindowHandle, '预处理错误', 'window', 12, 12, 'red', 'true')
write_string(FileHandle, 'Preprocess Error:'+ImageFiles[ImgIndex]+'\n')
continue
endtry
* 2. 瓶盖区域定位
binary_threshold (VFiltered, Regions, 'max_separability', 'light', UsedThreshold)
connection (Regions, ConnectedRegions)
select_shape(ConnectedRegions, SelectedRegions, ['area','circularity'], 'and', [5000,0.7], [9999999,1.0])
fill_up (SelectedRegions, RegionFillUp)
shape_trans (RegionFillUp, CapRegion, 'convex')
* 3. 缺陷检测流程
* 3.1 划痕检测
reduce_domain (VFiltered, CapRegion, VReduced)
equ_histo_image (VReduced, VEquHist)
edges_sub_pix (VEquHist, Edges, 'canny', 1.0, 15, 25)
select_shape_xld(Edges, Scratches, ['contlength','width'], 'and', [100,1], [9999,3])
gen_region_contour_xld (Scratches, ScratchesRegion, 'filled')
* 3.2 污渍检测
texture_laws(VReduced, TextureEL, 'el', 5, 5)
texture_laws(VReduced, TextureLE, 'le', 5, 5)
add_image(TextureEL, TextureLE, TextureCombined, 0.5, 0)
var_threshold(TextureCombined, Stains, 15, 15, 0.15, 1.2, 'dark')
connection(Stains, ConnectedStains)
select_shape(ConnectedStains, SelectedStains, 'area', 'and', StainAreaMin, 99999)
* 4. 合并缺陷区域
union2(ScratchesRegion, SelectedStains, AllDefects)
* 5. 后处理
connection (AllDefects, ConnectedDefects)
closing_circle(ConnectedDefects, FinalDefects, 1.5)
* 6. 结果显示
count_obj (FinalDefects, NumDefects)
dev_display (Image)
dev_set_color ('green')
dev_display (CapRegion)
if (NumDefects > 0)
dev_set_color ('blue')
dev_display (ScratchesRegion)
dev_set_color ('magenta')
dev_display (SelectedStains)
* 添加图例说明
disp_message (WindowHandle, '检测结果:', 'window', 12, 12, 'white', 'true')
disp_message (WindowHandle, '蓝色:划痕缺陷', 'window', 40, 12, 'blue', 'true')
disp_message (WindowHandle, '紫色:污渍缺陷', 'window', 60, 12, 'magenta', 'true')
else
disp_message (WindowHandle, '检测通过:无缺陷', 'window', 12, 12, 'green', 'true')
endif
endfor
* 关闭日志文件
close_file(FileHandle)```未处理的程序异常:HALCON算子错误,当在函数 'main' 中调用 'write_string'时.line: 45.
Invalid handle type (HALCON错误代码: 2404)
最新发布