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.用分類器對測試圖片進行分類app
* 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)oop