跟我學機器視覺-HALCON學習例程中文詳解-測量圓環腳寬間距
-
This example program demonstrates the basic usage of a circular measure object.
-
Here, the task is to determine the width of the cogs.
*首先讀取圖像,獲取圖像的寬度和高度
- First, read in the image and initialize the program.
read_image (Image, 'rings_and_nuts')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
get_image_size (Image, Width, Height)
*讀到的圖像如下:

- Extract the rings.
自動閾值分割,此時為一個區域,如圖*********
bin_threshold (Image, Region)

**********進行連通區域划分,不同的區域用不同的顏色表示,如圖:
connection (Region, ConnectedRegions)

*填充區域內的孔洞,效果如圖
fill_up (ConnectedRegions, RegionFillUp)

**計算區域的緊性,機器視覺算法與應用第168頁
compactness (RegionFillUp, Compactness)
****通過緊性特征進行區域篩選,在1.5到2.5之間的為需要測量的圓環
for i := 0 to |Compactness|-1 by 1
if (Compactness[i] > 1.5 and Compactness[i] < 2.5)
select_obj (RegionFillUp, ObjectSelected, i+1)
* Determine the size and position of the rings.
計算選擇區域的最小外接圓和最大內接圓,存儲圓心和半徑
兩個半徑的均值作為測量圓弧半徑,AnnulusRadius 為測量圓弧寬度******************
smallest_circle (ObjectSelected, Row, Column, RadiusMax)
inner_circle (ObjectSelected, CenterRow, CenterCol, RadiusMin)
Radius := (RadiusMax+RadiusMin)/2.0
AnnulusRadius := (RadiusMax-RadiusMin)/4.0
* Determine the position between two cogs.
* This position is then used as the start angle for the circular ROI.
********多邊形近似逼近區域,存儲多邊形角點的坐標值,機器視覺算法與應用第252頁
get_region_polygon (ObjectSelected, AnnulusRadius, RowsBorder, ColumnsBorder)
計算每個角點到最大內接圓圓心的距離,然后進行排序*************
SqrDistanceBorder := (RowsBorder-CenterRow)(RowsBorder-CenterRow) + (ColumnsBorder-CenterCol)(ColumnsBorder-CenterCol)
*************從小到大排序
tuple_sort_index (SqrDistanceBorder, Indices)
********計算直線的角度,圓心和到圓心最近的多邊形角點所確定的直線
***************將該角度作為測量圓弧的起始角度
line_orientation (CenterRow, CenterCol, RowsBorder[Indices[0]], ColumnsBorder[Indices[0]], AngleStart)
我認為檢測圓開始角度直接設為0度也可以**************
-
AngleStart := rad(0) AngleExtent := rad(360) * Create the measure for a circular ROI. Interpolation := 'bilinear'
生成測量圓弧,為完整圓環(360度)***
gen_measure_arc (CenterRow, CenterCol, Radius, AngleStart, AngleExtent, AnnulusRadius, Width, Height, Interpolation, MeasureHandle)
* Determine all edge pairs that have a negative transition, i.e., edge pairs
* that enclose dark regions.
* Note that the output parameters IntraDistance and InterDistance are given as arc lengths.
Sigma := 1.0
Threshold := 30
Transition := 'negative'
Select := 'all'
***進行邊緣對測量,RowEdgeFirst, ColumnEdgeFirst,RowEdgeSecond, ColumnEdgeSecond為檢測圓弧檢測到的邊界的中心點的坐標,
IntraDistance, InterDistance分別為邊緣對間的距離和邊緣對的弧線距離
measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
* Determine the number of cogs.
NumCogs := |RowEdgeFirst|
* Determine the linear distance between the two edges of each edge pair ('Linear cog size')
* as well as the angular distance of the edge pairs ('Angular cog size').
計算邊緣對的直線距離和弧度*
distance_pp (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, LinearDistance)
AngularDistance := deg(IntraDistance/Radius)
Visualize the determined edges.
將邊緣對的起始終止點畫出,並顯示測量結果****

gen_empty_obj (Crosses)
for i := 0 to |RowEdgeFirst|-1 by 1
gen_cross_contour_xld (Cross, RowEdgeFirst[i], ColumnEdgeFirst[i], AnnulusRadius*2.0, atan2(RowEdgeFirst[i]-CenterRow,ColumnEdgeFirst[i]-CenterCol))
concat_obj (Crosses, Cross, Crosses)
gen_cross_contour_xld (Cross, RowEdgeSecond[i], ColumnEdgeSecond[i], AnnulusRadius*2.0, atan2(RowEdgeSecond[i]-CenterRow,ColumnEdgeSecond[i]-CenterCol))
concat_obj (Crosses, Cross, Crosses)
endfor
dev_display (Image)
dev_set_line_width (4)
dev_set_color ('black')
dev_display (Crosses)
dev_set_line_width (1)
dev_set_color ('white')
dev_display (Crosses)
* Display the measured size of the cogs.
disp_message (WindowHandle, 'Number of cogs: '+|RowEdgeFirst|+' ', 'window', 260, 10, 'black', 'true')
disp_message (WindowHandle, 'Mean cog size: ', 'window', 286, 10, 'black', 'true')
disp_message (WindowHandle, '- Arc length: '+mean(IntraDistance)$'.2f'+' +/- '+deviation(IntraDistance)$'.2f'+' pixel', 'window', 310, 10, 'black', 'true')
disp_message (WindowHandle, '- Linear: '+mean(LinearDistance)$'.2f'+' +/- '+deviation(LinearDistance)$'.2f'+' pixel', 'window', 334, 10, 'black', 'true')
disp_message (WindowHandle, '- Angular: '+mean(AngularDistance)$'.2f'+' +/- '+deviation(AngularDistance)$'.2f'+' deg ', 'window', 358, 10, 'black', 'true')
*
* Close the measure
close_measure (MeasureHandle)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
for循環,測量其他圓環*****
endfor
