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.pswp.cn/news/256332.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256332.shtml
英文地址,請注明出處:http://en.pswp.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的各種形式的使用場…

404頁面 3秒后跳到首頁 實現

---恢復內容開始--- 當我們訪問一個頁面不存在的時候&#xff0c;就會跳到404頁面 一般網站都在在404頁面中做一個處理&#xff0c; 就是當用戶3秒種內還沒有任何操作的話&#xff0c;就會自動跳轉到其它頁面 技術實現有兩種方法 1. 在404頁面中的header間加上 <meta http-e…

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;…

redis和memcached緩存

memcached memcache開源的&#xff0c;高性能&#xff0c;高并發分布式內存緩存系統,天生支持集群 memcached下載地址&#xff1a; http://memcached.org/downloads python實現memcached緩存 pip3 install python-memcached import memcache aamemcache.Client(["10.0.0.2…

C# —— 進程與線程的理解

定義 進程 進程&#xff08;Process&#xff09;是Windows系統中的一個基本概念&#xff0c;它包含著一個運行程序所需要的資源。一個正在運行的應用程序在操作系統中被視為一個進程&#xff0c;進程可以包括一個或多個線程。 程序是在特定操作系統上的可執行文件&#xff0c…

git安裝和初步使用

基本參照以下鏈接&#xff1a; http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000 注意&#xff1a; &#xff08;1&#xff09;每臺電腦對于github來說都有對應的ssh密鑰&#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…

(轉)SQLServer實例講解

歡迎和大家交流技術相關問題&#xff1a; 郵箱: jiangxinnju163.com 博客園地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://github.com/jiangxincode 知乎地址: https://www.zhihu.com/people/jiangxinnju 實例就是一個在數據庫中存在的&#xff0c;現實的數據…

Basler相機SDK相關問題收集與知識匯總

1.心跳時間導致報錯 此心跳非彼心跳,相機心跳是指過通過設置一個時間用于監測相機和網卡的連接,如果超過這個時間,相機就會釋放跟網卡的連接,進入一種允許相機被再次連接的狀態。 但此方式會導致相機SDK報錯,經常顯示在使用中,無可用連接,如何解決? 1.干等。不可取 2.…

14-會場安排問題

會場安排問題 時間限制&#xff1a;3000 ms | 內存限制&#xff1a;65535 KB難度&#xff1a;4描述學校的小禮堂每天都會有許多活動&#xff0c;有時間這些活動的計劃時間會發生沖突&#xff0c;需要選擇出一些活動進行舉辦。小劉的工作就是安排學校小禮堂的活動&#xff0c;…

【TensorFlow】——實現minist數據集分類的前向傳播(常規神經網絡非卷積神經網絡)

目錄 一、常規神經網絡模型 二、TensorFlow實現前向傳播步驟 1、讀取數據集 2、batch劃分 3、根據神經網絡每一層的神經元個數來初始化參數w,b 4、進行每一層輸入輸出的計算 5、對每一層的輸出進行非線性relu函數變換 6、計算一個batch訓練后的誤差loss 7、計算每一次…

大道至簡第二章

懶人造就了方法 開頭說的&#xff0c;有一個人叫李冰也要移山&#xff0c;可是他卻要用和愚公截然不同的方法&#xff0c;那就是用“燒”的方法來碎石&#xff0c;那么這樣的好處就是省力。那么他又是怎樣想到的辦法的呢&#xff0c;就是生活中仔細觀察他老婆做飯時水溢出是石頭…

C# 回調函數詳解

回調和回調函數是什么 軟件模塊之間存在著接口&#xff0c;而回調是一種雙向調用模式&#xff0c;被調用方在接口被調用時也會調用對方的接口。 對于一般的結構化語言&#xff0c;可以通過回調函數來實現回調。回調函數是一個函數或過程&#xff0c;是一個由調用方自己實現&am…

神經網絡(11)--具體實現:unrolling parameters

我們需要將parameters從矩陣unrolling到向量&#xff0c;這樣我們就可以使用adanced optimization routines. unroll into vectors costFunction與fminunc里面的theta都是n1維的向量&#xff0c;costFunction的返回值gradient也是n1維的向量。 但是當我們使用神經網絡時&#x…