Halcon分类----高斯混合模型GMM
前言
现有的图像中目标的分类常用深度学习模型处理,但是深度学习需要大量模型处理。对于明显提取的目标,常常有几个明显特征,利用这几个明显特征使用少量图片便可以完成图像目标分类工作。这里介绍使用高斯混合模型GMM处理图像。
常用算子及流程
1. 先提取特征,提取区域特征(或者边缘,灰度特征等)
*计算区域圆度
circularity(区域,圆度值)
*计算区域面积
area_center(区域,面积)
*将两个特征转化为一个向量中
FeatureVector:=real([Circularity,Area])
2. 定义特征类
*定义特征面积
FeaturesArea:=[]
*定义特征圆度
FeaturesName:=[]
*定义类
ClassName:=['充电器','瓶盖']
3. 创建分类高斯模型
creat_class_gmm(2,2,1,'spherical','normalization',10,42,GMMHandle)
//creat_class_gmm(2个类,2个特征,要分为1个,'spherical','normalization',10,42,GMMHandle)
之后,将瓶盖的面积和圆度放到类中
add_sample_class_gum(GMMHandle,FeatureVector,0,0)
//add_sample_class_gum(GMMHandle,要存储的训练向量样本,要存储的训练样本的类ID,加载到样本的标准差)
4. 训练高斯混合模型
train_class_gmm(GMMHandle,100,0.001,'training',0.0001,centers,Iter)
//train_class_gmm(GMMHandle,最大迭代次数,期望偏差,'training',0.0001,每个类找到的中心数,每个迭代次数)
5. 使用高斯混合模型分类
classify_class_gmm(GMMHandle,FeatureVector,1,classID,classProb,Density,KSigmaProb)
//classify_class_gmm(GMMHandle,当前物体的特征向量,要确定的最大类数,对应className中的ID数,类后验概率,概率密度,特征向量概率)
完整的高斯混合模型的案例如下,提取图像特征用的笨方法,一个个提取的,如果成像比较好的话完全可以使用for循环,这里只是方便大家理解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | dev_close_window () dev_open_window (0, 0, 512, 512, 'black' , WindowHandle) *已知瓶盖1 read_image (Image, 'D:/瓶盖.bmp.tif' ) rgb1_to_gray (Image, GrayImage) threshold (GrayImage, Regions1, 0, 32) connection (Regions1, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area' , 'and' , 210952, 500000) select_shape (SelectedRegions, SelectedRegions1, 'phi' , 'and' , 0.4861, 0.8447) fill_up (SelectedRegions1, RegionFillUp) circularity (RegionFillUp, Circularity) area_center (RegionFillUp, Area, Row, Column) *已知瓶盖2 read_image (Image2, 'D:/1.bmp.tif' ) rgb1_to_gray (Image2, GrayImage2) threshold (GrayImage2, Regions2, 127, 255) connection (Regions2, ConnectedRegions2) select_shape (ConnectedRegions2, SelectedRegions3, 'area' , 'and' , 147143, 500000) fill_up (SelectedRegions3, RegionFillUp2) circularity (RegionFillUp2, Circularity2) area_center (RegionFillUp2, Area2, Row2, Column2) *已知瓶盖3 read_image (Image3, 'D:/2.bmp.tif' ) rgb1_to_gray (Image3, GrayImage3) threshold (GrayImage3, Regions3, 0, 36) connection (Regions3, ConnectedRegions3) select_shape (ConnectedRegions3, SelectedRegions4, 'area' , 'and' , 210000, 500000) select_shape (SelectedRegions4, SelectedRegions5, 'area' , 'and' , 210000, 305453) fill_up (SelectedRegions5, RegionFillUp3) circularity (RegionFillUp3, Circularity3) area_center (RegionFillUp3, Area3, Row3, Column3) *已知瓶盖4 read_image (Image4, 'D:/3.bmp.tif' ) rgb1_to_gray (Image4, GrayImage4) threshold (GrayImage4, Regions4, 121, 255) connection (Regions4, ConnectedRegions4) select_shape (ConnectedRegions4, SelectedRegions6, 'area' , 'and' , 280957, 452627) fill_up (SelectedRegions6, RegionFillUp4) circularity (RegionFillUp4, Circularity4) area_center (RegionFillUp4, Area4, Row4, Column4) *已知充电器1 read_image (Image1, 'D:/充电器.bmp.tif' ) rgb1_to_gray (Image1, GrayImage1) threshold (GrayImage1, Regions, 1, 32) connection (Regions, ConnectedRegions1) select_shape (ConnectedRegions1, SelectedRegions2, 'area' , 'and' , 498095, 1e+006) fill_up (SelectedRegions2, RegionFillUp1) circularity (RegionFillUp1, Circularity1) area_center (RegionFillUp1, Area1, Row1, Column1) *已知充电器2 read_image (Image5, 'D:/11.bmp.tif' ) rgb1_to_gray (Image5, GrayImage5) threshold (GrayImage5, Regions5, 124, 251) connection (Regions5, ConnectedRegions5) select_shape (ConnectedRegions5, SelectedRegions7, 'area' , 'and' , 549524, 1e+006) fill_up (SelectedRegions7, RegionFillUp5) circularity (RegionFillUp5, Circularity5) area_center (RegionFillUp5, Area5, Row5, Column5) *已知充电器3 read_image (Image6, 'D:/22.bmp.tif' ) rgb1_to_gray (Image6, GrayImage6) threshold (GrayImage6, Regions7, 152, 244) connection (Regions7, ConnectedRegions6) select_shape (ConnectedRegions6, SelectedRegions8, 'area' , 'and' , 293621, 1e+006) fill_up (SelectedRegions8, RegionFillUp6) circularity (RegionFillUp6, Circularity6) area_center (RegionFillUp6, Area6, Row6, Column6) *已知充电器4 read_image (Image7, 'D:/33.bmp.tif' ) rgb1_to_gray (Image7, GrayImage7) threshold (GrayImage7, Regions6, 0, 34) connection (Regions6, ConnectedRegions7) select_shape (ConnectedRegions7, SelectedRegions9, 'area' , 'and' , 293621, 1e+006) fill_up (SelectedRegions9, RegionFillUp7) circularity (RegionFillUp7, Circularity7) area_center (RegionFillUp7, Area7, Row7, Column7) ClassNum:=[ '瓶盖' , '充电器' ] *创建一个高斯混合模型分类器 create_class_gmm (2, 2, 1, 'spherical' , 'normalization' , 10, 42, GMMHandle) *增添瓶盖的特征 FeatureVector:=real([Circularity,Area]) add_sample_class_gmm (GMMHandle, FeatureVector, 0, 0) FeatureVector2:=real([Circularity2,Area2]) add_sample_class_gmm (GMMHandle, FeatureVector2, 0, 0) FeatureVector3:=real([Circularity3,Area3]) add_sample_class_gmm (GMMHandle, FeatureVector3, 0, 0) FeatureVector4:=real([Circularity4,Area4]) add_sample_class_gmm (GMMHandle, FeatureVector4, 0, 0) *增添充电器的特征 FeatureVector1:=real([Circularity1,Area1]) add_sample_class_gmm (GMMHandle, FeatureVector1, 1, 0) FeatureVector5:=real([Circularity5,Area5]) add_sample_class_gmm (GMMHandle, FeatureVector5, 1, 0) FeatureVector6:=real([Circularity6,Area6]) add_sample_class_gmm (GMMHandle, FeatureVector6, 1, 0) FeatureVector7:=real([Circularity7,Area7]) add_sample_class_gmm (GMMHandle, FeatureVector7, 1, 0) *训练高斯模型 train_class_gmm (GMMHandle, 100, 0.001, 'training' , 0.0001, Centers, Iter) *读取现有瓶盖模型 read_image (Image8, 'D:/验证集.bmp.tif' ) rgb1_to_gray (Image8, GrayImage8) threshold (GrayImage8, Regions8, 128, 253) connection (Regions8, ConnectedRegions8) select_shape (ConnectedRegions8, SelectedRegions10, 'area' , 'and' , 150952, 500000) fill_up (SelectedRegions10, RegionFillUp8) circularity (RegionFillUp8, Circularity8) area_center (RegionFillUp8, Area8, Row8, Column8) *读取现有充电器模型 read_image (Image9, 'D:/充电器验证集.bmp.tif' ) rgb1_to_gray (Image9, GrayImage9) threshold (GrayImage9, Regions9, 113, 255) connection (Regions9, ConnectedRegions9) select_shape (ConnectedRegions9, SelectedRegions11, 'area' , 'and' , 787619, 1e+006) fill_up (SelectedRegions11, RegionFillUp9) circularity (RegionFillUp9, Circularity9) area_center (RegionFillUp9, Area9, Row9, Column9) *区分时,将现有瓶盖物体特征输入 FeatureVector8:=real([Circularity8,Area8]) classify_class_gmm (GMMHandle, FeatureVector8, 1, ClassID, ClassProb, Density, KSigmaProb) *分类后显示瓶盖类目名称 dev_display (Image8) disp_message (WindowHandle, '类别:' +ClassNum[ClassID], 'window' , 0,0, 'black' , 'true' ) stop () *区分时,将现有充电器物体特征输入 FeatureVector9:=real([Circularity9,Area9]) classify_class_gmm (GMMHandle, FeatureVector9, 1, ClassID, ClassProb, Density, KSigmaProb) dev_display (Image9) disp_message (WindowHandle, '类别:' +ClassNum[ClassID], 'window' , 0,0, 'black' , 'true' ) *清除分类器,释放内容 clear_class_gmm (GMMHandle) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话