Halcon 中的全局阈值分割算子 binary_threshold 中,Method = 'max_separability' 指的就是Otsu法(最大类间方差法,有时也称之为大津算法)。
基本的计算步骤如上面讲的那样,然后我们在Haclon中自己来实现代码:
get_image_size (srcImage, Width, Height) * 最大方差初始化为0 otu:=[] MaxVariance := 0.0 * 自动计算的最佳阈值 BsetThreshold := [] * 前景区域的灰度均值 Mean0 := [] * 背景区域的灰度均值 Mean1 := [] * 图像像素点总个数 PixelSum := Width * Height tuple_real (PixelSum, PixelSum) * 前景区域的像素点个数 Area0 := [] * 背景区域的像素点个数 Area1 := [] for ImgThreshold := 1 to 255 by 1 dev_display (srcImage) * 区域分割 threshold (srcImage, Region, ImgThreshold, 255) * 获得前景区域像素个数 area_center (Region, Area0, Row, Column) * 获得前景区域均值和方差 intensity (Region, srcImage, Mean0, Deviation) * 获得背景区域像素个数、均值和方差 difference (srcImage, Region, RegionDifference) area_center (RegionDifference, Area1, Row1, Column1) intensity (RegionDifference, srcImage, Mean1, Deviation1) * 计算类间方差 Otsu := Area0/PixelSum * Area1/PixelSum * pow(Mean0-Mean1,2) * 获取最大类间方差最佳阈值 otu:=[otu,Otsu] if (Otsu > MaxVariance) MaxVariance := Otsu BestThreshold := ImgThreshold endif endfor
我在Halcon中对代码进行了封装,生成了一个.hdvp外部函数 threshold_Otsu(Image : Region : LightDark : UsedThreshold) ,可以直接调用使用,调用方法如下所示:
read_image (Image, 'particle') threshold_Otsu (Image, Region1, 'Light', UsedThreshold1) binary_threshold (Image, Region2, 'max_separability', 'light', UsedThreshold2)
看一下最终的实现效果:
threshold_Otsu.hdvp
转载自:https://blog.csdn.net/IntegralforLove/article/details/100102105
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


