HALCON12里的example,classify_pills_auto_select_features.hdev.
执行流程:
1.选取相关特征(本例选取color和region组的所有特征)
(本例用get_feature_names( : : GroupNames : Names),根据特征组名得到一系列特征
2.创建训练数据,添加样本的相关特征至训练数据结构中。
(本例用calculate_features(Region, Image : : FeatureNames : Features)),根据特征名计算特征
3.自动选择最适合特征
(本例用select_feature_set_svm( : : ClassTrainDataHandle, SelectionMethod, GenParamNames, GenParamValues : SVMHandle,SelectedFeatureIndices, Score)
4.用分类器对测试图片进行分类
* This example shows how to use the calculate_feature_set
* procedure library together with the automatic feature selection
* to classify different pill types using a SVM classifier.
* 该示例演示如何从calculate_feature得到的特征,进行自动最佳特征选择,
* 并使用SVM分类器对各类型的pill进行分类。
* First, the pills are segmented in some training images.
* Then, a list of color and region features are calculated
* for each pill and stored in a classifier training data structure.
* After that, the best features are automatically selected
* with the operator select_feature_set_svm, and finally
* the resulting classifier is applied on a number of test
* images.
* 首先,对包含有pills的训练图片进行分割。
* 计算color和region组相关的特征。
* 每1个pill的相关特征添加至分类器训练数据结构里。
* 所有的训练数据添加完毕,用select_feature_set_svm
* 也可以用其他的分类器类型选择比如(select_feature_set_knn(mlp,gmm))
* 自动选择最佳特征,最后应用分类器对测试图像分类。
* Init visualization
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.
* 获取相关特征组的对应的特征名称(1个特征组包含许多特征),比如region组,有area,width,height,phi,ra,rb等。
* 获取相关特征的特征向量长度,比如rgb_mean特征,特征向量长度为3(每个通道对应1个值),area特征向量长度为1。
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)
* 以上注意第二参数‘feature_column‘的选择,此例中由于Number的数大于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
* Clean up memory
* 清除内存
clear_class_svm (SVMHandle)
原文地址:https://www.cnblogs.com/touch-skyer/p/9087486.html