目的:将感兴趣图像从背景中分离出来。
- 图像分割标准:像素的灰度、边界、几何形状、颜色、纹理
阈值处理
- 全局阈值:threshold()
- 自适应阈值:auto_threshold() 以灰度直方图中出现谷底为分割点,对灰度直方图的波峰进行分割
- 自动全局阈值分割法:binary_threshold() 可选最大类间方差法或平滑直方图法
- 局部阈值分割法:dyn_threshold() 适用于无法用单一灰度进行分割的情况
- var_threshold() 算子和char_threshold()算子和dual_threshold()
全局阈值处理 threshold算子
read_image(Image,'i7z')
rgb1_to_gray(Image, GrayImage)
*选择ROI
gen_rectangle1(ROI_0,60, 50, 350, 350)
*从原图中分割出ROI
reduce_domain(GrayImage,ROI_0, ImageReduced)
get_image_size(ImageReduced, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
threshold(ImageReduced, Region, 0, 180)
dev_display(Region)
测试图
阈值提取文字部分
auto_threshold算子
read_image(Image,'i7zroi')
rgb1_to_gray(Image, GrayImage)
auto_threshold(GrayImage, Region, 8.0)
get_image_size(GrayImage, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Region)
binary_thresholds算子 适合亮背景中提取暗字符
read_image(Image,'i7zroi')
rgb1_to_gray(Image, GrayImage)
binary_threshold(GrayImage,Region1, 'max_separability', 'dark', UsedThreshold)
get_image_size(GrayImage, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Region1)
dyn_threshold算子
read_image(Image,'shuxie')
rgb1_to_gray(Image, GrayImage)
*若图像对比度比较低,可以对图像进行相乘,增强对比度
mult_image(GrayImage, GrayImage, ImageResult, 0.005, 0)
*使用平滑滤波器对原始图像进行适当平滑
mean_image(ImageResult, ImageMean, 7, 7)
*动态阈值分割 提取字符区域
dyn_threshold(ImageResult, ImageMean, RegionDynThresh, 7, 'not_equal')
*开运算,去除无意义小点
opening_circle(RegionDynThresh, RegionOpening, 1.3)
dev_clear_window()
dev_display(RegionOpening)
上面自动阈值提取到右上角的暗部背景
另一个例子 背景中有亮的和前景差不多
read_image(Image,'yumao')
rgb1_to_gray(Image, GrayImage)
*使用平滑滤波器对原始图像进行适当平滑
mean_image(GrayImage, ImageMean, 30, 30)
*动态阈值分割 提取字符区域
dyn_threshold(GrayImage, ImageMean, RegionDynThresh, 20, 'not_equal')
*开运算,去除无意义小点
erosion_circle(RegionDynThresh, RegionErosion, 0.5)
dev_clear_window()
dev_display(RegionErosion)
var_threshold 灰度变化符合阈值的被提取出来
read_image(Image,'va')
rgb1_to_gray(Image, GrayImage)
var_threshold(GrayImage, Region, 15, 15, 0.2, 2, 'dark')
dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
dev_display (Region)
char_threshold算子,适合在亮背景中提取暗字符
read_image(Image,'jianpan')
rgb1_to_gray(Image, GrayImage)
char_threshold(GrayImage,GrayImage , Characters, 5, 95, Threshold)
get_image_size(GrayImage, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Characters)
区域生长法
在图像中选定种子像素或种子区域,然后从种子邻域像素搜索,将灰度和颜色相近的像素归入种子区域中
- regiongrouwing算子:将灰度相近的像素合并
- regiongrowing_mean():输入为灰度均值图像
- 分水岭算法:watershed() 基于边缘的图像分割算法,通过寻找区域之间分界线,对图像进行分割。灌水,形成分水岭,适用于缺陷识别,如有一个小黑点。
regiongrouwing算子
read_image(Image,'xuexiao')
mean_image(Image, ImageMean, 5, 5)
*找出颜色相似的区域
regiongrowing(ImageMean, Regions, 1, 1, 3.0, 100)
closing_circle(Regions, RegionClosing, 3.5)
get_image_size(ImageMean, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(RegionClosing)
分水岭算法
较好用于复杂背景下的目标分割,特别是具有蜂窝状结构的画面内容
read_image(Image,'muwen2')
rgb1_to_gray(Image, GrayImage)
gauss_filter(GrayImage, ImageGauss, 11)
watersheds(ImageGauss, Basins, Watersheds)
watersheds_threshold(ImageGauss, Basins1, 60)
- 开运算:先腐蚀后膨胀