阈值处理
图像分割是图像处理与计算机视觉领域低层次视觉中最为基础和重要的领域之一,是进行视觉分析和模式识别的基本前提。而阈值法是一种传统的图像分割方法,因其实现简单、计算量小、性能较稳定而成为图像分割中最基本和应用最广泛的分割技术。数字化图像中,对图像二值化是个基本操作,简单来说就是一个指定像素灰度值的范围,假设一幅图像灰度值在0-255范围分布,阈值处理就是将图像不同位置处的灰度值与该阈值进行比较,在该范围的灰度值置为1,称为前景,不在该范围的置为0,称为背景。
1.全局阈值化
最简单的阈值化就是全局阈值化,就是设置一个值g,图像灰度大于该值时置为1,小于该值时置为0,或者反过来也可以。
下面就举一个简单样例:
read_image (Audi2, 'audi2')
fill_interlace (Audi2, ImageFilled, 'odd')
threshold (ImageFilled, Region, 0, 90)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'width', 'and', 30, 70)
select_shape (SelectedRegions, Letters, 'height', 'and', 60, 110)
dev_clear_window ()
dev_set_colored (12)
dev_display (ImageFilled)
dev_display (Letters)
![](https://i-blog.csdnimg.cn/blog_migrate/55626fc86251ba35a9d8831d21fa0778.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3f398d499bf8a8144f78b05f7136e8f4.png)
2.自动阈值化
当图像灰度值不是那么一致分明,灰度有一定分布性,自动根据绝对灰度直方图中的灰度分布生成区域,其实是根据平滑的 σ \sigma σ的图像和灰度值,选取每个山峰为独立区域,分割区域是以谷底(最小值)为准!用一个固定灰度值不能完全分割时,可以考虑这种方法—阈值化。
例子:
dev_close_window ()
read_image (Aegypt1, 'egypt1')
get_image_size (Aegypt1, Width, Height)
Sigma := 4
auto_threshold (Aegypt1, Regions, Sigma)
![](https://i-blog.csdnimg.cn/blog_migrate/9787620b7b3eb54fd20a4184a206a113.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e8582be84e85365159fb0822aece7441.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3f424d640f826aa05ded20be42e25f43.png)
3.动态阈值化
用相机拍摄图像时,由于外部光线受外界光线的干扰一般比较大,所成的图像往往灰度分布不均匀,在图像处理时,如在阈值分割时采用固定阈值,那么在环境改变时分割效果受影响极大,那么为了避免此影响就必须采用动态阈值,自动求出合适的阈值进行分割。
* 准备
read_image (Image, 'pic01')
get_image_size (Image, Width, Height)
* 预处理均值模糊图像
mean_image (Image, ImageMean, 59, 59)
*动态阈值提取区域
dyn_threshold (Image, ImageMean, RegionDynThresh, 15, 'not_equal')
* 修补提取区域
closing_circle (RegionDynThresh, RegionClosing, 8.5)
opening_circle (RegionClosing, RegionOpening, 6.5)
connection (RegionOpening, ConnectedRegions)
smallest_circle (ConnectedRegions, Row, Column, Radius)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
![](https://i-blog.csdnimg.cn/blog_migrate/8b2ee5dd201ff7d68dd1897393ea576a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/087e3e3efd7e8ba9e03f414f2b3558da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/009c7c48077621c118d1b6959442ffe7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d2b3bfb0ad91b7251c56e7b403eb5ff6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f06974f324349f89d0b6435a2ee0cd54.png)
待续…
区域生长
分水岭