Halcon算子学习——图像阈值分割算子

图像二值化是图像分析与处理中最常见最重要的处理手段,二值处理方法也非常多。越精准的方法计算量也越大。

一、threshold-全局固定阈值分割

threshold(Image : Region : MinGray, MaxGray : )

使用全局固定阈值分割图像,阈值从输入图像中选取灰度值g满足以下条件的像素点

满足条件的图像的所有点作为一个区域返回。如果传递多个灰度值间隔(MinGray和MaxGray的元组),则每个间隔返回一个单独的区域。对于向量场图像,阈值不应用于灰度值,而是应用于向量的长度。 

read_image (Image, 'F:/2.jpg')
*计算直方图
gray_histo (Image, Image, AbsoluteHisto, RelativeHisto)
*将直方图转换为区域(包含直方图的区域;输入的直方图;直方图中心的行/列坐标;直方图的比例因子)
gen_region_histo (Region, AbsoluteHisto, 255, 255, 1)
*利用直方图获取阈值
histo_to_thresh (AbsoluteHisto,10, MinThresh, MaxThresh)
*期望阈值
TarGetGray:=23
for Index := |MinThresh|-1 to 0 by -1
 if(MinThresh[Index]<=TarGetGray)
 	 	 MinThresh:= MinThresh[Index]
  	 break
  	 endif
 endfor
 for Index1 := 0 to |MaxThresh|-1 by 1
 	  if (MaxThresh[Index1]>=TarGetGray)
  	  		 MaxThresh:=MaxThresh[Index1]
 	  break
      endif
 endfor
*全局阈值分割
threshold (Image, Region1, MinThresh, MaxThresh)

 二、auto_threshold

自动根据绝对灰度直方图中的灰度分布生成区域,其实是根据平滑的sigma的图像和灰度值,选取每个山峰为独立区域,分割区域是以谷底(最小值)为准!

read_image (Image, 'F:/2.jpg')
median_image (Image, Median, 'circle', 3, 'mirrored')

*使用灰度直方图分割
* gray_histo (Median, Median, AbsoluteHisto, RelativeHisto)
* gen_region_histo (Region, AbsoluteHisto, 255, 255, 1)
* histo_to_thresh (AbsoluteHisto, 1, MinThresh, MaxThresh)
* threshold (Median, Region1, MinThresh, MaxThresh)
* connection (Region1, Connected1)


*使用自动多阈值分割
auto_threshold (Median, Region2, 1.0)

 

 三、Binary Threshold-自动全局阈值分割

binary_threshold(Image : Region : Method, LightDark : UsedThreshold)
——自动全局阈值分割(自动确定的全局阈值分割单通道图像,并在区域中返回分割后的区域)
输入:

    Image:需要进行阈值的图像
    Region:处理后的区域

输出:

    Method:分割方法(‘max_separability’:最大限度的可分性, ‘smooth_histo’:直方图平滑)
    LightDark:提取的是黑色部分还是白色部分
    UsedThreshold:自动阈值使用的阈值值
 

read_image (Image, 'F:/2.jpg')
median_image (Image, Median, 'circle', 3, 'mirrored')
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)

 

例如,分割均匀照明的背景上有用的字符。binary_threshold还返回UsedThreshold中使用的阈值。
使用的阈值由方法中给出的方法确定。目前,该操作符提供了以下两个方法:
’max_separability’:最大限度的可分性, ‘smooth_histo’:直方图平滑。
这两种方法只能用于具有双峰直方图的图像。

四、dyn_threshold-局部动态阈值分割

dyn_threshold(OrigImage, ThresholdImage : RegionDynThresh : Offset, LightDark : )
——局部动态阈值分割 Dynamic Threshold

    ThresholdImage:是我们用来作为灰度值参考的另外一幅图像,在实际使用过程中通常都是对原图像OrigImage进行一次平滑处理,然后用平滑处理之后得到的图像作为参考图像。
    LightDark:是问我们提取亮?暗?还是相似的区域?(当然,这是相对于ThresholdImage)
    Offset:其实是在设定一个比较的区间范围,因为在图像处理这个主观性本来就比较强的领域中“绝对”这个科学名次实在有点太过分,所以什么事情,只要在一个合理的范围内,我们都是可以接受的,而不是死死抓住一个点不放,最后也得不到想要的结果。
 

read_image (Image, 'F:/2.jpg')
mean_image (Image, ImageMean, 9, 9)
dyn_threshold (Image, ImageMean, RegionDynThresh, 10,'light')

小结:

threshold是最简单的阈值分割算子,理解最为简单;binary_threshold是自动阈值算子,它可以自动选出暗(dark)的区域,或者自动选出亮(light)的区域,理解起来也没有难度。
动态阈值算子dyn_threshold理解起来稍微复杂一点,使用dyn_threshold算子的步骤基本是这样的:

① 将原图进行滤波模糊处理。

② 用原图和模糊后的图逐个像素做比较,它可以根据参数分割出原图比模糊后的图灰度高(或者低)若干个灰度值的区域。
 

参考:

https://blog.csdn.net/Vichael_Chan/article/details/102576060

猜你喜欢

转载自blog.csdn.net/sinat_31608641/article/details/107545573