Halcon視覺輪廓線擬合測量-@龍熙視覺培訓

Halcon基礎視頻教程
——帶你進入圖像處理的世界

在這裏插入圖片描述

1.方法1 輪廓線擬合測量

1.1.測量1–Measure pump
* step: create and select relevant contours
*
threshold_sub_pix (ImageReduced, Border, 80)
select_shape_xld (Border, SelectedXLD, [‘contlength’,‘outer_radius’], ‘and’, [70,15], [99999,99999])
segment_contours_xld (SelectedXLD, ContoursSplit, ‘lines_circles’, 4, 2, 2)
select_shape_xld (ContoursSplit, SelectedXLD3, [‘outer_radius’,‘contlength’], ‘and’, [15,30], [45,99999])
union_cocircular_contours_xld (SelectedXLD3, UnionContours2, 0.5, 0.1, 0.2, 2, 10, 10, ‘true’, 1)
sort_contours_xld (UnionContours2, SortedContours, ‘upper_left’, ‘true’, ‘column’)

在這裏插入圖片描述

1.2 鑽石角度測量 -XLD

2.2D測量–卡尺測量工具
序號 名稱 案例
1 add_metrology_object_rectangle2_measure Apply_metrology_model.hdev案例
2 add_metrology_object_circle_measure Apply_metrology_model.hdev案例
3 add_metrology_object_ellipse_measure
4 add_metrology_object_line_measure Apply_model_diamond.hdev鑽石角度測量案例(形態學找到位置,然後應用2維線測量)
5 add_metrology_object_generic Measure_stamping_part匹配+2維測量案例(應用匹配找到位置)
6 add_metrology_object_line_measure Point_line_to_hom_mat2d(對halcon圖像進行點線變換)

apply_metrology_model.hdev案例

2.1二維計量 create_metrology_model
Metrology: 測量
MetrologyHandle 手柄
create_metrology_model(算子)
create_metrology_model - 創建測量幾何形狀所需的數據結構。
create_metrology_model(::: MetrologyHandle)

描述
create_metrology_model創建計量模型,即通過2D計量測量具有特定幾何形狀(計量對象)的對象所需的數據結構,並將其返回到手柄MetrologyHandle中。

二維計量的基本原理
通常,對於2D度量,必須提供要測量的對象的位置,方向和幾何形狀的近似值。在顯示對象的圖像內,這些近似對象的邊界用於定位對象的真實邊緣以適應幾何形狀的參數,使得它們最佳地適合圖像數據。測量結果是優化的參數。計量模型用於存儲所有必要的信息,例如計量對象的位置和幾何形狀的初始參數,控制測量的參數以及測量結果。可以通過2D計量測量的幾何形狀包括圓形,橢圓形,矩形和線條。

圖像中對象的邊緣位於所謂的測量區域內。這些是矩形區域,其垂直於近似物體的邊界佈置,使得它們的中心位於邊界上。調整測量區域的尺寸和分佈的參數與每個測量對象的近似形狀參數一起指定。

當應用測量時,使用RANSAC算法確定所有測量區域內的邊緣位置並使其適合幾何形狀。

2.2 set_metrology_model_image_size
設置被測量圖像的尺寸大小

2.3增加一個形狀(矩形,圓形)到測量模型

共有5種類型
1 add_metrology_object_rectangle2_measure
2 add_metrology_object_circle_measure
3 add_metrology_object_ellipse_measure
4 add_metrology_object_line_measure
5 add_metrology_object_generic

//2.3.1增加一個矩形到測量模型 矩形2
//參數說明: MetrologyHandle指測量模型的句柄, Row爲矩形中心的y座標值, Column爲矩形中心的x座標值, Phi爲矩形的長半軸的方向,
//Length1爲矩形長半軸的長度, Length2爲矩形短半軸的長度,
MeasureLength1相對於長半軸垂直邊界的檢測區域一半長度, RectangleTolerance 10
//MeasureLength2相對於短半軸垂直邊界的檢測區域一半長度, 5
MeasureSigma高斯平滑用到的參數, .5
MeasureThreshold最小邊界的閾值, 1
// GenParamName指定通用參數的名稱, []
GenParamValue指定通用參數的值, []
Index指定創建測量對象的索引 -MetrologyRectangleIndice
add_metrology_object_rectangle2_measure (MetrologyHandle, RectangleInitRow, RectangleInitColumn, RectangleInitPhi, RectangleInitLength1, RectangleInitLength2, RectangleTolerance, 5, .5, 1, [], [], MetrologyRectangleIndices)

// 增加一個圓形到測量模型 圓形2

矩形

//measure_stamping part
測量衝壓件

2.3.2 add_metrology_object_generic(:: MetrologyHandle,Shape,ShapeParam,MeasureLength1,MeasureLength2,MeasureSigma,MeasureThreshold,Gen​​ParamName,GenParamValue:Index)

  • Add several full circles
    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)
  • Add two partial circles
    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]
  • Add a retangle
    RectangleParam := [599,279,rad(90),62,51]
    add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices)
  • Add two lines
    Line1 := [143,1122,709,1132]
    Line2 := [151,153,136,1115]
    add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)

描述
add_metrology_object_generic將類型爲Shape的計量對象添加到計量模型中並準備矩形度量區域(有關2D度量的基本原理,請參閱create_metrology_model)。模型的句柄在MetrologyHandle中傳遞。 Shape指定將哪種類型的對象添加到計量模型中。運算符add_metrology_object_generic在參數Index中返回添加的度量對象的索引。請注意,add_metrology_object_generic在一個運算符中提供運算符add_metrology_object_circle_measure,add_metrology_object_ellipse_measure,add_metrology_object_rectangle2_measure和add_metrology_object_line_measure的功能。

序號 名稱 解釋
1 可能的形狀 ‘圓形’:‘旋轉矩形’:‘橢圓’:‘線’:
2 測量區域 ShapeParam

3 測量區域的定義 MeasureLength1 MeasureLength2

4 高斯平滑用到的參數 MeasureSigma

5 最小邊緣閾值 MeasureThreshold

根據Shape中指定的對象,需要以下值:

‘圓形’:
類型圓的計量對象的幾何形狀由其中心(行,列)和半徑指定。
ShapeParam = [行,列,半徑]
‘旋轉矩形’:
矩形類型的計量對象的幾何形狀由其中心(行,列),主軸Phi的方向以及半邊長度Length1和Length2指定。 Phi的輸入值自動映射到間隔。
ShapeParam = [行,列,Phi,長度1,長度2]
‘橢圓’:
橢圓類型的計量對象的幾何形狀由其中心(行,列),主軸Phi的方向,較大的半軸Radius1的長度以及較小的半軸Radius2的長度來指定。 Phi的輸入值自動映射到間隔。
ShapeParam = [行,列,Phi,Radius1,Radius2]
‘線’:
類型線的度量對象的幾何形狀由其起點(RowBegin,ColumnBegin)的座標和其終點的座標(RowEnd,ColumnEnd)描述。
ShapeParam = [RowBegin,ColumnBegin,RowEnd,ColumnEnd]
測量區域的定義
add_metrology_object_generic還準備矩形度量區域。矩形測量區域垂直於物體的邊界。與對象邊界垂直和相切的測量區域的半邊長度在MeasureLength1和MeasureLength2中設置。測量區域的中心位於物體的邊界上。參數MeasureSigma指定運算符apply_metrology_model用於平滑圖像灰度值的標準偏差。可以使用參數MeasureThreshold選擇突出邊緣,該參數構成幅度的閾值,即邊緣的一階導數的絕對值。

通用參數
可以使用GenParamName和GenParamValue指定通用參數及其值。運算符set_metrology_object_param中可用的所有通用參數也可以在add_metrology_object_generic中設置。但請注意,對於許多應用程序,默認值足夠,無需進行任何調整。此外,GenParamName和GenParamValue的以下值僅適用於Shape =‘circle’和’ellipse’:

多線程類型:可重入(與非獨佔運算符並行運行)。
多線程範圍:全局(可以從任何線程調用)。
沒有並行化處理。
此運算符修改以下輸入參數的狀態:
MetrologyHandle
沒有外部同步,可能無法在多個線程之間共享此參數的值。
參數
MetrologyHandle(input_control,狀態被修改)metrology_model→(整數)
處理計量模型。
Shape(input_control)attribute.name(-array)→(string)
要添加的計量對象的類型。
默認值:‘circle’
值列表:‘circle’,‘ellipse’,‘line’,‘rectangle2’
ShapeParam(input_control)attribute.value-array→(實/整數)
要添加的計量對象的參數。
MeasureLength1(input_control)數字→(實/整數)
垂直於邊界的測量區域的一半長度。
默認值:20.0
建議值:10.0,20.0,30.0
典型值範圍:1.0≤MeasureLength1≤511.0(lin)
最小增量:1.0
建議增量:10.0
MeasureLength2(input_control)數字→(實/整數)
與邊界相切的測量區域的一半長度。
默認值:5.0
建議值:3.0,5.0,10.0
典型值範圍:1.0≤MeasureLength2≤511.0(lin)
最小增量:1.0
建議增量:10.0

MeasureSigma(input_control)數字→(實/整數)
Sigma的高斯函數用於平滑。
默認值:1.0
建議值:0.4,0.6,0.8,1.0,1.5,2.0,3.0,4.0,5.0,7.0,10.0
典型值範圍:0.4≤MeasureSigma≤100(lin)
最小增量:0.01
建議增量:0.1
限制:(0.4 <= MeasureSigma)&&(MeasureSigma <= 100)
MeasureThreshold(input_control)數字→(實/整數)
最小邊緣幅度。
默認值:30.0
建議值:5.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,90.0,110.0
典型值範圍:1≤MeasureThreshold≤255(lin)
最小增量:0.5
建議增量:2

GenParamName(input_control)attribute.name(-array)→(string)
通用參數的名稱。
默認值: []
值列表:‘distance_threshold’,‘end_phi’,‘instances_outside_measure_regions’,‘max_num_iterations’,‘measure_distance’,‘measure_interpolation’,‘measure_select’,‘measure_transition’,‘min_score’,‘num_instances’,‘num_measures’,'point_order ',‘rand_seed’,‘start_phi’

‘start_phi’:
該參數指定圓弧或橢圓弧的起點處的角度。對於橢圓,起點處的角度是相對於正主軸測量的,並且對應於橢圓的最小周圍圓。橢圓的實際起點是橢圓與相應圓點在主軸上的正交投影的交點。要創建閉合圓或橢圓,參數’start_phi’的值設置爲0,參數’end_phi’的值設置爲(帶正點順序)。輸入值自動映射到間隔。

值列表:0.0,0.78,6.28318

默認值:0.0

‘end_phi’:
該參數指定圓弧或橢圓弧的終點處的角度。對於橢圓,終點處的角度是相對於正主軸測量的,並且對應於橢圓的最小周圍圓。橢圓的實際終點是橢圓與相應圓點在主軸上的正交投影的交點。要創建閉合圓或橢圓,參數’start_phi’的值設置爲0,參數’end_phi’的值設置爲(帶正點順序)。輸入值自動內部映射到間隔。

值列表:0.0,0.78,6.28318

默認值:6.28318

‘point_order’:
該參數指定圓弧或橢圓弧的方向。對於值“正”,弧在數學正方向(逆時針方向)的’start_phi’和’end_phi’之間定義。對於值“負”,弧在數學上爲負方向(順時針)定義在’start_phi’和’end_phi’之間。

價值表:‘正面’,‘負面’

默認值:‘正面’

GenParamValue(input_control)attribute.value(-array)→(real / integer / string)
通用參數的值。
默認值: []
建議值:1,2,3,4,5,10,20,‘all’,‘true’,‘false’,‘first’,‘last’,‘positive’,‘negative’,‘uniform’,’ nearest_neighbor’,‘bilinear’,‘bicubic’

索引(output_control)整數→(整數)
創建的計量對象的索引。

2.3.3 Halcon找線找圓的設置參數用法總結
set_metrology_object_param
2.3.3.1

  • 可以在一次調用中爲每個計量對象測量多個圓 / 矩形 / 線 / 橢圓 * 實例。 * 因爲測量兩個圓,* 我們將‘ num instances’設置爲2。
    set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, ‘num_instances’, 2)
  • 設定「測量過渡」至「均勻」可確保只會返回唯一的圓圈,即只有由亮到暗的邊緣或由亮到暗的邊緣。 因爲一致性檢查增加了運行時間,所以從默認切換爲均勻姿態uniform。 在這個例子中,因爲同時存在正面和負面的邊緣,打開它更安全。
    set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, ‘measure_transition’, ‘uniform’)
  • Setting the minimum score can make the results more robust 確保結果更可靠
    set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, ‘min_score’, .9)

2.3.4對一個圖像進行測量並且匹配指定的測量對象

//對一個圖像進行測量並且匹配指定的測量對象

  • Perform the measurement進行測量
    apply_metrology_model (Image, MetrologyHandle)
    2.3.5 獲取測量結果

序號 算子 註釋
1 get_metrology_object_fuzzy_param 模糊參數
2 get_metrology_object_indices 指數
3 get_metrology_object_measures 獲取測量點和矩形外觀
4 get_metrology_object_model_contour 模型輪廓
5 get_metrology_object_num_instances 數字實例
6 get_metrology_object_param 參數
7 get_metrology_object_result 結果
8 get_metrology_object_result_contour 獲取結果輪廓

矩形:
輸入:MetrologyHandle, MetrologyRectangleIndices, ‘all’, ‘result_type’, ‘all_param’
輸出:RectangleParameter
get_metrology_object_result (MetrologyHandle, MetrologyRectangleIndices, ‘all’, ‘result_type’, ‘all_param’, RectangleParameter)
對 句柄 MetrologyHandle中的 [0,1] 進行測量

Sequence1 := [0:5:|RectangleParameter| - 1]
*這裏是5個數值
RectangleRow := RectangleParameter[Sequence1]
RectangleColumn := RectangleParameter[Sequence1 + 1]
RectanglePhi := RectangleParameter[Sequence1 + 2]
RectangleLength1 := RectangleParameter[Sequence1 + 3]
RectangleLength2 := RectangleParameter[Sequence1 + 4]

圓形:

  • Access the results of the circle measurement
    get_metrology_object_result (MetrologyHandle, MetrologyCircleIndices, ‘all’, ‘result_type’, ‘all_param’, CircleParameter)
  • Extract the parameters for better readability
    Sequence2 := [0:3:|CircleParameter| - 1]

*這裏是3個數值
CircleRow := CircleParameter[Sequence2]
CircleColumn := CircleParameter[Sequence2 + 1]
CircleRadius := CircleParameter[Sequence2 + 2]

2.3.6 get_metrology_object_measures
*獲取測量區域的輪廓和座標,這些座標是擬合圓和矩形的基礎
獲取測量點和矩形外觀
get_metrology_object_measures (Contour, MetrologyHandle, ‘all’, ‘all’, Row1, Column1)

  • get_metrology_object
    dev_clear_window()
    dev_set_draw(‘margin’)
    dev_display(Image)
    dev_display(Contour)

2.3.7顯示結果

在這裏插入圖片描述

*清除模型

  • Clean up memory
    clear_metrology_model (MetrologyHandle)

3.應用案列-實戰講解1
鑽石角度測量講解
通過形態學 找到基準。
*設置矩形框的寬度
Tolerance := 120

  • Create two metrology line objects and set parameters
    add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1)

  • Create two metrology line objects and set parameters
    add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1)

  • Create region of interest for the alignment
    gen_rectangle1 (Rectangle, LineRow1[0] - 40, LineColumn1[0] - 50, LineRow1[0] + 20, LineColumn1[0] + 50)

  • Change the reference coordinate system in which the

  • metrology model is given to be situated at the top of the diamond
    reduce_domain (Image, Rectangle, ImageReduced)
    threshold (ImageReduced, Region, 128, 255)
    *區域換算成點
    get_region_points (Region, Rows, Columns)
    *設置參考系統
    dev_set_color(‘red’)
    gen_cross_contour_xld(Cross4,Rows[0],Columns[0],16, 0.785398)

set_metrology_model_param (MetrologyHandle, ‘reference_system’, [Rows[0],Columns[0],0])

在這裏插入圖片描述

read_image (Image, 'diamond/diamond_' + I$'02')
* Roughly segment the diamond's position
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, Region, 128, 255)
* Extract the top of the diamond
get_region_points (Region, Rows, Columns)
* 
* Use the top of the diamond to align the metrology model in
* the current image
* 

align_metrology_model (MetrologyHandle, Rows[0], Columns[0], 0)

*找到測量的結果,求取曲線

在這裏插入圖片描述

4.案例講解2
測量衝壓件

  • Choose an alignment method by activating the desired line
    *通過選擇所需的模式來選擇一個校準方法
    AlignmentMode := ‘shape-based matching’

  • AlignmentMode := ‘region processing’

  • AlignmentMode := ‘rigid transformation’

  • Add several full circles
    *生成4個圓形

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)
*

  • Add two partial circles
    *生成2個圓弧
    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]

  • Add a retangle
    *生成1個矩形
    RectangleParam := [599,279,rad(90),62,51]
    add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices)

  • Add two lines
    *生成2條線
    Line1 := [143,1122,709,1132]
    Line2 := [151,153,136,1115]
    add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)

在這裏插入圖片描述

第2步 * 檢查添加到測量模型內的形狀
dev_clear_window()
get_metrology_object_model_contour (ModelContour, MetrologyHandle, ‘all’, 1.5)
get_metrology_object_measures (MeasureContour, MetrologyHandle, ‘all’, ‘all’, Row, Column)

第3步 顯示 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)

第4步 兩個算子之間的區別
*
dev_clear_window()
dev_display(Image)
get_metrology_object_measures (Contour, MetrologyHandle, ‘all’, ‘all’, MRow, MColumn)
gen_cross_contour_xld (Cross, MRow, MColumn, 6, rad(45))
stop()
* Get the edge points that were actually used to fit the
* geometric forms
*方法2 —獲取edges 上的點
dev_clear_window()
dev_display(Image)
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))、

在這裏插入圖片描述

方法1 —雜亂的點較多

方法2 —沒有干擾的點 (‘used_edges’)

第5步 獲取結果並且顯示
* Get a visualization of the measured geometric forms
*查詢計量對象的結果輪廓
get_metrology_object_result_contour (ResultContours, MetrologyHandle, ‘all’, ‘all’, 1.5)

*獲取結果和上面的初始設置 一一對應

*生成4個圓形
*前兩位是圓心的Row值和Column值,後一位是半徑
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)
*

  • Add two partial circles
    *生成2個圓弧
    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]

  • Add a retangle
    *生成1個矩形
    RectangleParam := [599,279,rad(90),62,51]
    add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices)

  • Add two lines
    *生成2條線
    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_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'radius', RadiusC1)
* Extract the radius of the larger incomplete circles
get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'radius', RadiusC2)
* Extract the lengths of the rectangle sides
get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length1', Length1R)
get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length2', Length2R)
* Get the start and end point of each measured line
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)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章