提取图像中的特定区域
提取图像中的特定区域
这段代码是使用HALCON软件编写的,HALCON是一个用于机器视觉和图像处理的高级编程语言。代码的主要功能是处理一组图像,并对每张图像执行一系列图像处理操作
dev_update_window (‘off’)
read_image (Fins, ‘fin’ + [1:3])
get_image_size (Fins, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width[0], Height[0], ‘black’, WindowID)
set_display_font (WindowID, 14, ‘mono’, ‘true’, ‘false’)
for I := 1 to 3 by 1
select_obj (Fins, Fin, I)
dev_display (Fin)
binary_threshold (Fin, Background, ‘max_separability’, ‘light’, UsedThreshold)
dev_set_color (‘blue’)
dev_set_draw (‘margin’)
dev_set_line_width (4)
dev_display (Background)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
closing_circle (Background, ClosedBackground, 250)
dev_set_color (‘green’)
dev_display (ClosedBackground)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
difference (ClosedBackground, Background, RegionDifference)
opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
dev_display (Fin)
dev_set_color (‘red’)
dev_display (FinRegion)
area_center (FinRegion, FinArea, Row, Column)
if (I < 3)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
endif
endfor
fin1.png
fin2.png
fin3.png
以下是程序运行结果:
以下是代码的逐行解释:
dev_update_window (‘off’): 关闭窗口的自动更新,以避免在处理过程中显示不完整的图像。
read_image (Fins, ‘fin’ + [1:3]): 读取名为’fin1’, ‘fin2’, 'fin3’的三张图像,并将它们存储在Fins数组中。
get_image_size (Fins, Width, Height): 获取Fins数组中图像的宽度和高度,并将结果存储在Width和Height数组中。
dev_close_window (): 关闭当前打开的所有窗口。
dev_open_window (, , Width[], Height[], ‘black’, WindowID): 打开一个新的窗口,窗口的位置在屏幕左上角,大小与第一张图像相同,背景颜色为黑色,并将窗口ID存储在WindowID变量中。
set_display_font (WindowID, 14, ‘mono’, ‘true’, ‘false’): 设置窗口中显示文本的字体大小、类型和样式。
for I := 1 to 3 by 1: 开始一个循环,从1到3,每次增加1。
select_obj (Fins, Fin, I): 从Fins数组中选择第I张图像,存储在Fin变量中。
dev_display (Fin): 显示当前选中的图像。
binary_threshold (Fin, Background, ‘max_separability’, ‘light’, UsedThreshold): 对Fin图像应用最大可分性二值化阈值,得到背景图像Background,并记录使用的阈值。
dev_set_color (‘blue’): 设置绘图颜色为蓝色。
dev_set_draw (‘margin’): 设置绘图模式为边缘绘制。
dev_set_line_width (4): 设置线条宽度为4。
dev_display (Background): 显示背景图像。
disp_continue_message (WindowID, ‘black’, ‘true’): 在窗口中显示继续消息,文本颜色为黑色,消息持续显示。
stop (): 等待用户操作,直到按下任意键。
closing_circle (Background, ClosedBackground, 250): 对Background图像应用圆形闭运算,半径为250,得到ClosedBackground图像。
dev_set_color (‘green’): 设置绘图颜色为绿色。
dev_display (ClosedBackground): 显示经过闭运算后的图像。
重复步骤15和16。
difference (ClosedBackground, Background, RegionDifference): 计算ClosedBackground和Background的差异,得到RegionDifference图像。
opening_rectangle1 (RegionDifference, FinRegion, 5, 5): 对RegionDifference图像应用矩形开运算,尺寸为5x5,得到FinRegion图像。
dev_display (Fin): 重新显示原始图像。
dev_set_color (‘red’): 设置绘图颜色为红色。
dev_display (FinRegion): 显示开运算后的区域。
area_center (FinRegion, FinArea, Row, Column): 计算FinRegion图像的面积中心,结果存储在FinArea, Row, Column变量中。
if (I < 3): 如果当前处理的图像不是最后一张,则执行以下操作。
重复步骤15和16。
endif: 结束条件判断。
endfor: 结束循环。
这段代码主要用于图像的预处理和特征提取,通过二值化、闭运算和开运算等操作,提取图像中的特定区域。
更多推荐
所有评论(0)