HALCON示例程序measure_ring.hdev齒輪齒寬度測量
示例程序源碼(加注釋)
- 關于顯示類函數解釋
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) - 使用灰度直方圖分割圖像
binary_threshold (Image, Region, ‘max_separability’, ‘dark’, UsedThreshold)
connection (Region, ConnectedRegions)
fill_up (ConnectedRegions, RegionFillUp) - 返回區域緊湊度
compactness (RegionFillUp, Compactness)
for i := 0 to |Compactness| - 1 by 1
if (Compactness[i] > 1.5 and Compactness[i] < 2.5)
select_obj (RegionFillUp, ObjectSelected, i + 1)
* 最小外接圓
smallest_circle (ObjectSelected, Row, Column, RadiusMax)
* 最大內接圓
inner_circle (ObjectSelected, CenterRow, CenterCol, RadiusMin)
Radius := (RadiusMax + RadiusMin) / 2.0
AnnulusRadius := (RadiusMax - RadiusMin) / 4.0
* 得到區域輪廓的多邊形
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)
AngleExtent := rad(360)
Interpolation := ‘bilinear’
* 準備提取垂直于環形弧的直邊。
gen_measure_arc (CenterRow, CenterCol, Radius, AngleStart, AngleExtent, AnnulusRadius, Width, Height, Interpolation, MeasureHandle)
Sigma := 1.0
Threshold := 30
Transition := ‘negative’
Select := ‘all’
* 提取垂直于矩形或環形弧的直邊對。
measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
distance_pp (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, LinearDistance)
AngularDistance := deg(IntraDistance / Radius)
gen_empty_obj (Crosses)
for J := 0 to |RowEdgeFirst| - 1 by 1
gen_cross_contour_xld (Cross1, RowEdgeFirst[J], ColumnEdgeFirst[J], 12, atan2(CenterRow - RowEdgeFirst[J],ColumnEdgeFirst[J] - CenterCol))
gen_cross_contour_xld (Cross2, RowEdgeSecond[J], ColumnEdgeSecond[J], 12, atan2(CenterRow - RowEdgeSecond[J],ColumnEdgeSecond[J] - CenterCol))
concat_obj (Crosses, Cross1, Crosses)
concat_obj (Crosses, Cross2, Crosses)
endfor
dev_display (Image)
dev_set_line_width (2)
dev_set_color (‘green’)
dev_display (Crosses)
Message := 'Number of cogs: ' + |RowEdgeFirst| Message[1] := 'Mean cog size:' Message[2] := '- Arc length: ' + mean(IntraDistance)$'.2f' + ' +/- ' + deviation(IntraDistance)$'.2f' + ' pixel' Message[3] := '- Linear: ' + mean(LinearDistance)$'.2f' + ' +/- ' + deviation(LinearDistance)$'.2f' + ' pixel' Message[4] := '- Angular: ' + mean(AngularDistance)$'.2f' + ' +/- ' + deviation(AngularDistance)$'.2f' + ' deg' disp_message (WindowHandle, Message, 'window', 260, 10, 'black', 'true')
close_measure (MeasureHandle)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
處理思路
這個例子是主要講解了如何使用gen_measure_arc、measure_pairs 搭配提取垂直圓弧的直邊。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。