dev_clear_window ()
dev_close_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)
**************************************************************
*實驗法确定門檻值分割//或者打開灰階直方圖,門檻值調節
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Region,43, 255)
dev_display (Region)
**************************************************************
*根據直方圖谷底确定門檻值
dev_open_window (0, 0, 512, 512, 'black', WindowHandle1)
dev_set_color ('red')
*計算圖像灰階直方圖
gray_histo (GrayImage, GrayImage, AbsoluteHisto, RelativeHisto)
*從直方圖中确定灰階值門檻值
histo_to_thresh (AbsoluteHisto, 2, MinThresh, MaxThresh)
dev_set_colored (12)
*根據上面計算的MinThresh, MaxThresh,進行門檻值分割
threshold (GrayImage, Region1, MinThresh[3],MaxThresh[6])
dev_display (Region1)
***************************************************************
*疊代選擇門檻值法
dev_open_window (0, 0, 512, 512, 'black', WindowHandle2)
binary_threshold (GrayImage, Region2, 'smooth_histo', 'light', UsedThreshold)
dev_display (Region2)
**************************************************************
*最大類間方差法(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)