关于Halcon分类计数面积(三合一)

一、身为unity程序员最近在研究Halcon,接到了公司这样一个技术验证的需求:
1.根据图片中物体体积大小分类 Count功能
2.根据类型 算出每种类型的物体面积
参考HalconCount案例代码和图片改动了一下(图片内三个白的圆自己另加的,为了好做区分)
在这里插入图片描述

* This programs demonstrates the use of basic morphology
* operators.
* The aim of the program is to detect each single pellet
* (bright particle on a darker background).
* 
dev_update_off ()
*路径
read_image (Image, 'D:/Users/sk/Desktop/pellets.png')
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_set_part (0, 0, Height - 1, Width - 1)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_set_colored (6)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
disp_message (WindowID, 'Detect each single pellet', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Segment the regions of the pellets from the background
*二进制阈值分割图像
binary_threshold (Image, LightRegion, 'max_separability', 'light', UsedThreshold)
* 利用圆进行开运算,清除小圆点
opening_circle (LightRegion, Region, 3.5)
dev_display (Region)
disp_message (WindowID, 'First, segment the pellets', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Compute the connected pellet regions
* Note, that this approach fails, because some of
* the pellets are still connected.
*筛选出直接分割连通域的错误演示,因为这个时候还有豆子黏连在一起。
connection (Region, ConnectedRegionsWrong)
dev_display (Image)
dev_display (ConnectedRegionsWrong)
disp_message (WindowID, 'Simple connection fails', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Separate each pellet from the others by erosion
*使用圆形元素进行腐蚀运算
erosion_circle (Region, RegionErosion, 7.5)
dev_display (Image)
dev_display (RegionErosion)
disp_message (WindowID, 'Erosion of the pellet regions', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Now, compute the connected pellet regions
*现在进行连通域分割
connection (RegionErosion, ConnectedRegions)
dev_display (Image)
dev_display (ConnectedRegions)
disp_message (WindowID, 'Perform connection now', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Turn back to the original pellet size by applying a dilation
*恢复豆子原本区域,使用圆形元素进行膨胀。
dilation_circle (ConnectedRegions, RegionDilation, 7.5)
*--------------------------------改动 
*A类 语句可以用 特征直方图来筛选插入代码
select_shape (RegionDilation, SelectedRegions, 'area', 'and', 2477, 4000)
*A类统计
count_obj (SelectedRegions, Number)
stop ()
*B类
select_shape (RegionDilation, SelectedRegions1, 'area', 'and', 219.93, 2428.19)
*B类统计
count_obj (SelectedRegions1, Number)
stop ()
*对区域进行排序
*   sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'row')
*计算每个区域的面积 中心坐标 
*  area_center (SortedRegions, Area, Row, Column)
**测得A类的面积和中心位置 SelectedRegions:这里指的是那三个大圆 也可根据情况去做18个的小圆
area_center (SelectedRegions, Area, Row, Column)
**获取一个字符串的空间大小
get_string_extents (WindowID, 12345, Ascent, Descent, TxtWidth, TxtHeight)
*disp_message (WindowID, Area, 'image', Row - TxtHeight / 2, Column - TxtWidth / 2, 'white', 'false')
*------------------------------------------------------------
*对区域进行计数
* count_obj (RegionDilation, Number)
* dev_display (Image)
* dev_display (RegionDilation)
* disp_message (WindowID, Number + ' pellets detected', 'window', 12, 12, 'black', 'true')

运行之后,最终结果 Number 个数18(小圆) 面积(这里不是实际面积)Area{三个大圆}:

最终呈现的结果
用到的工具:
体积类型筛选

参考:
官网Demo 打上Count 第二个就是
https://blog.csdn.net/weixin_43734374/article/details/107976570

猜你喜欢

转载自blog.csdn.net/quailchivalrous/article/details/122240220