打开halcon,按下ctrl+e打开halcon自带例程。工业领域->制药业->classify_pills_auto_select_features.hdev
* 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.
*
* 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.
*
* 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.
*定义我们用来分类的特征,这里用区域和颜色来做分类,区域有分为很多特征类型,比如面积,圆度等等,而color下有hls_mean,rgb_mean等等
FeatureGroups := ['region','color']
*获得特征的名字,通过我们选择的组选择其对应的特征名字,选中组中的全部,比如这里region组下有area,width,height等
get_feature_names (FeatureGroups, FeatureNames)
*计算总的用作分类的特征数目,说白了就是特征向量的维度,这里总共是38,FeatureLenth下面可能还有子特征,这里都是一维
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)
decompose3 (Image, ImageR, ImageG, ImageB)
threshold (ImageR, RegionR, 0, 60)
threshold (ImageB, RegionB, 0, 100)
*联结图像
union2 (RegionR, RegionB, RegionUnion)
*闭运算
closing_circle (RegionUnion, RegionClosing, 2.5)
*分割图像
connection (RegionClosing, ConnectedRegions)
*通过面积特征筛选出真正的药粒区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
*填充
fill_up (SelectedRegions, 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 ()
*
* 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)
decompose3(Image, ImageR, ImageG, ImageB)
threshold (ImageR, RegionR, 0, 60)
threshold (ImageB, RegionB, 0, 100)
*联结图像
union2 (RegionR, RegionB, RegionUnion)
*闭运算
closing_circle (RegionUnion, RegionClosing, 2.5)
*分割图像
connection (RegionClosing, ConnectedRegions)
*通过面积特征筛选出真正的药粒区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
*填充
fill_up (SelectedRegions, 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
*选中第P个区域
select_obj (Pills, PillSelected, P)
*计算这个区域的特征值
calculate_features (PillSelected, Image, SelectedFeatures, Features)
*进行分类
*第一个参数SVM模型句柄
*第二个参数:特征值
*第三个参数:分到的类别的数目,1,分到的最符合的那一类
*第四个参数:类指示值
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
特征训练图片
药粒分类识别结果