Halcon算子measure_pos和measure_pairs经典案例

不废话,直接上代码。

*测量引线的长度,然后测量它们之间的宽度和距离
*测量引线的个数
read_image (Image, 'ic_pin')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 509, 509, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Image)
dev_set_draw ('margin')
dev_set_line_width (3)
* 
* 定义检测边缘的矩形roi参数
Row := 55
RowBottom := 955
Column := 200
*角度-90度,代表的是以水平线顺时针旋转90度。这样子卡尺搜索方向刚好是从上到下,与水平轴夹角90度
Phi := rad(-90)
Length1 := 50
Length2 := 35
dev_set_color ('gray')
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, RowBottom, Column, Phi, Length1, Length2)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
*为图像顶部的ROI创建一个测量对象。
Interpolation := 'nearest_neighbor'
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation, MeasureHandle)
* 
* measure_pos测量参数赋值
Sigma := 1.0
Threshold := 30
Transition := 'all'
Select := 'all'
measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance)
LeadLength1 := Distance
* 
* Display the results
dev_set_color ('white')
disp_line (WindowHandle, RowEdge, ColumnEdge - Length2, RowEdge, ColumnEdge + Length2)
disp_message (WindowHandle, 'Lead Length:  ' + LeadLength1$'.2f', 'window', RowEdge[1] + 40, ColumnEdge[1] + 100, 'yellow', 'false')
* 
* 转换测量模型的中心点到图像底部Roi(RowBottom, Column)
translate_measure (MeasureHandle, RowBottom, Column)
measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance)
LeadLength2 := Distance
* 
* Display the results
dev_set_color ('white')
disp_line (WindowHandle, RowEdge, ColumnEdge - Length2, RowEdge, ColumnEdge + Length2)
disp_message (WindowHandle, 'Lead Length:  ' + LeadLength2$'.2f', 'window', RowEdge[1] - 120, ColumnEdge[1] + 100, 'yellow', 'false')
* 
* Close the measure
close_measure (MeasureHandle)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* 现在,定义并显示垂直于引线的矩形ROI
dev_display (Image)
Row := 47
Column := 485
*角度0度,代表的是卡尺搜索方向刚好是从左到右,与水平轴夹角0度
Phi := 0
Length1 := 420
Length2 := 8
Interpolation := 'nearest_neighbor'
dev_set_color ('black')
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation, MeasureHandle)
* 
* Determine all edge pairs that have a negative transition, i.e., edge pairs
* that enclose dark regions.
Sigma := 1.0
Threshold := 30
Transition := 'negative'
Select := 'all'
*RowEdgeFirst,ColumnEdgeFirst是negative模式下,同一对卡尺找到的白到黑的边。RowEdgeSecond,ColumnEdgeSecond则是同一对卡尺找到的黑到白的边
*当Transition为all时,RowEdgeFirst是同一对卡尺找到的第一条边,不分极性。RowEdgeSecond是同一对卡尺找到的第二条边,不分极性。
*AmplitudeFirst,AmplitudeSecond是边缘振幅
*IntraDistance同一对卡尺对中间的距离。InterDistance是相邻两对卡尺对之间的边距离。所以IntraDistance个数比InterDistance多1
measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_set_color ('white')
disp_line (WindowHandle, RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond)
avgLeadWidth := sum(IntraDistance) / |IntraDistance|
avgLeadDistance := sum(InterDistance) / |InterDistance|
numLeads := |IntraDistance|
disp_message (WindowHandle, 'Number of Leads: ' + numLeads, 'window', 200, 100, 'yellow', 'false')
disp_message (WindowHandle, 'Average Lead Width:  ' + avgLeadWidth$'.2f', 'window', 260, 100, 'yellow', 'false')
disp_message (WindowHandle, 'Average Lead Distance:  ' + avgLeadDistance$'.2f', 'window', 320, 100, 'yellow', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
*放大以更详细地显示测量结果。
*首先,定义并显示缩放窗口。
Row1 := 0
Column1 := 600
Row2 := 100
Column2 := 700
dev_set_color ('blue')
disp_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Then, zoom the image and display the results.
dev_set_part (Row1, Column1, Row2, Column2)
dev_display (Image)
dev_set_color ('black')
dev_display (Rectangle)
p_disp_edge_marker (RowEdgeFirst, ColumnEdgeFirst, Phi, 5, 'white', 3, WindowHandle)
p_disp_edge_marker (RowEdgeSecond, ColumnEdgeSecond, Phi, 5, 'white', 3, WindowHandle)
* 
* Close the measure
close_measure (MeasureHandle)
* 
* Reset the display parameters.
dev_set_part (0, 0, Height - 1, Width - 1)
dev_set_draw ('fill')
dev_set_line_width (1)

原图
Halcon算子measure_pos和measure_pairs经典案例_第1张图片
过程图
Halcon算子measure_pos和measure_pairs经典案例_第2张图片
Halcon算子measure_pos和measure_pairs经典案例_第3张图片
Halcon算子measure_pos和measure_pairs经典案例_第4张图片
Halcon算子measure_pos和measure_pairs经典案例_第5张图片

你可能感兴趣的:(计算机视觉,人工智能)