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

例程功能

检查塑料零件的外部边界,是否存在毛刺。

代码如下

csharp 复制代码
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

要点

  • 阈值分割
    全局固定阈值分割threshold ------使用全局固定阈值分割图像,是最简单、最快且使用频率最高的方法。该方法将[MinGray, MaxGray]间的像素点以区域region的形式返回。若MinGrayMaxGray为元组,则每个间隔返回一个region。使用threshold需要环境稳定,阈值可以一次确定;如果照明和物体表面变化,考虑通过对比度标准化或者每幅图分别确定一个阈值。
     对于照明条件变化的情况,可以用直方图定阈值:
csharp 复制代码
	gray_histo (Image, Image, AbsoluteHisto, RelativeHisto)
	PeakGray := sort_index(AbsoluteHisto)[255]
	threshold (Image, Region, 0, PeakGray - 25)

自动全局阈值分割binary_Threshold ------分割阈值是该算子的返回参数,由Method (最大限度可分性max_separability和直方图平滑smooth_histo)自动计算而来。

最大限度可分性max_separability:首先计算图像的直方图;然后利用统计矩找到将像素分割为前景和背景的最优阈值,并最大化这两个类之间的可分性。此方法仅适用于byteuint2图像。

直方图平滑smooth_histo:首先确定灰度值的相对直方图;然后从直方图提取相关的最小值,作为阈值操作的参数。为了减少最小值,直方图被平滑处理为一个高斯函数,就像在auto_threshold中一样。在平滑直方图中,掩模尺寸增大,直到最小值(即阈值)。

局部动态阈值分割dyn_threshold ------在没有通用的参考图像来确定阴影矫正,或者图像背景非均匀的条件下,很难通过全局阈值进行图像分割。因此局部邻域需要由平滑滤波确定。

ThresholdImage一般为对OrigImage的平滑处理(mean_image/binomial_filter/gauss_filter等)结果,以此作为参考图像。滤波尺寸建议大于被提取物的直径,但是太大会使相邻物体连在一起。

均值和标准偏差局部阈值分割var_threshold ------与dyn_threshold相似。不同和联系如下:

var_threshold集成度更高,并且加入了"标准差因子"这一变量。可以有效地减少噪声对分割的影响;

dyn_threshold是将原图和滤波平滑后的图像对比, var_threshold是将原图和对应像素掩膜覆盖的像素的平均灰度值对比;

var_threshold中,如果参数StdDevScale=0,那么就可以用动态阈值的方式非常近似地模拟;当StdDevScale>0时,var_threshold在黑白过渡处能减少分割出不需要的区域的概率(StdDevScale值不能太小);

双重阈值分割dual_threshold ------只能分割出灰度值高的亮区域,不能分割出灰度值低的暗区域;

自动全局阈值分割auto_threshold------根据直方图确定阈值自动全局阈值分割;

csharp 复制代码
		read_image (Image, '')
		median_image (Image, Median, 'circle', 3, 'mirrored')
		auto_threshold (Median, Regions, 3)

快速全局阈值分割fast_threshold

分水岭算法分割watersheds ------见【issue-halcon例程学习】atoms.hdev

相关推荐
林林宋2 小时前
nvidia&cuda&gpu 关系学习
学习
学编程的闹钟2 小时前
102【php开发准备】
学习
半夏知半秋2 小时前
Elasticsearch 分词器
大数据·学习·elasticsearch·搜索引擎·全文检索
逐云者1233 小时前
Nested Learning:Google Research 正在尝试重新定义深度学习的“学习结构”
人工智能·深度学习·学习·nested learning·google新模型·快慢记忆·学习结构
Ada大侦探3 小时前
新手小白学习Power BI第二弹--------《电商销售数据分析与可视化项目》需求展示
数据库·学习·数据分析
盐焗西兰花3 小时前
鸿蒙学习实战之路-Web 页面适配最佳实践
前端·学习·harmonyos
代码游侠3 小时前
Linux系统编程 - 文件操作
linux·运维·服务器·学习
思成不止于此3 小时前
【MySQL 零基础入门】DML 核心语法全解析:表数据的增删改操作篇
数据库·笔记·sql·学习·mysql
xhyyvr4 小时前
火场自救绳技 ——VR 消防结绳学习系统
学习·vr·vr消防安全·vr消防安全教育·vr安全知识科普·vr技巧培训
Kratzdisteln4 小时前
【Web-Crawler-Steamdt】以项目文件steamdt_crawler.py学习python爬虫
爬虫·python·学习