程序:
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’) //设置文字字体和大小等,fond的意思是字体
gen_rectangle1 (FuseOrange, 195, 90, 230, 120) //gen=generate生成的意思
gen_empty_obj (Classes) //建立一个空的对象
concat_obj (FuseOrange, FuseRed, Classes) //连接两个标志性的对象元组,以下几句是将这几个对象全部连接在一个元组里
disp_continue_message (WindowHandle, ‘black’, ‘true’) //在屏幕右下角生成一个单机继续的按钮
create_class_gmm (3, 5, 1, ‘full’, ‘none’, 3, 42, GMMHandle) //创建一个高斯混合模型类(gmm=Gaussian Mixture Model)
add_samples_image_class_gmm (Image, Classes, GMMHandle, 0) //将图像中的训练样本添加到高斯混合模型的训练数据中
train_class_gmm (GMMHandle, 100, 0.001, ‘training’, 0.001, Centers, Iter) //训练高斯混合模型。
create_class_lut_gmm (GMMHandle, [‘bit_depth’,‘rejection_threshold’], [6,0.03], ClassLUTHandle) //创建一个使用高斯混合模型分类字节图像查找表,(lut== look-up table )。
classify_image_class_lut (Image, ClassRegions, ClassLUTHandle) //使用查找表对字节图像进行分类
shape_trans (RegionFillUp, RegionTrans, ‘convex’) //转换区域的形状 trans=transform。
/*
* In this example five different color fuses are segmented with
* a look-up table classifier (LUT) based on a Gaussian Mixture
* Model (GMM).
*/
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 800, 600, 'white', WindowHandle)
dev_set_draw ('margin')
ImageRootName := 'color/color_fuses_0'
FuseTypes := [5,10,15,20,30]
FuseColors := ['Orange','Red','Blue','Yellow','Green']
FuseHighlight := ['orange','red','blue','goldenrod','forest green']
DisplayTextShift := [85,65,75,85,85]
dev_set_color ('white')
dev_set_line_width (2)
read_image (Image, ImageRootName + '0')
dev_display (Image)
/*
* Define ROIs for the training data of the classifier
*/
set_display_font (WindowHandle, 14, 'mono', 'true', 'false') //设置文字字体和大小等,fond的意思是字体
gen_rectangle1 (FuseOrange, 195, 90, 230, 120) //gen=generate生成的意思
dev_display (FuseOrange)
disp_message (WindowHandle, 'Orange Fuse', 'image', 160, 90 - 65, 'black', 'true')
gen_rectangle1 (FuseRed, 191, 280, 226, 310)
dev_display (FuseRed)
disp_message (WindowHandle, 'Red Fuse', 'image', 160, 280 - 55, 'black', 'true')
gen_rectangle1 (FuseBlue, 190, 470, 225, 500)
dev_display (FuseBlue)
disp_message (WindowHandle, 'Blue Fuse', 'image', 160, 470 - 60, 'black', 'true')
gen_rectangle1 (FuseYellow, 192, 672, 227, 702)
dev_display (FuseYellow)
disp_message (WindowHandle, 'Yellow Fuse', 'image', 160, 672 - 70, 'black', 'true')
gen_rectangle1 (FuseGreen, 197, 880, 232, 910)
dev_display (FuseGreen)
disp_message (WindowHandle, 'Green Fuse', 'image', 160, 880 - 65, 'black', 'true')
gen_empty_obj (Classes) //建立一个空的对象
concat_obj (FuseOrange, FuseRed, Classes) //连接两个标志性的对象元组,以下几句是将这几个对象全部连接在一个元组里
concat_obj (Classes, FuseBlue, Classes)
concat_obj (Classes, FuseYellow, Classes)
concat_obj (Classes, FuseGreen, Classes)
disp_message (WindowHandle, 'ROIs for the training data', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true') //在屏幕右下角生成一个单机继续的按钮
stop ()
*
* Create the GMM classifier, add the samples, and train it
create_class_gmm (3, 5, 1, 'full', 'none', 3, 42, GMMHandle) //创建一个高斯混合模型类(gmm==Gaussian Mixture Model)
add_samples_image_class_gmm (Image, Classes, GMMHandle, 0) //将图像中的训练样本添加到高斯混合模型的训练数据中
disp_message (WindowHandle, 'Training GMM classifier...', 'window', 48, 12, 'black', 'true')
train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.001, Centers, Iter) //训练高斯混合模型。
*
* Create the GMM-based LUT classifier
disp_message (WindowHandle, 'Creating LUT classifier...', 'window', 84, 12, 'black', 'true')
create_class_lut_gmm (GMMHandle, ['bit_depth','rejection_threshold'], [6,0.03], ClassLUTHandle) //创建一个使用高斯混合模型分类字节图像查找表,(lut== look-up table )。
*
* Segment images with LUT classifier
for Img := 0 to 3 by 1
read_image (Image, ImageRootName + Img)
count_seconds (T1)
classify_image_class_lut (Image, ClassRegions, ClassLUTHandle) //使用查找表对字节图像进行分类
count_seconds (T2)
TimeToClassify := (T2 - T1) * 1000
dev_display (Image)
dev_set_line_width (3)
for Fuse := 1 to 5 by 1
*
* Perform post-processing on returned classes
copy_obj (ClassRegions, ObjectsSelected, Fuse, 1) //复制HALCON数据库中的一个标志性对象。
closing_circle (ObjectsSelected, RegionClosing, 3.5) //使用圆形结构元素关闭区域
connection (RegionClosing, ConnectedRegions) //计算一个区域的连接组件
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 2500, 99999) ///借助形状特征选择区域。
fill_up (SelectedRegions, RegionFillUp)
area_center (RegionFillUp, Area, Row, Column)
shape_trans (RegionFillUp, RegionTrans, 'convex') //转换区域的形状 trans=transform。
dev_set_color (FuseHighlight[Fuse - 1])
dev_display (RegionTrans)
*
disp_message (WindowHandle, FuseColors[Fuse - 1] + ' ' + FuseTypes[Fuse - 1] + ' A', 'image', Row - 10, Column - DisplayTextShift[Fuse - 1], FuseHighlight[Fuse - 1], 'white')
endfor
disp_message (WindowHandle, TimeToClassify$'.1f' + ' ms', 'window', 12, 12, 'black', 'true')
if (Img < 3)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
*
disp_message (WindowHandle, 'No more lines to execute', 'window', 50, 12, 'black', 'true')