Halcon--阈值分割总结

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)

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值