* 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
* 定义ROI 进行颜色分类
set_display_font(WindowHandle,14,'mono','true','false')gen_rectangle1(FuseOrange,195,90,230,120)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)* 将图片和归类放入模型
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)* 清除高斯句柄
clear_class_gmm(GMMHandle)** Segment images with LUT classifier
for Img :=0 to 3 by 1read_image(Image, ImageRootName + Img)count_seconds(T1)* 进行分类classify_image_class_lut(Image, ClassRegions, ClassLUTHandle)count_seconds(T2)TimeToClassify :=(T2 - T1)*1000dev_display(Image)dev_set_line_width(3)for Fuse :=1 to 5 by 1** Perform post-processing on returned classescopy_obj(ClassRegions, ObjectsSelected, Fuse,1)* 闭运算先膨胀再腐蚀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')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')endfordisp_message(WindowHandle, TimeToClassify$'.1f'+' ms','window',12,12,'black','true')if(Img <3)disp_continue_message(WindowHandle,'black','true')stop()endif
endfor
*clear_class_lut(ClassLUTHandle)disp_message(WindowHandle,'No more lines to execute','window',50,12,'black','true')