Halcon算子 binary_threshold、auto_threshold、dyn_threshold
算子 | 适用场景 | 特点 |
threshold | 全局对比度高或均匀光照图像 | 固定阈值、速度快 |
binary_threshold | 自动计算阈值(基于直方图或Otsu) | 自动化、但无法处理光照不均匀 |
dyn_threshold | 光照不均匀、局部对比度变化 | 动态调整阈值,灵活性强 |
自动阈值分割 auto_threshold
根据图像的灰度直方图自动确定阈值
使用自动阈值,可以适当滤除一定的边缘干扰
auto_threshold (Image, Regions, Sigma)
- Image: 输入单通道图像
- Regions: 输出的区域,每个区域对应一个灰度值区间
- Sigma: 用于直方图高斯平滑的标准差 默认值:2 Sigma值越大,提取的区域就越少
read_image (Image1, 'F:/Halcon/Image/5-2.png
auto_threshold (Image1, Regions1, 5)
dev_display (Regions1)
动态阈值分割 dyn_threshold
使用动态阈值可以有效的对应光照不均匀或背景变化较大(对比度变化)的图像分割问题
dyn_threshold (Image, ImageMean, RegionDynThresh, Offset, LightDark)
- OrigImage: 原始图像
- ThresholdImage: 参考图像,通过对原始图像进行平滑滤波等预处理生成
- RegionDynThresh: 输出的分割区域
- Offset: 阈值偏移量;值越大检测越严格;值越小,检测越宽松
- LightDrak: 提取的目标类型,'light'(更亮区域);'dark'(更暗区域);'equal'(与参考图像相似区域);'not_equal'(差异较大区域)
read_image (Image2, 'photometric_stereo/embossed_01.png')
get_image_size (Image2, Width1, Height1)
dev_open_window_fit_size (0, 0, Width1, Height1, -1, -1, WindowHandle1)
mean_image (Image2, ImageMean, 59, 59)
dyn_threshold (Image2, ImageMean, RegionDynThresh, 10, 'not_equal')
直方图去顶阈值 histo_to_thresh
通过平滑直方图提取直方图中的最小值来确定阈值
大白话: 从灰度直方图确定阈值
histo_to_thresh (Histogram, Sigma, MinThresh, MaxThresh)
- Histogram: 灰度值直方图,可以是绝对直方图或相对直方图
- Sigma: 用于高斯平滑的Sigma值。默认是2.0 会影响阈值的提取效果,值越大提取区域越少
- MinThresh: 最小阈值
- MaxThresh: 最大阈值
dev_close_window ()
dev_update_off ()
read_image (Image, 'F:/Halcon/Image/5-1.png')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Width, -1, -1, WindowHandle)
dev_set_color ('red')
gray_histo (Image, Image, AbsoluteHisto, RelativeHisto)
histo_to_thresh (AbsoluteHisto,5, MinThresh, MaxThresh)
dev_set_colored (12)
threshold (Image, Regions,MinThresh[0],MaxThresh[0])
dev_display (Regions)
二值化阈值 binary_threshold
binary_threshold(Image, Region, MinGray, MaxGray, Method)
- Image: 输入图像,可以是单或多通道
- Region: 输出的二值图像区域
- Method: 分割方法
- ‘max_separability’最大限度的可分性
- ‘smooth_histo’ 直方图平滑
- max比smooth 速度要快
- LightDark: ‘dark’(暗区域);‘light’(亮区域)
- UsedThreshold: 自动阈值使用的阈值
read_image(Image, 'fabrik')
rgb1_to_gray(Image, GrayImage)
binary_threshold (GrayImage, Region, 'max_separability', 'dark', UsedThreshold)