【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

相关推荐
2303_Alpha13 小时前
SpringBoot
笔记·学习
萘柰奈13 小时前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
沐矢羽14 小时前
Tomcat PUT方法任意写文件漏洞学习
学习·tomcat
好奇龙猫14 小时前
日语学习-日语知识点小记-进阶-JLPT-N1阶段蓝宝书,共120语法(10):91-100语法+考え方13
学习
向阳花开_miemie14 小时前
Android音频学习(十八)——混音流程
学习·音视频
工大一只猿14 小时前
51单片机学习
嵌入式硬件·学习·51单片机
c0d1ng15 小时前
量子计算学习(第十四周周报)
学习·量子计算
Hello_Embed1 天前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中1 天前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Magnetic_h1 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa