Analysis of the principle of binary_threshold threshold segmentation algorithm

Halcon's global threshold segmentation algorithm (binary_threshold) is actually the otsu method (maximum inter-class variance method, sometimes called the Otsu algorithm.

The idea of ​​clustering is used, and the gray level of the image is divided into two parts according to the gray level, so that the gray value difference between the two parts is the largest, and the gray difference between each part is the smallest. Calculate to find a suitable gray level to divide. Therefore, the otsu algorithm can be used to automatically select the threshold for binarization during binarization.

See: http://blog.csdn.net/xw20084898/article/details/17564957

The code is implemented as follows:

gray_histo (Region, Image, AbsoluteHisto, RelativeHisto)
 nAveragray: = 0
 for Index := 0 to 255 by 1
     nAveragray: = nAveragray + RelativeHisto [Index] * Index
 endfor
 wk:=0.0
 uk:=0.0
 MaxDiff:=0.0
 Diff:=0.0
 AutoThre: = 0

 for Index := 0 to 255 by 1
     wk:=wk+RelativeHisto[Index]
     uk:=uk+RelativeHisto[Index]*Index
     if(wk<=0.0 or wk>=1.0)
          Diff:=0
     else
          Diff:=(nAveragray*wk-uk)*(nAveragray*wk-uk )/(wk*(1-wk))
     endif
     if(Diff>MaxDiff)
           MaxDiff:=Diff
           AutoThre:=Index
     endif
 endfor

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774615&siteId=291194637