HALCON示例程序classify_image_class_gmm.hdev使用GMM分类器分割彩色图片
示例程序源码(加注释)
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 735, 485, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
dev_set_colored (6)
dev_set_line_width (3)
read_image (Image, ‘patras’)
dev_display (Image)
-
定义颜色数组
Color := [‘indian red’,‘cornflower blue’,‘white’,‘black’,‘yellow’] -
为要进行分类的四个类创建矩形框并显示
gen_rectangle1 (Sea, 10, 10, 120, 270)
gen_rectangle2 (Deck, [170,400], [350,375], [-0.56,-0.75], [64,104], [26,11])
union1 (Deck, Deck)
gen_rectangle1 (Walls, 355, 623, 420, 702)
gen_rectangle2 (Chimney, 286, 623, -0.56, 64, 33)
concat_obj (Sea, Deck, Classes)
concat_obj (Classes, Walls, Classes)
concat_obj (Classes, Chimney, Classes)
dev_set_color (Color[0])
dev_display (Deck)
dev_set_color (Color[1])
dev_display (Sea)
dev_set_color (Color[2])
dev_display (Walls)
dev_set_color (Color[3])
dev_display (Chimney)
Message := ‘Training regions for the color classifier’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () -
创建gmm分类器
-
之前的博客已经介绍了使用方法https://blog.csdn.net/cashmood/article/details/90664731
create_class_gmm (3, 4, [1,10], ‘full’, ‘none’, 2, 42, GMMHandle) -
向GMM分类器中添加训练样本
add_samples_image_class_gmm (Image, Classes, GMMHandle, 2.0) -
显示图片
dev_display (Image) -
定义一个字符串变量,并进行初始化
Message := ‘Training …’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’) -
训练分类器
train_class_gmm (GMMHandle, 500, 1e-4, ‘uniform’, 1e-4, Centers, Iter)
Message := Message + ’ ready.’
Message[1] := ‘Segment image using the classifier …’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’) -
clear_samples_class_gmm - 清除高斯混合模型的训练数据。主要是了释放内存。
clear_samples_class_gmm (GMMHandle) -
使用GMM分类器对图片区域进行分类
classify_image_class_gmm (Image, ClassRegions, GMMHandle, 0.0001) -
使用区域的平均灰度值绘制区域。
region_to_mean (ClassRegions, Image, ImageClass) -
显示
dev_display (ImageClass)
Message[1] := Message[1] + ’ ready.’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’) -
清除GMM分类器的句柄,释放内存
clear_class_gmm (GMMHandle)
处理思路
这个例子介绍了使用GMM分类器对彩色图片进行分类的例子,选取多通道图片的感兴趣区域与背景可以对多通道图片进行快速分类。
后记
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。