天天看點

Halcon--門檻值分割之(最大類間方差法(OSTU))

*最大類間方差法(OSTU)

dev_close_window ()
dev_clear_window ()

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-18.11-Steady/examples/images/color/citrus_fruits_15.png')
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)

get_image_size (Image, Width, Height)
rgb1_to_gray (Image, GrayImage)

*最大方差初始化為0
MaxVariance:=0.0
*最佳分割灰階門檻值從1周遊到255,初始門檻值的選取可以取圖像平均灰階值
for TH := 1 to 255 by 1
    
    dev_display (GrayImage)
    *區域分割
    threshold (GrayImage, Region3, TH, 255)
    
    *獲得前景區域像素個數
    area_center (Region3, Area, Row, Column)
    *獲得前景區域均值和方差
    intensity (Region3, GrayImage, Mean, Deviation)
    
     *獲得背景區域像素個數,均值和方差
     complement (Region3, RegionComplement)
     area_center (RegionComplement, Area1, Row1, Column1)
     intensity (RegionComplement, GrayImage, Mean1, Deviation1)
     
     
     *計算類間方差
     Ostu:=Area*1.0/[Width*Height]*Area1*1.0/[Width*Height]*pow(Mean-Mean1,2)
     
     *獲得最大類間方差的最佳門檻值
     if(Ostu>MaxVariance)
         MaxVariance:=Ostu
         BestThreshold:=TH     
     endif
endfor

*利用得到門檻值分割
threshold (GrayImage, Region4, BestThreshold, 255)
dev_display (Region4)
           

繼續閱讀