文章转自微信公众号:机器视觉那些事
**********************************************************
*********公众号:机器视觉那些事儿**********
* 1. 算法功能:Blob分析--粘连颗粒检测
* 2. 算法思路:
* (1)简单的阈值分割;
* (2)计算连通域connection;
* (3)基于距离变换的分水岭区域分割,使用算子distance_tansform,watersheds
* (4)盆地与原连通域求交集,分离粘连颗粒;
**********************************************************
*采集图像
dev_close_window ()
read_image (Image, 'pellets')
*计算图片大小,并以原图尺寸显示
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
stop ()
*01 简单的阈值分割
threshold (Image, Region, 105, 255)
*02 计算连通域
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 99999)
dev_set_draw ('margin')
dev_display (Image)
dev_display (SelectedRegions)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*03 基于距离变换的分水岭区域分割,使用算子distance_tansform,watersheds
*距离变换
distance_transform (SelectedRegions, DistanceImage, 'octagonal', 'true', 380, 350)
*转换图像类型,将real类型转换为byte,因为分水岭迭代运算的图像为‘byte’类型
convert_image_type (DistanceImage, DistanceImageByte, 'byte')
*反转图像,使得高灰度值在边缘代表山脉,低灰度值在里面代表盆地
invert_image (DistanceImageByte, DistanceImageInv)
* 扩大图像灰度范围【0,255】,增加对比度
scale_image_max (DistanceImageInv, DistanceImageInvScaled)
watersheds_threshold (DistanceImageInv, Basins, 5)
dev_display (DistanceImageInvScaled)
dev_display (Basins)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 原图中显示分水岭
dev_display (Image)
dev_display (SelectedRegions)
dev_set_color ('blue')
dev_display (Basins)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*04 盆地与原连通域求交集,分离粘连颗粒
intersection (Basins, SelectedRegions, SegmentedPellets)
*结果显示
dev_display (Image)
dev_set_colored (12)
dev_display (SegmentedPellets)