下述代码是一个基于HALCON图像处理软件的脚本,用于对不同种类的药片进行分类。
dev_close_window ()
dev_update_off ()
read_image (Image, ‘color/pills_class_01’)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_draw (‘margin’)
dev_set_line_width (2)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
dev_set_colored (12)
*
PillNames := [‘big_round_red’,‘round_green’,‘small_round_red’,‘yellow_trans’,‘brown’,‘brown_green’]
PillNames := [PillNames,‘brown_grain’,‘purple’,‘turquese’,‘pink’]
PillColors := [‘#D08080’,‘#ADC691’,‘#FFB0A1’,‘#D5C398’,‘#B59C87’,‘#BCB3B8’,‘#B7ACA1’,‘#908E99’,‘#97B9BC’,‘#C0ABA9’]
*
- Check, which features and feature groups are available
query_feature_group_names (AvailableGroupNames)
query_feature_names_by_group (AvailableGroupNames, AvailableFeatureNames, AvailableCorrespondingGroups) - Generate list of color and region features using the
- calculate_feature_set library procedures.
FeatureGroups := [‘region’,‘color’]
get_feature_names (FeatureGroups, FeatureNames)
get_feature_lengths (FeatureNames, FeatureLengths) - Create and prepare classifier training data structure
create_class_train_data (sum(FeatureLengths), ClassTrainDataHandle)
set_feature_lengths_class_train_data (ClassTrainDataHandle, FeatureLengths, FeatureNames) - Training loop
for I := 1 to 10 by 1
* Segment pills in training image
read_image (Image, ‘color/pills_class_’ + I$‘.2d’)
segment_pills (Image, Pills)
* Display segmentation result
dev_display (Image)
dev_set_color (‘white’)
dev_display (Pills)
disp_message (WindowHandle, ‘Collecting ’ + PillNames[I - 1] + ’ samples’, ‘window’, 12, 12, ‘black’, ‘true’)
*
* Calculate features for all segmented pills and store
* them in the training data structure
count_obj (Pills, Number)
calculate_features (Pills, Image, FeatureNames, Features)
add_sample_class_train_data (ClassTrainDataHandle, ‘feature_column’, Features, I - 1)
*
* Visualize processed pills
dev_set_color (PillColors[I - 1])
dev_display (Pills)
GroupList := sum(‘’’ + FeatureGroups + ‘’, ')
tuple_str_first_n (GroupList, strlen(GroupList) - 3, GroupList)
Message := ‘Calculate ’ + |FeatureNames| + ’ features from following feature groups:’
disp_message (WindowHandle, [Message,GroupList], ‘window’, 40, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endfor
*
- Automatically select suitable features from the training data
disp_message (WindowHandle, ‘Selecting optimal features…’, ‘window’, 90, 12, ‘black’, ‘true’)
select_feature_set_svm (ClassTrainDataHandle, ‘greedy’, [], [], SVMHandle, SelectedFeatures, Score)
disp_message (WindowHandle, [‘Selected:’,SelectedFeatures], ‘window’, 120, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - Free memory, because the training data is no longer used
clear_class_train_data (ClassTrainDataHandle) - Classify pills in test images
- using the automatically trained classifier with the
- automatically selected features
dev_set_line_width (4)
dev_set_colored (12)
for I := 1 to 3 by 1- Segment pills in test image
read_image (Image, ‘color/pills_test_’ + I$‘.2d’)
dev_display (Image)
segment_pills (Image, Pills) - For all pills, calculate selected features
- using the calculate_features procedure from the
- calculate_feature_set library and classify them.
PillsIDs := []
count_obj (Pills, NPills)
for P := 1 to NPills by 1
select_obj (Pills, PillSelected, P)
calculate_features (PillSelected, Image, SelectedFeatures, Features)
classify_class_svm (SVMHandle, real(Features), 1, Class)- Display results
PillsIDs := [PillsIDs,Class]
dev_set_color (PillColors[Class])
dev_display (PillSelected)
area_center (PillSelected, Area, Row, Column)
disp_message (WindowHandle, Class + 1, ‘image’, Row, Column - 10, ‘black’, ‘true’)
endfor
disp_message (WindowHandle, ‘Classify image ’ + I + ’ of 3 using following features:’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_message (WindowHandle, SelectedFeatures, ‘window’, 40, 12, ‘black’, ‘true’)
if (I < 3)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
- Display results
- Segment pills in test image
- Clean up memory
clear_class_svm (SVMHandle)
pills_class_01.png
pills_class_02.png
pills_class_03.png
pills_class_04.png
pills_class_05.png
pills_class_06.png
pills_class_07.png
pills_class_08.png
pills_class_09.png
pills_class_010.png
pills_test_01.png
pills_test_02.png
pills_test_03.png
以下是程序运行结果:
以下是代码的主要步骤和功能解释:
初始化和图像读取:
dev_close_window() 和 dev_update_off() 用于关闭当前窗口并停止窗口更新。
read_image(Image, ‘color/pills_class_1’) 用于读取训练图像。
设置绘图参数:
dev_open_window_fit_image() 用于打开一个窗口并适配图像大小。
dev_set_draw(), dev_set_line_width(), set_display_font() 和 dev_set_colored() 用于设置绘图样式。
定义药片名称和颜色:
PillNames 和 PillColors 分别存储药片的名称和对应的颜色代码。
查询特征和特征组:
query_feature_group_names() 和 query_feature_names_by_group() 用于查询可用的特征和特征组。
生成特征列表:
get_feature_names() 和 get_feature_lengths() 用于获取特征名称和特征长度。
创建训练数据结构:
create_class_train_data() 和 set_feature_lengths_class_train_data() 用于创建和设置分类器训练数据结构。
训练循环:
循环读取药片图像,使用 segment_pills() 进行分割,然后使用 calculate_features() 计算特征,并将样本添加到训练数据中。
特征选择:
select_feature_set_svm() 用于从训练数据中选择最优特征。
分类测试图像中的药片:
读取测试图像,使用 segment_pills() 分割药片,然后使用 classify_class_svm() 对药片进行分类。
结果显示:
使用 dev_display() 和 disp_message() 在图像上显示分割结果和分类信息。
清理资源:
clear_class_train_data() 和 clear_class_svm() 用于释放训练数据和分类器占用的内存。
这段代码是一个完整的机器学习流程,包括数据预处理、特征提取、特征选择、模型训练和测试。它展示了如何使用HALCON软件进行图像分类任务。