工控编程吧
标题:
GMM分类器识别水果种类实例
[打印本页]
作者:
qq263946146
时间:
2019-5-31 12:55
标题:
GMM分类器识别水果种类实例
(, 下载次数: 0)
上传
点击文件名下载附件
* 例程序展示如何应用一个通用的GMM分类器,再配合属性area,circularity来区分柑橘类水果
read_image (Image, 'color/citrus_fruits_01')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display (Image)
dev_update_window ('off')
dev_update_pc ('off')
dev_update_var ('off')
*
FeaturesArea := []
FeaturesCircularity := []
ClassName := ['orange','lemon']
*
* 创建一个GMM分类器
create_class_gmm (2, 2, 1, 'spherical', 'normalization', 10, 42, GMMHandle)
* 增加训练样本
for I := 1 to 4 by 1
read_image (Image, 'color/citrus_fruits_' + I$'.2d')
decompose3 (Image, ImageRed, ImageGreen, ImageBlue)
threshold (ImageRed, Region, 50, 255)
fill_up (Region, RegionFillUp)
connection (RegionFillUp, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 999999)
count_obj (SelectedRegions, NumberObjects)
for J := 1 to NumberObjects by 1
select_obj (SelectedRegions, ObjectSelected, J)
circularity (ObjectSelected, Circularity)
area_center (ObjectSelected, Area, RowRegionCenter, ColumnRegionCenter)
FeaturesArea := [FeaturesArea,Area]
FeaturesCircularity := [FeaturesCircularity,Circularity]
FeatureVector := real([Circularity,Area])
if (I <= 2)
add_sample_class_gmm (GMMHandle, FeatureVector, 0, 0)
disp_message (WindowHandle, 'Add to Class:' + ClassName[0], 'window', RowRegionCenter, ColumnRegionCenter - 100, 'black', 'true')
else
add_sample_class_gmm (GMMHandle, FeatureVector, 1, 0)
disp_message (WindowHandle, 'Add to Class:' + ClassName[1], 'window', RowRegionCenter, ColumnRegionCenter - 100, 'black', 'true')
endif
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
dev_clear_window ()
train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.0001, Centers, Iter)
stop ()
*执行分类
for I := 1 to 15 by 1
read_image (Image, 'color/citrus_fruits_' + I$'.2d')
dev_display (Image)
decompose3 (Image, ImageRed, ImageGreen, ImageBlue)
threshold (ImageRed, Region, 50, 255)
fill_up (Region, RegionFillUp)
connection (RegionFillUp, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 999999)
count_obj (SelectedRegions, NumberObjects)
for J := 1 to NumberObjects by 1
select_obj (SelectedRegions, ObjectSelected, J)
circularity (ObjectSelected, Circularity)
area_center (ObjectSelected, Area, RowRegionCenter, ColumnRegionCenter)
FeaturesArea := [FeaturesArea,Area]
FeaturesCircularity := [FeaturesCircularity,Circularity]
FeatureVector := real([Circularity,Area])
classify_class_gmm (GMMHandle, FeatureVector, 1, ClassID, ClassProb, Density, KSigmaProb)
disp_message (WindowHandle, 'Class: ' + ClassName[ClassID], 'window', RowRegionCenter, ColumnRegionCenter - 100, 'black', 'true')
disp_message (WindowHandle, 'KSigmaProb: ' + KSigmaProb, 'window', RowRegionCenter + 30, ColumnRegionCenter - 100, 'black', 'true')
endfor
if (I != 15)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
* 清空分类器,释放资源
clear_class_gmm (GMMHandle)
[halcon]1[/halcon]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4