halcon学习之阈值分割(threshold、binary_threshold、dyn_threshold、var_threshold、auto_threshold、fast_threshold、)


1. threshold(Image : Region : MinGray, MaxGray : )

全局阈值分割,适用于环境稳定,目标与背景存在明显的灰度差的场合。

应用1:利用灰度直方图确定阈值进行图像分割。一般是物体与背景之间存在一个明显的灰度差,直方图会存在两个波峰一个是目标一个是背景,那么阈值就是物体与背景之间的最小值。

       read_image (Image, 'clip')

       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)

扫描二维码关注公众号,回复: 11293562 查看本文章

                 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)

2. binary_threshold(Image : Region : Method, LightDark : UsedThreshold)

自动全局阈值分割,主要对灰度直方图存在两个波峰图像的自动阈值分割。提供两种方法“max_separability”和“smooth_histo”。

最大限度的可分性(max_separability):根据“灰度直方图的阈值选择方法”的灰度直方图自动阈值调用。该算法首先计算图像的直方图,然后利用统计矩找到将像素分割为前景和背景的最优阈值,并最大化这两个类之间的可分性。此方法仅适用于byte和uint2图像。

直方图平滑(smooth_histo):首先确定灰度值的相对直方图。然后从直方图提取相关的最小值,作为阈值操作的参数。为了减少最小值,直方图被平滑处理为一个高斯函数,就像在auto_threshold中一样。在平滑直方图中,掩模尺寸增大,直到得到2个波峰的最小值。然后,阈值设置为这个最小值的位置。

       read_image (Image, 'clip')

       binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)

3. dyn_threshold(OrigImage, ThresholdImage : RegionDynThresh : Offset, LightDark : )

局部阈值分割,当前背景之间差异明显时,可以设定全局阈值进行threshold,但很多情况下由于背景不均一,目标体经常表现为比背景局部亮一些或暗一些,无法确定全局阈值操作,需要通过其邻域找到一个合适的阈值进行分割这时就要用到局部阈值分割了。

4. var_threshold(Image : Region : MaskWidth, MaskHeight, StdDevScale, AbsThreshold, LightDark : )

均值和标准偏差局部阈值分割,能够较好的分开目标和背景,对不适合的参数设置不敏感。MaskWidth、 MaskHeight是用于滤波平滑的掩膜单元;StdDevScale是标准差乘数因子(简称标准差因子);AbsThreshold是设定的绝对阈值;LightDark有4个值可选,’light’、’dark’、’equal’、’not_equal’。

需要强调的是var_threshold算子和dyn_threshold算子极为类似。不同的是var_threshold集成度更高,并且加入了“标准差×标准差因子”这一变量。可以有效地减少噪声对分割的影响。

5. auto_threshold(Image : Regions : Sigma : )

根据直方图确定阈值自动全局阈值分割,运行原理,第一,计算灰度直方图。第二,高斯平滑后从直方图提取最小值。第三,根据提取的最小值进行阈值分割。sigma越大提取区域越少。

       read_image (Image, 'fabrik')

       median_image (Image, Median, 'circle', 3, 'mirrored')

       auto_threshold (Median, Regions, 3)

6. fast_threshold(Image : Region : MinGray, MaxGray, MinSize : )

快速全局阈值分割,灰度值满足MinGray<=g<=MaxGra聚合为一个区域,为了节省时间按两步执行。第一,先处理行列间隔Minsize的所有像素点。第二,处理上一步选择点的领域。和threshold相比分割速度快。

7. watersheds(Image : Basins, Watersheds : : )

分水岭图像分割。可以分割出分水岭和盆地。

       read_image (Br2, 'particle')

       gauss_filter (Br2, ImageGauss, 9)

       invert_image (ImageGauss, ImageInvert)

       watersheds (ImageInvert, Basins, Watersheds)

       dev_set_draw ('margin')

       dev_set_colored (12)

       dev_display (Br2)

       dev_display (Basins)

8. watersheds_threshold(Image : Basins : Threshold : )

使用阈值分水岭图像分割,可以提取分水岭盆地。
 

猜你喜欢

转载自blog.csdn.net/weixin_42398658/article/details/105951102