【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

相关推荐
叶子20242212 分钟前
学习使用YOLO的predict函数使用
人工智能·学习·yolo
jackson凌15 分钟前
【Java学习笔记】SringBuffer类(重点)
java·笔记·学习
黑客老李2 小时前
JavaSec | SpringAOP 链学习分析
java·运维·服务器·开发语言·学习·apache·memcached
海的诗篇_2 小时前
移除元素-JavaScript【算法学习day.04】
javascript·学习·算法
傍晚冰川3 小时前
FreeRTOS任务调度过程vTaskStartScheduler()&任务设计和划分
开发语言·笔记·stm32·单片机·嵌入式硬件·学习
月初,4 小时前
MongoDB学习和应用(高效的非关系型数据库)
学习·mongodb·nosql
casual_clover4 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin
Love__Tay5 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融
我的golang之路果然有问题6 小时前
云服务器部署Gin+gorm 项目 demo
运维·服务器·后端·学习·golang·gin
Lester_11017 小时前
嵌入式学习笔记 - freeRTOS xTaskResumeAll( )函数解析
笔记·stm32·单片机·学习·freertos