22-Halcon机器视觉实例入门:图像阈值分割-局部阈值分割
·
图像阈值分割-局部阈值分割
在Halcon中local_thresh算子实现局部阈值分割。其原型如下:
local_threshold(Image : Region : Method, LightDark, GenParamName, GenParamValue : )
- Image:输入图像
- Region:输出分割区域
- Method:分割策略
- LightDark:前景和背景的颜色
- GenParamName:参数名称,‘mask_size’, ‘range’, ‘scale’
- GenParamValue:参数GenParamName对应的值
local_threshold使用Method中给出的阈值方法对单通道图像进行分段,并返回Region中的分段区域。 目前该算子仅提供方法’adapted_std_deviation’。 该算法是文本二值化技术,为文档图像提供了良好的结果。
演示代码如下:
* Init display
dev_update_off ()
dev_close_window ()
read_image (Letters, 'letters')
dev_open_window_fit_image (Letters, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* the strength of the adapted_std_deviation method more clearly.
get_image_size (Letters, Width, Height)
gen_image_surface_first_order (ImageSurface, 'byte', 0.5, 0.5, 0.5, Width / 2, Height / 2, Width, Height)
add_image (Letters, ImageSurface, ImageCombined, 0.5, 0)
GlobalThreshold := 128
threshold (ImageCombined, RegionGlobal, 0, GlobalThreshold)
dev_display (ImageCombined)
dev_display (RegionGlobal)
Message := 'In images with inhomogeneous background'
Message[1] := 'it is impossible to segment the characters'
Message[2] := 'with a global threshold.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
local_threshold (ImageCombined, Region, 'adapted_std_deviation', 'dark', [], [])
*
* Display results
dev_display (ImageCombined)
dev_display (Region)
Message := 'With a local threshold, the characters'
Message[1] := 'can be segmented successfully.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
原图像:

程序运行结果:

更多推荐

所有评论(0)