Halcon阈值算子 -- 局部阈值之var_threshold

Halcon阈值算子 – 局部阈值之var_threshold

与local_threshold类似,var_threshold也是基于局部均值和方差。只不过计算公式不一样。
算子的函数签名如下:
var_threshold(Image : Region : MaskWidth, MaskHeight, StdDevScale, AbsThreshold, LightDark : )
下面介绍它的阈值计算公式:

  • 令g(x,y)为点(x,y)处的灰度值,m(x,y) 为均值,d(x,y) 为方差(实际是标准差),则点(x,y)处的阈值v(x,y)定义为:

在这里插入图片描述

  • 当LightDark = ‘light’:时,满足下述条件的点被选中到输出区域
    在这里插入图片描述

  • 当LightDark = ‘dark’:时,满足下述条件的点被选中到输出区域
    在这里插入图片描述

  • 当LightDark = ‘equal’:时,满足下述条件的点被选中到输出区域

在这里插入图片描述

  • 当LightDark = ‘not_equal’:时,满足下述条件的点被选中到输出区域

在这里插入图片描述

我们来和local_threshold算子的计算公式做个对比:

  • 都是基于均值和方差
  • 都可以设置均值运算窗口大小。local_threshold是通过设置mask_size,var_threshold是通过设置MaskWidth, MaskHeight
  • 都有一个方差的比例因子。local_threshold的就是参数k,var_threshold的就是参数StdDevScale
  • local_threshold中的方差通过参数R做了归一化。var_threshold未做这样的处理
  • var_threshold有一个特别之处:参数AbsThreshold设置了最小灰度差。如果图像的某一区域背景光照很均匀,那么方差就很小。此时,根据上面计算阈值v(x,y)的公式,AbsThreshold就开始起作用。

以下实例来源于官方帮助文档。

list_image_files ('label', 'default', [], ImageFiles)
ImageFiles1 := regexp_select(ImageFiles,'label_...png$')
* 
* Open a window to display the results.
read_image (Image, ImageFiles1[0])
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 
* Loop over all images.
for Index := 0 to |ImageFiles1| - 1 by 1
    read_image (Image, ImageFiles1[Index])
    * Threshold the image.
    var_threshold (Image, Region, 15, 15, 1.01, 40, 'dark')
    * Select regions of particular height and size.
    connection (Region, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, ['height','area'], 'and', [20,100], [100,400])
    * Display the results
    dev_display (Image)
    dev_display (SelectedRegions)
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
disp_end_of_program_message (WindowHandle, 'black', 'true')

待分割图像如下
在这里插入图片描述
分割后结果如下
在这里插入图片描述