本文主要是介绍Halcon颜色抽取-多产品颜色(MLP),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
//颜色抽取,单产品多区域
https://blog.csdn.net/m0_51559565/article/details/135216905
由于在颜色抽取上面,我已经发了一篇博客了,是单产品多区域的情况。所以对于细分原理上就不过多的描述了。
本文主要是针对多产品多颜色的情况进行说明。实际生产线中,往往会出现n种产品同时存在的情况,他们可能颜色不同,或者颜色及其相近。其中本文章就针对5种相近颜色的不同产品进行说明。
1.halcon程序
*创建MLP分类器,区分6种颜色区域。5种所需要的颜色,1种背景create_class_mlp (3, 7, 6, 'softmax', 'normalization', 3, 42, MLPHandle)
dev_get_window (WindowHandle)list_files ('D:/2024Work/work/4.11颜色分选/砖石', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1read_image (Image, ImageFiles[Index])*创建6个分类的空区域gen_empty_region (EmptyObject1)gen_empty_region (EmptyObject2)gen_empty_region (EmptyObject3)gen_empty_region (EmptyObject4)gen_empty_region (EmptyObject5)gen_empty_region (back)*创建存放区域的集合数组gen_empty_obj (Union)*绘制我们需要的颜色区域dev_display (Image)draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)gen_contour_region_xld (Rectangle, Contours, 'border')dev_display (Image)dev_display (Contours)reduce_domain (Image, Rectangle, ROI)*绘制背景dev_display (Image)draw_rectangle1 (WindowHandle, Row11, Column11, Row21, Column21)gen_rectangle1 (Rectangle1, Row11, Column11, Row21, Column21)gen_contour_region_xld (Rectangle1, Contours1, 'border')dev_display (Image)dev_display (Contours1)reduce_domain (Image, Rectangle1, back)*第一种颜色if (Index=0)concat_obj (Union, ROI, Union)concat_obj (Union, EmptyObject2, Union)concat_obj (Union, EmptyObject3, Union)concat_obj (Union, EmptyObject4, Union)concat_obj (Union, EmptyObject5, Union)concat_obj (Union, back, Union)endif*第二种颜色if (Index=1)concat_obj (Union, EmptyObject1, Union)concat_obj (Union, ROI, Union)concat_obj (Union, EmptyObject3, Union)concat_obj (Union, EmptyObject4, Union)concat_obj (Union, EmptyObject5, Union)concat_obj (Union, back, Union)endif*第三种颜色if (Index=2)concat_obj (Union, EmptyObject1, Union)concat_obj (Union, EmptyObject2, Union)concat_obj (Union, ROI, Union)concat_obj (Union, EmptyObject4, Union)concat_obj (Union, EmptyObject5, Union)concat_obj (Union, back, Union)endif*第四种颜色if (Index=3)concat_obj (Union, EmptyObject1, Union)concat_obj (Union, EmptyObject2, Union)concat_obj (Union, EmptyObject3, Union)concat_obj (Union, ROI, Union)concat_obj (Union, EmptyObject5, Union)concat_obj (Union, back, Union)endif*第五种颜色if (Index=4)concat_obj (Union, EmptyObject1, Union)concat_obj (Union, EmptyObject2, Union)concat_obj (Union, EmptyObject3, Union)concat_obj (Union, EmptyObject4, Union)concat_obj (Union, ROI, Union)concat_obj (Union, back, Union)endif*将全部颜色添加到mlp中add_samples_image_class_mlp (Image, Union, MLPHandle)
endfor
*训练mlp
train_class_mlp (MLPHandle, 400, 0.5, 0.01, Error, ErrorLog)
stop ()
******************************************验证**********************
*将句柄写入文件中
write_class_mlp (MLPHandle, 'D:/2024Work/work/4.11颜色分选/砖石/mlp.mlp')
*从文件中读取句柄
read_class_mlp ('D:/2024Work/work/4.11颜色分选/砖石/mlp.mlp', MLPHandle1)
*初始化结果参数
ResultColor:=0
list_files ('D:/2024Work/work/4.11颜色分选/砖石', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1read_image (Image, ImageFiles[Index])*对图像应该分类器classify_image_class_mlp (Image, ClassRegions, MLPHandle1, 0.5)*通过循环和面积筛选,判断当前的颜色for i := 1 to 5 by 1select_obj (ClassRegions, Result, i)union1 (Result, RegionUnion)area_center (RegionUnion, Area1, Row3, Column3)if (Area1>100000)ResultColor:=iendifendforstop ()
endfor
2.halcon程序解析
2.1创建多颜色mlp模型
*创建MLP分类器,区分6种颜色区域。5种所需要的颜色,1种背景
create_class_mlp (3, 7, 6, 'softmax', 'normalization', 3, 42, MLPHandle)
在本文中,需要区分的产品是5个蓝色的石头,颜色相近,所以在创建mlp模型时,需要使用5个区分项和背景。总共6个区分项
2.2创建存放数组和绘制ROI
gen_empty_region (EmptyObject1)gen_empty_region (EmptyObject2)gen_empty_region (EmptyObject3)gen_empty_region (EmptyObject4)gen_empty_region (EmptyObject5)gen_empty_region (back)*创建存放区域的集合数组gen_empty_obj (Union)*绘制我们需要的颜色区域dev_display (Image)draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)reduce_domain (Image, Rectangle, ROI)*绘制背景dev_display (Image)draw_rectangle1 (WindowHandle, Row11, Column11, Row21, Column21)gen_rectangle1 (Rectangle1, Row11, Column11, Row21, Column21)reduce_domain (Image, Rectangle1, back)
在绘制我们需要的颜色的时候,只需要绘制的区域包含所需颜色即可,但是同时应该尽量避免出现其他颜色。
在绘制背景时,应该将大部分背景的颜色的区域尽量的包括其中,这样子可以有效的区分背景。
2.3训练模型
add_samples_image_class_mlp (Image, Union, MLPHandle)
*训练mlp
train_class_mlp (MLPHandle, 400, 0.5, 0.01, Error, ErrorLog)
每次绘制完图像后,需要对区域进行添加,将所需要训练的内容添加到模型中,然后开启模型训练
2.4写入和读取文件
*将句柄写入文件
write_class_mlp (MLPHandle, 'D:/2024Work/work/4.11颜色分选/砖石/mlp.mlp')
*从文件中读取句柄
read_class_mlp ('D:/2024Work/work/4.11颜色分选/砖石/mlp.mlp', MLPHandle1)
2.5验证
*初始化结果参数
ResultColor:=0
list_files ('D:/2024Work/work/4.11颜色分选/砖石', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1read_image (Image, ImageFiles[Index])*对图像应该分类器classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5)*通过循环和面积筛选,判断当前的颜色for i := 1 to 5 by 1select_obj (ClassRegions, Result, i)union1 (Result, RegionUnion)area_center (RegionUnion, Area1, Row3, Column3)if (Area1>100000)ResultColor:=iendifendforstop ()
endfor
将图像输入到图像分类器中,即可得到所需要的颜色的面积,根据颜色面积进行筛选即可
总结
有不清楚的地方,可以移步前言的链接查看。mlp常用的场景一般都是单颜色区分,相当多时候是在预处理中进行。但是作为项目上,常见于各种颜色有色差的分类。比如产品有色差的情况,也是可以用颜色抽取实现。
这篇关于Halcon颜色抽取-多产品颜色(MLP)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!