【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

相关推荐
im_AMBER4 分钟前
React 11 登录页项目框架搭建
前端·学习·react.js·前端框架
py有趣7 小时前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
BreezeJuvenile8 小时前
外设模块学习(15)——MQ-2烟雾气体传感器(STM32)
stm32·单片机·学习·mq-2·烟雾气体传感器
XH1.10 小时前
学习RT-thread(RT-thread定时器)
stm32·单片机·学习
2301_7965125211 小时前
Rust编程学习 - 为什么说Cow 代表的是Copy-On-Write, 即“写时复制技术”,它是一种高效的 资源管理手段
java·学习·rust
故里213011 小时前
学习前端记录(二)21-40
学习
ThreeYear_s11 小时前
电力电子技术学习路径与FPGA/DSP技术结合方向(gemini生成)
学习·fpga开发
好奇龙猫12 小时前
【生活相关-日语-日本-入国&出国-海关&市役所(4)-办理手续】
学习·生活
sendnews12 小时前
红松小课如何成为激活老年人生活的新引擎?从兴趣学习到价值重塑!
学习·生活
The_Second_Coming12 小时前
ELK 学习笔记
笔记·学习·elk