得到halcon默认图片存储路径
get_system (‘image_dir’, HalconImages)
获得操作系统类型
get_system (‘operating_system’, OS)
OS{0:2}的意思是只选取OS里边的0,1,2三个字符
if (OS{0:2} == ‘Win’)
得到系统是否禁止错误对话框显示
dev_get_preferences (‘suppress_handled_exceptions_dlg’, SaveMode)
设置禁止错误对话框显示由try、catch捕获错误
dev_set_preferences (‘suppress_handled_exceptions_dlg’, ‘true’)
按照路径读取图片,如果读取不成功catch错误,显示读取图片出错
for k := 0 to |HalconImages| - 1 by 1
try
read_image (Image, HalconImages[k] + ‘/halogen_bulb/halogen_bulb_01.png’)
ReadPath := HalconImages[k] + ‘/halogen_bulb/’
ReadOK := true
break
catch (Exception)
endtry
endfor
if (not ReadOK)
disp_message (WindowHandle, ‘Could not find the images in $HALCONIMAGES’, ‘window’, -1, -1, ‘black’, ‘true’)
stop ()
endif
再将错误对话框显示恢复为默认模式
dev_set_preferences (‘suppress_handled_exceptions_dlg’, SaveMode)
读入图片
read_image (Image, ‘halogen_bulb/halogen_bulb_01.png’)
得到指向图片第一个通道的指针get_image_pointer1 (输入图像, 指针, 图片类型, 宽, 高)
get_image_pointer1 (Image, Pointer, Type, Width, Height)
开窗口与显示
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
定义类名
ClassNames := [‘good’,‘bad’,‘none’]
定义颜色数组,用于显示不同类别
Colors := [‘forest green’,‘red’,‘red’]
Nu := 0.05
KernelParam := 0.02
创建一个svm分类器
create_class_svm (特征数,内核函数, 向量对其周围环境的影响量, 错误率,类数量, ‘模式’, ‘预处理’, 样本数量, SVM分类器句柄)
create_class_svm (7, ‘rbf’, KernelParam, Nu, |ClassNames|, ‘one-versus-one’, ‘principal_components’, 5, SVMHandle)
为SVM分类器添加样本,自定义函数
add_samples_to_svm (ClassNames, SVMHandle, WindowHandle, ReadPath)
for ClassNumber := 0 to |ClassNames| - 1 by 1
list_files (ReadPath + ClassNames[ClassNumber], ‘files’, Files)
从Files中选择图片格式为png的图片
Selection := regexp_select(Files,’.*[.]png’)
for Index := 0 to |Selection| - 1 by 1
read_image (Image, Selection[Index])
dev_display (Image)
threshold (Image, Region, 0, 40)
自定义函数
calculate_features (Region, Features)
求取region中心
area_center (Region, Area, Row, Column)
区域紧凑度
compactness (Region, Compactness)
计算区域二维不变矩
moments_region_central_invar (Region, PSI1, PSI2, PSI3, PSI4)
计算区域的凸度
convexity (Region, Convexity)
real将元组转换为浮点数的元组。
Features := real([Area,Compactness,PSI1,PSI2,PSI3,PSI4,Convexity])
return ()
为svm分类器添加样本
add_sample_class_svm(:: SVM句柄,特征向量,训练类别 ?
add_sample_class_svm (SVMHandle, Features, ClassNumber)
endfor
endfor
return ()
dev_clear_window ()
训练SVM分类器
disp_message (WindowHandle, ‘Training…’, ‘window’, -1, -1, ‘black’, ‘true’)
训练svn分类器train_class_svm (SVM分类器句柄, 梯度阈值, 训练模式)
train_class_svm (SVMHandle, 0.001, ‘default’)
disp_message (WindowHandle, ‘Training completed’, ‘window’, -1, -1, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
自定义函数使用svm分类器进行识别
classify_regions_with_svm (SVMHandle, Colors, ClassNames, ReadPath)
list_files (ReadPath, [‘files’,‘recursive’], Files)
Selection := regexp_select(Files,’.*[.]png’)
read_image (Image, Selection[0])
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width / 2, Height / 2, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
for Index := 0 to |Selection| - 1 by 1
read_image (Image, Selection[Index])
threshold (Image, Region, 0, 40)
自定义函数和上边做训练的是一个函数
calculate_features (Region, Features)
使用SVM分类器进行分类classify_class_svm(:: 分类器句柄,特征向量,最佳类数:识别结果)
classify_class_svm (SVMHandle, Features, 1, Class)
dev_display (Image)
dev_set_color (Colors[Class])
dev_display (Region)
disp_message (WindowHandle, ‘Classified as:’ + ClassNames[Class], ‘window’, -1, -1, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endfor
dev_display (Image)
return ()
清除SVM分类器,释放内存
clear_class_svm (SVMHandle)
这个例子介绍了create_class_svm 、add_sample_class_svm、train_class_svm、classify_class_svm 的使用方法,使用SVM分类器对元素的凸度与不变的结构向量进行分类。完成了对卤素灯泡的完整性的检测。
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。