【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

相关推荐
西岸行者10 小时前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意11 小时前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码13 小时前
嵌入式学习路线
学习
毛小茛14 小时前
计算机系统概论——校验码
学习
babe小鑫14 小时前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms15 小时前
ROS2知识大白话
笔记·学习·ros2
在这habit之下15 小时前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。15 小时前
2026.2.25监控学习
学习
im_AMBER15 小时前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J15 小时前
从“Hello World“ 开始 C++
c语言·c++·学习