【issue-halcon例程学习】particle.hdev

例程功能

检测液体中的颗粒物。难度在于图像中大亮斑区域、低对比度小物体和噪声影响。

代码如下

csharp 复制代码
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')
dev_display (Image)
dev_disp_text ('Original image', 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
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')
dev_disp_text ('Exclude large areas from processing', 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
* Continue to calculate small regions
* Return the complement of a region
complement (LargeDilation, NotLarge)
reduce_domain (Image, NotLarge, ParticlesRed)
mean_image (ParticlesRed, Mean, 31, 31)
* 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)
dev_disp_text ('Extracted small particles', 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
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
* Define limits for the displayed message at the end of the while-loop.
MaxRow := 450
MaxColumn := 440
MinRow := 40
MinColumn := 100
while (Button == 1)
    dev_disp_text (['Select object with left mouse button', 'Right button to quit'], 'window', 12, 12, 'black', 'box_color', '#fce9d4dd')
    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)
    dev_display (SmallSingle)
    count_obj (SmallSingle, NumSingle)
    if (NumSingle == 1)
        intensity (SmallSingle, Image, MeanGray, DeviationGray)
        area_center (SmallSingle, Area, Row, Column)
        * Limit the message so that it is displayed entirely inside the graphics window.
        if (Row > MaxRow)
            Row := MaxRow
        endif
        if (Column > MaxColumn)
            Column := MaxColumn
        endif
        if (Row < MinRow)
            Row := MinRow
        endif
        if (Column < MinColumn)
            Column := MinColumn
        endif
        dev_disp_text (['Area = ' + Area,'Intensity = ' + MeanGray$'.3'], 'image', Row + 10, Column - 90, 'black', 'box_color', '#fce9d4dd')
    endif
endwhile
dev_set_line_width (1)
dev_update_on ()

要点

  1. 代码中采用两种方法---thresholddyn_threshold,以分别分割不同的物体。再使用额外的后处理稳定地提取小颗粒物。
csharp 复制代码
	threshold (Image, Large, 110, 255)	# 全局阈值分割
	dilation_circle (Large, LargeDilation, 7.5)	# 以结构圆的方式做膨胀运算
	complement (LargeDilation, NotLarge)	# 返回一个区域的补集
	reduce_domain (Image, NotLarge, ParticlesRed)
	
	mean_image (ParticlesRed, Mean, 31, 31)	# 均值滤波
	dyn_threshold (ParticlesRed, Mean, SmallRaw, 3, 'light')	# 局部阈值分割
	opening_circle (SmallRaw, Small, 2.5)	# 开运算
	connection (Small, SmallConnection)	# 将不相连的区域打散

膨胀运算(dilation):kernel区域中的最大值赋给中心位置,所以kernel_size一般为奇数。这样操作的结果会将图像外围的突出点连接并向外延伸;

腐蚀运算(erosion):kernel区域中的最小值赋给中心位置。这样操作的结果是会将图像外围的突出点加以腐蚀;

开运算(opening): 先腐蚀后膨胀,用来消除小物体、在纤细点处分离物体、平滑较大物体的边界的同时并不明显改变其面积,消除物体表面的突起;

闭运算(closing): 闭操作就是对图像先膨胀,再腐蚀。闭操作的结果一般是可以将许多靠近的图块相连称为一个无突起的连通域。原图首先经过膨胀操作将两个分开的图块结合起来,接着通过腐蚀操作将连通域的边缘和突起进行削平,最后得到的是一个无突起的连通域;

  1. 通过鼠标互动显示颗粒物的面积和灰度信息(再熟悉一遍)
csharp 复制代码
	while (Button == 1) # 右击结束循环
		get_mbutton (WindowID, Row, Column, Button)	# 获取在WindowID中鼠标的状态
		select_region_point (SmallConnection, SmallSingle, Row, Column)	# 选取区域中所有包含点(Row, Column)的region
		......	# do sth.
	endwhile
相关推荐
Charles Ray27 分钟前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
我要吐泡泡了哦1 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
骑鱼过海的猫1231 小时前
【tomcat】tomcat学习笔记
笔记·学习·tomcat
贾saisai3 小时前
Xilinx系FPGA学习笔记(九)DDR3学习
笔记·学习·fpga开发
北岛寒沫4 小时前
JavaScript(JS)学习笔记 1(简单介绍 注释和输入输出语句 变量 数据类型 运算符 流程控制 数组)
javascript·笔记·学习
铁匠匠匠5 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
架构文摘JGWZ7 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
小齿轮lsl7 小时前
PFC理论基础与Matlab仿真模型学习笔记(1)--PFC电路概述
笔记·学习·matlab
Aic山鱼7 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
qq11561487077 小时前
Java学习第八天
学习