二值化化阈值
- threshold 适用于简单的全局阈值分割
- binary_threshold 适用于自动选择阈值的场景
- dyn_threshold 和 var_threshold 适用于光照变换较大的场景
- local_threshold 适用于光照不均匀的图像
- dual_threshold 适用于需要考虑区域面积和灰度值的场景
binary 和 auto 区别:
他们两者都属于自动阈值
- binary可根据图像的灰度,分割出暗或者亮区域
- auto 是灰度直方图确定阈值,适用于复杂的背景和目标;可以适当滤除边缘干扰,适用于复杂背景下的目标检测
对比 | 适用场景 | 实际应用 |
local_threshold | 适用于光照不均匀或复杂背景图像,能够分割目标区域;适用于文档图像处理 | 主要用于文档图像处理和复杂背景下的目标检测 |
var_threshold | 适用于低对比度、纹理均匀的图像,不依赖全局光照的场景,适用于检测光滑表面上的划痕,赃物等对比度目标 | 常用于金属划痕等需要考虑局部对比度的场景 |
1、threshold 固定阈值
功能:简单的阈值分割算子,根据设定的固定阈值将图像转换为二值图像
threshold(Image,BinaryRegion,Min,Max)
- Image:输入的图像
- BinaryRegion:输出的区域
- Min Max:输入的最小阈值、最大阈值
示例代码
read_image (Image, 'clip')
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
Min:=0
Max:=120
threshold (Image, Region, Min, Max)
dev_display (Region)
为下文与fast做比较的代码
dev_update_off ()
read_image (Image, 'clip')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_set_part (0, 0, Height+199, Width-1)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)MinHeight:=20
Min:=0
Max:=128
threshold (Image, Region, Min, Max)
count_seconds (Start)
for i:=1 to 100 by 1threshold (Image, Region, Min, Max)
endfor
count_seconds (End)
TimeThreshold:=(End-Start)/100*1000
dev_set_color ('yellow')
dev_display (Region)
disp_message (WindowHandle, 'TimeThreshold:'+TimeThreshold$'.2'+'ms', 'image', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop()
2、fast_threshold 快速全局阈值
该算子是 threshold 的增强版本,在多核计算机上处理速度快
但是由于多核计算机支持SSE2指令集的,所以在多核计算机上threshold的速度大于fast_threshold的,
注意: fast_threshold主要用于嵌入式系统中,速度才会大于threshold
fast_threshold(Image,Region,MinGray,MaxGray,MinSize)
- Image:输入的图像
- Region:分割后的区域
- MinGray:灰度值的下界
- MaxGray:灰度值的上界
- MinSize:提取对象的最小尺寸
fast_threshold (Image, Region1, Min, Max, MinHeight)
count_seconds (Seconds)
for i:=1 to 100 by 1fast_threshold (Image, Region1, Min, Max, MinHeight)
endfor
count_seconds (Seconds1)
TimeFastThreshold := (Seconds1 - Seconds) / 100 * 1000
dev_display (Image)
dev_set_color ('green')
dev_display (Region1)
disp_message (WindowHandle, 'TimeFastThreshold:'+TimeFastThreshold$'.2'+'ms', 'image', 12, 12, 'black', 'true')
从下面的我们可以看出来,相比匹配速度比较快
3、 dyn_threshold 动态阈值
功能: 动态阈值算子,通过将图像分多为个区域,并基于每个区域的局部特征动态计算阈值,适用于光照变换较大的场景
注意:需要将原图进行滤波平滑处理
dyn_threshold(OrigImage,ThresholdImage,RegionDynThresh,Offest,LightDark)
- OrigImage:原始图像
- ThresholdImage:参考图像,通过对原始图像进行平滑滤波等预处理生成
- RegionDynThresh:输出的分割区域
- Offset: 阈值偏移量,值越大检测越严格;值越小,检测越宽松
- LightDark:提取的目标类型: 'light'(更亮);'dark'(更暗区域);'equal'(与参考图像相似区域);'not_equal'(差异较大区域)
read_image (Image, 'photometric_stereo/embossed_01.png')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Width, -1, -1, WindowHandle)
mean_image (Image, ImageMean, 59, 59)
dyn_threshold (Image, ImageMean, RegionDynThresh, 10, 'not_equal')
4、binary_threshold 二进制阈值
可以根据图像的灰度分布自动选择阈值,它可以自动选出暗区域或亮区域
binary_threshold(Image,Region,Method,LightDark,UsedThreshold)
- Image:输入图像
- Region:输出的二值图像区域
- Method:分割方法
- 'max_separability' 最大限度的可分性
- 'smooth_histo' 直方图平滑
- max比smooth速度要快
- 'LightDark':'dark'(暗区域) 'light'(亮区域)
- UsedThreshold:自动阈值使用
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
ImageFiles := ['dip_switch/dip_switch_01','circular_barcode','fin1']
for I := 0 to |ImageFiles| - 1 by 1read_image (Image, ImageFiles[I])dev_resize_window_fit_image (Image, 0, 0, -1, -1)dev_display (Image)Message := 'Test image for binary_threshold'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()binary_threshold (Image, RegionMaxSeparabilityLight, 'max_separability', 'light', UsedThreshold)dev_display (Image)dev_display (RegionMaxSeparabilityLight)Message := 'Bright background segmented globally with'Message[1] := 'Method = \'max_separability\''Message[2] := 'Used threshold: ' + UsedThresholddisp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()binary_threshold (Image, RegionMaxSeparabilityDark, 'max_separability', 'dark', UsedThreshold)dev_display (Image)dev_display (RegionMaxSeparabilityDark)
5、char_threshold 动态全阈值
char_threshold 是专门设计来提取字符的一类算子,实际也是一种动态全局阈值算子。
要求:图像必须是亮背景暗字符。
char_threshold(Image,HistoRegion,Characters,Sigma,Percent,Threshold)
- Image: 输入的图像
- HistoRegion:要计算灰度直方图的区域
- Sigma:高斯平滑系数
- Percent:用于计算灰度值的百分比
- Threshold:计算得到的阈值
dev_close_window ()
read_image (Image, 'alpha1')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
dev_set_color ('white')
dev_clear_window ()
char_threshold (Image, Image, Characters, 6, 95, Threshold)
dev_display (Characters)
gray_histo (Image, Image, AbsoluteHisto, RelativeHisto)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_display (Image)
dev_set_color ('white')
gen_region_histo (Region, AbsoluteHisto, 255, 255, 1)
6、auto_threshold 自动阈值分割
根据图像的灰度直方图自动确定阈值,将图像分割为多个区域,通过平滑直方图来提取直方图中的最小值;例如:复杂的背景和目标
可以适当滤除边缘干扰,适用于复杂背景下的目标检测
auto_threshold(Image,Region,Sigma)
- Image:输入的图像
- Region:输出的区域
- Sigma:用于直方图高斯平滑的标准差,Sigma值越大,提取的区域就越少
dev_close_window ()
read_image (Aegypt1, 'egypt1')
get_image_size (Aegypt1, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
dev_set_colored (6)
dev_clear_window ()
Sigma := 6 //设置值
auto_threshold (Aegypt1, Regions, Sigma)* 灰度直方图
gray_histo (Aegypt1, Aegypt1, AbsoluteHisto, RelativeHisto)
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_clear_window ()
* 创建函数子数组
create_funct_1d_array (AbsoluteHisto, Function)
* 高斯平滑函数
smooth_funct_1d_gauss (Function, Sigma, SmoothedFunction)
dev_set_color ('red')
* 函数到对
funct_1d_to_pairs (SmoothedFunction, XValues, YValues)
gen_region_histo (Histo1, YValues, 255, 255, 1)
dev_display (Aegypt1)
dev_set_color ('white')
gen_region_histo (Histo2, RelativeHisto, 255, 255, 1)
7、var_threshold 局部阈值
适用于复杂的图像分割场景,通过局部标准差和均值动态确定阈值;
这个算子可以有效地分开目标和背景,并且对不合适的参数设置不敏感
var_threshold算子和dyn_threshold算子极为类似。
不同的是var_threshold集成度更高
var_threshold(Image,Region,MaskWidth,MaskHeight,StdDevScale,AbsThreshold,LightDark)
- Image:输入的图像
- Region:输出的区域
- MaskWidth 、MaskHeight:用于滤波平滑的掩模单元
- StdDevScale:标准差乘数因子
- AbsThreshold:设定的绝对阈值
- LightDark:四个值:'light'、'dark'、'equal'、'not_equal'
dev_close_window ()
dev_update_off ()
list_image_files ('label', 'default', [], ImageFiles)
ImageFiles1 := regexp_select(ImageFiles,'label_...png$')
read_image (Image, ImageFiles1[0])
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
for Index := 0 to |ImageFiles1| - 1 by 1read_image (Image, ImageFiles1[Index])var_threshold (Image, Region, 15, 15, 1.01, 40, 'dark')connection (Region, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, ['height','area'], 'and', [20,100], [100,400])dev_display (Image)dev_display (SelectedRegions)disp_continue_message (WindowHandle, 'black', 'true')stop ()
endfor
disp_end_of_program_message (WindowHandle, 'black', 'true')
var_threshold 是将原图和对应像素掩模覆盖的像素的平均灰度值对比
8、dual_threshold 双阈值
通过计算图像局部区域的灰度标准差和均值来动态确定阈值。集成了更多的参数,适用于复杂的图像分割场景。
注意: 他只能分割出灰度值高的高区域,不能分割出灰度值低的暗区域
dual_threshold(Image,RegionCrossings,MinSize,MinGray,Threshold)
- Image:原始图像
- RegionCrossings:输出分割后区域
- MinSize:最小面积控制参数
- MinGray:最小灰度值控制参数
- Threshold:阈值控制参数
dev_close_window ()
read_image (Traffic1, 'traffic1')
read_image (Traffic2, 'traffic2')
convert_image_type (Traffic1, ImageConverted1, 'int2')
convert_image_type (Traffic2, ImageConverted2, 'int2')
sub_image (ImageConverted1, ImageConverted2, ImageSub, 1, 0)
dual_threshold (ImageSub, RegionDiff, 500, 20, 10)
9、local_threshold 局部阈值
通过计算图像局部区域的均值和标准差来动态确定阈值,实现对图像的分割
特别适用于文档图像处理,该算子基于Sauvola算法,在非均质的背景下实现文本的二值化
local_threshold(Image,Region,Method,LightDark,GenParamName,GenParamValue)
- Image:输入的图像
- Region:输入的分割区域
- Method:分割方法,目前只支持 'adapted_std_deviation'
- LightDark:控制分割亮结构还是暗结构,可选值为 'dark'(分割暗结构)或 'light'(分割亮结构)
- GenParamName、GenParamValue:用于设置方法特定的参数
- 'mask_size':窗口大小,必须大于字符或结构的笔画宽度
- 'scale':控制阈值与局部均值差异的参数,值越小,分割出的结构对比度越低
- 'range':假设的标准差最大值,对于字节图像,通常设置为128
dev_update_off ()
dev_close_window ()
read_image (Image, 'letters')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
get_image_size (Image, Width, Height)
*创建一阶多项式弯曲灰度表面
gen_image_surface_first_order (ImageSurface, 'byte', 0.5, 0.5, 0.5, Width/2, Height/2, Width, Height)
add_image (Image, ImageSurface, ImageResult, 0.5, 0)
GlobalThreshold:=128
threshold (ImageResult, Region, 0, GlobalThreshold)
dev_display (ImageResult)
dev_display (Region)
Message:='In images with inhomogeneous background'
Message[1]:='it is impossible to segment the characters'
Message[2]:='with a global threshold'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()local_threshold (ImageResult, Region, 'adapted_std_deviation', 'dark', ['mask_size','scale','range'], [30,0.2,128])
dev_display (ImageResult)
dev_display (Region)
下面是一副光照不均匀的图片 阈值分割后