Halcon---基本形态学操作分割豆子,并计数
count_pellets.hdev* 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
·
count_pellets.hdev
* 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, 'pellets')
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)
count_obj (RegionDilation, Number)
dev_display (Image)
dev_display (RegionDilation)
disp_message (WindowID, Number + ' pellets detected', 'window', 12, 12, 'black', 'true')

更多推荐


所有评论(0)