Halcon例程详解(基于卡尺工具的匹配测量方法) —— measure_stamping_part.hdev

前言

1卡尺工具介绍

Halcon中的Metrology方法即为卡尺工具,可用来拟合线,圆,这种方法对于目标比背景很明显的图像尺寸测量是很方便的,不需要用blob进行边缘提取等,但缺点也很明显,需要目标的相对位置基本不变才行。
在这里插入图片描述

2匹配方法概念

HDevelop开发环境中提供的匹配的方法有三种,即Component-Based、Gray-Value-Based、Shape-Based,分别是基于组件的匹配,基于灰度值的匹配和基于形状的匹配,本文所用的例程方法为基于形状的匹配。

例程详解

**模型的名字为基于形状的匹配方法
AlignmentMode := 'shape-based matching'
* AlignmentMode := 'region processing'(区域)
* AlignmentMode := 'rigid transformation'(刚性变换)
* 
* 初始化视图
dev_update_off ()
dev_close_window ()
dev_set_draw ('margin')
gen_empty_obj (EmptyObject)
read_image (Image, 'metal-parts/circle_plate_01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')

在这里插入图片描述

* Part I:
* 
* 初始化卡尺模型
* 定义相机参数
gen_cam_par_area_scan_division (0.0128649, -661.434, 5.30004e-006, 5.3e-006, 620.043, 497.402, Width, Height, CameraParam)
* 测量平面的位姿是通过标定板标定得到的,懂标定的自然懂。
MeasurementPlane := [0.00940956,-0.00481017,0.29128,0.478648,359.65,0.785,0]
* 根据零件的高度和校准板的高度调整测量平面的位姿
CalibPlateThickness := 0.006
PartHeight := 0.005
AdjustThickness := CalibPlateThickness - PartHeight
set_origin_pose (MeasurementPlane, 0, 0, AdjustThickness, MeasurementPlaneAdjusted)
* 
* 创建标定模型并准备标定测量
create_metrology_model (MetrologyHandle)
* 提前设置图像大小,以加快第一次调用apply_metrology_model的速度
set_metrology_model_image_size (MetrologyHandle, Width, Height)
* 在卡尺模型里设置相机参数
set_metrology_model_param (MetrologyHandle, 'camera_param', CameraParam)
* 设置被测对象的位姿
set_metrology_model_param (MetrologyHandle, 'plane_pose', MeasurementPlaneAdjusted)
* 
* Add the objects to be measured to the metrology model
* 
* 添加圆的参数(由上图可知有四个完整的圆):行列坐标,半径
CircleParam := [354,274,53]
CircleParam := [CircleParam,350,519,53]
CircleParam := [CircleParam,345,764,52]
CircleParam := [CircleParam,596,523,53]
add_metrology_object_generic (MetrologyHandle, 'circle', CircleParam, 20, 5, 1, 30, [], [], CircleIndices1)
* 
* 添加两个残缺的圆。
CircleParam1 := [583,1010,79]
CircleParam2 := [336,1005,77]
*角度不同,故写了两次
add_metrology_object_generic (MetrologyHandle, 'circle', CircleParam1, 20, 5, 1, 30, ['start_phi','end_phi'], [0,rad(185)], CircleIndices2)
add_metrology_object_generic (MetrologyHandle, 'circle', CircleParam2, 20, 5, 1, 30, ['start_phi','end_phi'], [rad(45),rad(185)], Index3)
CircleIndices2 := [CircleIndices2,Index3]
* 
* 添加一个矩形
RectangleParam := [599,279,rad(90),62,51]
add_metrology_object_generic (MetrologyHandle, 'rectangle2', RectangleParam, 20, 5, 1, 30, [], [], RectIndices)* 添加两条线(边界线)
Line1 := [143,1122,709,1132]
Line2 := [151,153,136,1115]
add_metrology_object_generic (MetrologyHandle, 'line', [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)
* 检查已添加到计量模型中的形状
get_metrology_object_model_contour (ModelContour, MetrologyHandle, 'all', 1.5)
get_metrology_object_measures (MeasureContour, MetrologyHandle, 'all', 'all', Row, Column)
Message := 'This example shows how to measure geometric shapes using a'
Message[1] := 'metrology model. As preparation, their roughly known '
Message[2] := 'dimensions and tolerances are specified by the user.'
show_contours (Image, ModelContour, MeasureContour, EmptyObject, WindowHandle, Message)
stop ()

在这里插入图片描述

* Part 2:
* 
* 准备匹配
* 
* a) Shape-based matching
if (AlignmentMode == 'shape-based matching')dev_set_part (-Height / 2 - 100, -Width / 2, 1.5 * Height - 100, 1.5 * Width)* * 创建用于图像中计量模型匹配的形状模型,其中对象的位置和方向与用于创建模型的图像中对象的位置和方向不同。* 得到当前halcon系统的参数值get_system ('border_shape_models', BorderShapeModel)set_system ('border_shape_models', 'true')*阈值处理并截取区域threshold (Image, Region, 0, 50)dilation_rectangle1 (Region, ModelRegion, 5, 5)reduce_domain (Image, ModelRegion, ImageReduced)*创建用于匹配的模型:参数(Template : : 金字塔级的数量, 起始角度, 角度范围, 角度的步长,优化的类型, 匹配度规, 阈值, 目标最小对比值 : ModelID)create_shape_model (ImageReduced, 6, 0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 20, ShapeModelID)*将模型的原点设置为输入区域的中心area_center (ModelRegion, Area, RowModel, ColumnModel)*得到形状模型的轮廓get_shape_model_contours (ShapeModelContours, ShapeModelID, 1)Message := 'A shape model will be used for the alignment of the metrology'Message[1] := 'model. The contours of the shape model (white) and of the'Message[2] := 'metrology model (blue) are shown.'show_contours (Image, ModelContour, EmptyObject, ShapeModelContours, WindowHandle, Message)* * 更改定义卡尺模型的参考系统,使之与形状模型所使用的对应。set_metrology_model_param (MetrologyHandle, 'reference_system', [RowModel,ColumnModel,0])*得到卡尺模型的轮廓,参数:(模型轮廓,句柄,卡尺测量对象的索引,相邻两个轮廓点的距离get_metrology_object_model_contour (ModelContour, MetrologyHandle, 'all', 1.5)Message := 'To prepare the alignment, the origin of the shape model'Message[1] := 'is set as the reference system of the metrology model.'show_contours (Image, ModelContour, EmptyObject, ShapeModelContours, WindowHandle, Message)stop ()dev_set_part (0, 0, Height - 1, Width - 1)
endif
* 

在这里插入图片描述
*另外两种匹配模型:基于区域和仿射变换。

* b) Region processing
if (AlignmentMode == 'region processing')* Determine reference position and orientationthreshold (Image, Region, 0, 50)fill_up (Region, RegionFillUp)difference (RegionFillUp, Region, OriginalRegion)area_center (OriginalRegion, Area, RowOrig, ColumnOrig)orientation_region (OriginalRegion, AngleOrig)* Change the reference system of the metrology modelset_metrology_model_param (MetrologyHandle, 'reference_system', [RowOrig,ColumnOrig,AngleOrig])
endif
* 
* c) Rigid transformation
if (AlignmentMode == 'rigid transformation')* Reference points:extract_reference_points (Image, RowReference, ColumnReference)gen_cross_contour_xld (ReferencePoints, RowReference, ColumnReference, 15, 0.785398)dev_display (Image)dev_set_color ('white')dev_display (ReferencePoints)Message := 'To prepare the alignment, reference points are extracted.'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_message (WindowHandle, [1:4], 'image', RowReference, ColumnReference, 'black', 'true')stop ()
endif
* 
* 线上阶段
for I := 2 to 5 by 1read_image (CurrentImage, 'metal-parts/circle_plate_' + I$'02d')dev_set_line_width (1)dev_display (CurrentImage)* * a) Shape-based matchingif (AlignmentMode == 'shape-based matching')* * 测量物体的位置和方向,使用find_shape_model算子。* 参数:(测试图,句柄,搜索角度,范围,模型实例的最小分数,模型数量,最大重叠度,亚像素精度,金字塔层数,* 搜索贪婪度(这个值在很大程度上影响着搜索速度,若为0,则为启发式搜索,若为1,则为不安全搜索),模型的行坐标,列坐标,角度,分数)find_shape_model (CurrentImage, ShapeModelID, 0, rad(360), 0.5, 1, 0, 'least_squares', 5, 0.9, RowAlign, ColumnAlign, AngleAlign, Score)dev_display_shape_matching_results (ShapeModelID, 'white', RowAlign, ColumnAlign, AngleAlign, 1, 1, 0)endif* * b) Region processingif (AlignmentMode == 'region processing')* Determine the current position and orientationthreshold (CurrentImage, Region, 0, 50)fill_up (Region, RegionFillUp)difference (RegionFillUp, Region, CurrentRegion)area_center (CurrentRegion, Area, RowAlign, ColumnAlign)orientation_region (CurrentRegion, AngleAlign)endif* * c) Rigid transformationif (AlignmentMode == 'rigid transformation')* Referenzpunkte:extract_reference_points (CurrentImage, RowExtracted, ColumnExtracted)gen_cross_contour_xld (ExtractedPoints, RowExtracted, ColumnExtracted, 15, 0.785398)dev_display (CurrentImage)dev_set_color ('white')dev_display (ExtractedPoints)disp_message (WindowHandle, [1:4], 'image', RowExtracted, ColumnExtracted, 'black', 'true')vector_to_rigid (RowReference, ColumnReference, RowExtracted, ColumnExtracted, HomMat2D)hom_mat2d_to_affine_par (HomMat2D, Sx, Sy, AngleAlign, Theta, RowAlign, ColumnAlign)endif
    * * 使用计算的位置和方向将计量模型与当前发生的事件对齐align_metrology_model (MetrologyHandle, RowAlign, ColumnAlign, AngleAlign)* 展示匹配效果if (I <= 2)**展示提取的轮廓get_metrology_object_model_contour (ModelContour, MetrologyHandle, 'all', 1.5)dev_set_color ('blue')dev_set_line_width (2)dev_display (ModelContour)Message := 'In each image, the object is matched and aligned'Message[1] := 'before the metrology measurement.'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()endif* *在一次调用中对所有计量对象执行测量apply_metrology_model (CurrentImage, MetrologyHandle)* 获取测量区域以进行可视化get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', Row, Column)* 获取用于拟合几何形状的边缘点get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'row', UsedRow)get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'column', UsedColumn)gen_cross_contour_xld (UsedEdges, UsedRow, UsedColumn, 10, rad(45))* * 获取测量结果* 由于设置了摄像机参数,所有结果都以相对于测量平面所定义的坐标系的度量坐标给出* * 得到所有的轮廓目标get_metrology_object_result_contour (ResultContours, MetrologyHandle, 'all', 'all', 1.5)* 提取小圆的半径get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'radius', RadiusC1)* 提取较大的不完整圆的半径get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'radius', RadiusC2)* 提取矩形边的长度get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length1', Length1R)get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length2', Length2R)* 获取每条测量线的起点和终点get_metrology_object_result (MetrologyHandle, LineIndices[0], 'all', 'result_type', 'all_param', ParamLine1)get_metrology_object_result (MetrologyHandle, LineIndices[1], 'all', 'result_type', 'all_param', ParamLine2)* Display the resultsdev_display (CurrentImage)dev_set_line_width (1)dev_set_color ('light gray')dev_display (Contour)dev_set_color ('green')dev_set_line_width (2)dev_display (ResultContours)dev_set_line_width (1)dev_set_color ('white')dev_display (UsedEdges)* * 显示圆心处每个圆的半径* 获取圆心的度量坐标get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'x', XC1)get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'y', YC1)* 将圆心的度量坐标投影到图像中,得到圆心的图像坐标project_xy_to_image (XC1, YC1, MeasurementPlaneAdjusted, CameraParam, Row1, Column1)get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'x', XC2)get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'y', YC2)project_xy_to_image (XC2, YC2, MeasurementPlaneAdjusted, CameraParam, Row2, Column2)disp_message (WindowHandle, 'r=' + (RadiusC1 * 1000)$'.2f', 'image', Row1, Column1 - 80, 'black', 'true')disp_message (WindowHandle, 'r=' + (RadiusC2 * 1000)$'.2f', 'image', Row2, Column2 - 80, 'black', 'true')get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'x', XRectangle)get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'y', YRectangle)project_xy_to_image (XRectangle, YRectangle, MeasurementPlaneAdjusted, CameraParam, RowR, ColumnR)Area := Length1R * Length2R * 4 * 1000 * 1000disp_message (WindowHandle, 'area=' + Area$'.2f', 'image', RowR, ColumnR - 120, 'black', 'true')Message := 'Measured metric results after alignment (r in mm, area in mm^2):'* disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')if (I < 5)disp_continue_message (WindowHandle, 'black', 'true')endifstop ()endfor
if (AlignmentMode == 'shape-based matching')set_system ('border_shape_models', BorderShapeModel)
endif

总结

卡尺工具在halcon中的使用比较简单,基于匹配的卡尺测量分为以下几个步骤:

  1. 创建卡尺模型,添加测量项信息,create_metrology_model ,add_metrology_object_generic。
  2. 创建用于形状匹配的匹配模型,create_shape_model。
  3. 用匹配模型与实际图像进行匹配,find_shape_model
  4. 展示匹配结果,get_metrology_object_model_contour,apply_metrology_model,get_metrology_object_measures,get_metrology_object_result。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256332.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【TensorFlow】——不同shape的tensor在神经网络中的应用(scalar,vector,matrix)

目录 ​ 1、scalar——标量 1&#xff09;在神经网络中存在的场景 2&#xff09;one_hot编码 3&#xff09;举例应用 2、vector——向量 ​ 3、matrixs——矩阵 4、dim3的tensor 5、dim4的tensor 6、dim5的tensor 本文主要的目的是让初学者对tensor的各种形式的使用场…

Java - I/O

File类 java.io操作文件和目录&#xff0c;与平台无关。具体的常用实例方法&#xff1a; File file new File("."); // 以当前路径创建名为 "." 的 File 对象   文件目录信息函数     -   String getName/Path/Parent()&#xff1a; 文件名/路径…

Halcon —— 边缘检测算子详解

一、算子介绍 1.1 种类 halcon内常用的边缘检测算子包括如下几种&#xff1a; 1.edges_image: 提取2D 图像边缘 2.edges_sub_pix&#xff1a;提取2D图像亚像素边缘 3.edges_object_model_3d &#xff1a;提取3D图像边缘 4.edges_color和edges_color_sub_pix&#xff1a;提取彩…

【TensorFlow】——索引与切片

目录 1、利用index进行索引 2、利用“&#xff1a;”和“...”进行索引与切片 3、tf.gather&#xff08;&#xff09;——对一个维度进行乱序索引 优势&#xff1a; 缺点&#xff1a; 例子 4、tf.gather_nd()——同时对多个维度进行索引 5、tf.boolean_mask()——通过布…

华硕(ASUS)X554LP笔记本一开机就进入aptio setup utility 问题的解决

某次因大意一直未插电&#xff0c;华硕&#xff08;ASUS&#xff09;X554LP笔记本后来没电关机。后来每次一开机就进入aptio setup utility界面&#xff0c;按F9调入默认配置&#xff0c;F10保存后退出&#xff0c;重启仍然进入aptio setup utility。 网上查了一下&#xff0c;…

【TensorFlow】——broadcast_to(在不复制内存的情况下自动扩张tensor)

目录 作用&#xff1a; 内在的思路 优点 什么时候可以broadcast ​ tf.boradcast_to .VS tf.tile 作用&#xff1a; 在不会实际意义上复制数据的情况下进行tensor的维度和形状的扩张&#xff0c;使得两个tensor维度和形状一致 对两个维度不一致的tensor进行加减前进行br…

20145212 《信息安全系统设计基础》第2周学习总结

20145212 《信息安全系统设计基础》第2周学习总结 教材学习内容总结 Vim基本操作 1.使用vim命令进入vim界面vim后面加上你要打开的已存在的文件名或者不存在&#xff08;则作为新建文件&#xff09;的文件名。 打开Xfce终端&#xff0c;输入以下命令$ vim practice_1.txt 直接使…

Opencv—— 拟合直线

概念 最小二乘法是勒让德( A. M. Legendre)于1805年在其著作《计算慧星轨道的新方法》中提出的。 最小二乘法就是通过最小化误差的平方和&#xff0c;使得拟合对象无限接近目标对象。在图像处理中主要用于拟合线&#xff0c;通过求采样点距离误差最小的线&#xff0c;可以是直…

本地搭建Dubbo监控中心的安装步骤

Dubbo监控中心的安装步骤 参考链接&#xff1a;http://blog.csdn.net/lichunan/article/details/40349645 一、从github上下载dubbo源码进行编译&#xff1a; 1、下载地址为&#xff1a; https://github.com/alibaba/dubbo.git2、编译源码命令&#xff1a;首先进入~/dubbo&am…

【TensorFlow】——实现minist数据集分类的前向传播(常规神经网络非卷积神经网络)

目录 一、常规神经网络模型 二、TensorFlow实现前向传播步骤 1、读取数据集 2、batch划分 3、根据神经网络每一层的神经元个数来初始化参数w,b 4、进行每一层输入输出的计算 5、对每一层的输出进行非线性relu函数变换 6、计算一个batch训练后的误差loss 7、计算每一次…

神经网络(11)--具体实现:unrolling parameters

我们需要将parameters从矩阵unrolling到向量&#xff0c;这样我们就可以使用adanced optimization routines. unroll into vectors costFunction与fminunc里面的theta都是n1维的向量&#xff0c;costFunction的返回值gradient也是n1维的向量。 但是当我们使用神经网络时&#x…

js学习之地图生成

首先&#xff0c;上地图图片 接着&#xff0c;js&#xff0c;我们可以把图片看成一块块32*32像素的 var i; var j;window.onload function () {gamemap(15, 10, 10, "map.jpg"); }var mapimg new Image(); var map [[18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,…

下拉框选择

1&#xff0c;简单下拉框 <!DOCTYPE html> <html> <head lang"en"><meta charset"UTF-8" ><meta name"viewport" content"widthdevice-width, initial-scale1, user-scalableno, minimal-ui, maximum-scale1&qu…

基于C#的TCP/IP协议应用(一)

一、背景与概念 1.标准以太网 以太网是美国Xerox&#xff08;施乐&#xff09;公司的Palo Alto研究中心于1975年研制成功的&#xff0c;其核心技术起源于ALOHA网。目前以太网是指符合IEEE 802.3标准的局域网(LAN)产品组&#xff0c;其中IEEE 802.3是一组电气与电子工程师协会…

WORD文档的超链接无法打开——“由于本机的限制该操作已被取消”的解决方法

之前我电脑还很正常&#xff0c;最近装了一个打印机后&#xff0c;放在word文档的文字超链接就打不开了&#xff0c;提示说“由于本机的限制该操作已被取消”&#xff0c;请各位高手指导下该怎样解决这个问题&#xff1f; WORD文档的超链接无法打开的原因是文件在插入超链接之…

配置https

引子&#xff1a; 最近在一篇文章中了解到EFF(电子前哨基金会)为了推广https协议&#xff0c;成立了一个letsencrypt项目&#xff0c;可以发放免费的证书&#xff0c;此证书可以被大多数主流浏览器所信任&#xff0c;这个邪恶的念头一爆发&#xff0c;就让我走上了一条坎坷的不…

CentOS 6.5安装VNC server

1. 安装桌面&#xff0c;安装时选择了Desktop可以忽略 # yum groupinstall Desktop # yum install gnome-core xfce4 firefox 2. 安装VNC server # yum install tigervnc-server 3. 配置服务 # chkconfig vncserver on 4. 设置VNC用户密码 # vncpasswd 5. 配置文件 # vi /etc/s…

C#中数据类型及其转换知识点汇总

概念 C#中数据类型分为两大类&#xff0c;分别是值类型和引用类型。 值类型变量是从类 System.ValueType 中派生出来的&#xff0c;当声明一个值类型变量时&#xff0c;系统分配内存来存储值。 整形 包括8种类型&#xff0c;区别在于字节数和有无符号。 浮点型 float占用…

10亿个字符串的排序问题

一、问题描述 有一个大文件&#xff0c;里面有十亿个字符串&#xff0c;乱序的&#xff0c;要求将这些字符串以字典的顺序排好序 二、解决思路 将大文件切割成小文件&#xff0c;每个小文件内归并排序&#xff1b; 对所有的小文件进行归并排序——多重归并排序 三、解决方案 3.…