对应示例程序:
particle.hdev
目标:测量图像中的小斑点或者小颗粒
思路为:
1.读取图像,窗口初始化
2.阈值分割,先提取出图像中的大斑点区域,因为大斑点区域会影响到小区域的提取
3.利用算子complement取步骤2的补集,得到除大斑点以外,包含小斑点的区域
4.Blob分析,阈值分割,连通域分割等,得到小斑点
5.根据读取鼠标的位置,可视化的形式显示选中的小斑点的面积
图像:
原图
结果图
代码:
//读取图像 窗口初始化
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
read_image (Image, 'particle')
dump_window (WindowID, 'jpg', 'C:/Users/Bryce/Desktop/新建位图图像')
dev_display (Image)
disp_message (WindowID, 'Original image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
//阈值分割 先把图像中的大斑点区域 提取出来
threshold (Image, Large, 110, 255)
* Dilate regions with a circular structuring element
dilation_circle (Large, LargeDilation, 7.5) //圆形膨胀
dev_display (Image)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
dev_display (LargeDilation)
dev_set_draw ('fill')
disp_message (WindowID, 'Exclude large areas from processing', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* Continue to calculate small regions
* Return the complement of a region
//取大斑点以外的区域
complement (LargeDilation, NotLarge) //返回一个区域的补码 就是把ROI去掉 后剩下的图像区域
reduce_domain (Image, NotLarge, ParticlesRed)
mean_image (ParticlesRed, Mean, 31, 31)
//Blob分析 阈值分割 连通域分割 提取出小区域
* Segment the image using a local threshold
dyn_threshold (ParticlesRed, Mean, SmallRaw, 3, 'light')
opening_circle (SmallRaw, Small, 2.5)
connection (Small, SmallConnection)
dev_display (Image)
dev_set_colored (12)
dev_display (SmallConnection)
disp_message (WindowID, 'Extracted small particles', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
//互动环节 点一个小区域 输出面积和相关信息
* Continue to select several regions and to get information
dev_set_color ('green')
dev_display (Image)
dev_set_draw ('margin')
dev_display (SmallConnection)
Button := 1
while (Button == 1)
disp_message (WindowID, ['Select object with left mouse button','Right button to quit'], 'image', 5, 5, 'black', 'true')
dev_set_color ('green')
get_mbutton (WindowID, Row, Column, Button)
dev_display (Image)
dev_display (SmallConnection)
dev_set_color ('red')
select_region_point (SmallConnection, SmallSingle, Row, Column) //选择包含给定像素的所有区域 就行根据Row Col 确定选中了哪个区域
dev_display (SmallSingle)
count_obj (SmallSingle, NumSingle)
if (NumSingle == 1)
intensity (SmallSingle, Image, MeanGray, DeviationGray) //计算灰度值的平均值和偏差
area_center (SmallSingle, Area, Row, Column)
disp_message (WindowID, ['Area = ' + Area,'Intensity = ' + MeanGray$'.3'], 'image', Row + 10, Column - 90, 'black', 'true')
endif
endwhile
dev_set_line_width (1)
dev_update_on ()
用到的几个算子:
complement --返回一个区域的补码 就是把ROI去掉 后剩下的图像区域
select_region_point–选择包含给定像素的所有区域 就行根据Row Col 确定选中了哪个区域
intensity --计算灰度值的平均值和偏差