Halcon 一維測量開關引腳

基於機器視覺的測量原理

基於機器視覺的檢測過程:對感興趣對象或區域進行成像,然後根據其圖像信息用圖像處理軟件進行處理,根據處理結果自動判斷檢測對象的位置、尺寸、外觀信息,並依據人爲預先設定的標準進行合格與否的判斷,輸出其判斷信息給執行機構。
機器視覺檢測系統採用CCD相機或CMOS相機將被檢測的對象信息轉換成圖像信號,傳送給專用的圖像處理系統,根據像素分佈和亮度、顏色等信息,轉變成數字化信號,圖像處理系統再對這些信號進行各種運算來抽取對象的特徵,如面積、數量、位置、長度,再根據預設的值和其他條件輸出結果,包括尺寸、角度、個數、合格/不合格、有/無等,實現自動識別功能。

本例程最終效果如圖所示:
在這裏插入圖片描述
任務拆分:

圖像顯示
read_image (Image, 'bin_switch/bin_switch_2')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_resize_window_fit_size (0, 0, Width, Height, -1, 640)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)

在這裏插入圖片描述

顯示文字信息
disp_message (WindowHandle, 'Fuzzy measure results', 'window', 12, 12, 'black', 'true')

在這裏插入圖片描述
在這裏插入圖片描述

程序窗口

* 開關引腳測量
* 打開圖片,初始化程序
read_image (Image, 'bin_switch/bin_switch_2')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_resize_window_fit_size (0, 0, Width, Height, -1, 640)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)


*定義將在其中檢測邊緣的矩形ROI,並開始測量。
Row := 290
Column := 170
Phi := rad(-130)
Length1 := 60
Length2 := 10
Interpolation := 'nearest_neighbor'
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.1
Threshold := 20
Transition := 'negative'
Select := 'all'
measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
* 結果可視化
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('black')
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
p_disp_dimensions (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, IntraDistance, InterDistance, Phi, Length2, WindowHandle)

*顯示文字信息
disp_message (WindowHandle, 'Standard measure results', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Due to the reflections on the middle pin, its width cannot be determined correctly.
* 
* As we know that the pins appear approximately 9 pixels wide, it is very easy to expand the measure to a
* fuzzy measure that will only return pairs that have a width of approximately the given size.
* 
* First, create a fuzzy function that returns 1.0 for the given pair size and 0.0 for values that
* deviate more than 2 pixels from the given pair size.
create_funct_1d_pairs ([7,9,11], [0.0,1.0,0.0], SizeFunction)
* 
* Then, expand the measure to a fuzzy measure that will return only pairs of approximately the given size.
SetType := 'size'
set_fuzzy_measure (MeasureHandle, SetType, SizeFunction)
* 
* Finally, determine all edge pairs that have a negative transition and approximately the given size.
Sigma := 1.1
AmpThresh := 20
FuzzyThresh := 0.5
Transition := 'negative'
fuzzy_measure_pairs (Image, MeasureHandle, Sigma, AmpThresh, FuzzyThresh, Transition, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, RowEdgeCenter, ColumnEdgeCenter, FuzzyScore, IntraDistance, InterDistance)
* 結果可視化
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('black')
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
p_disp_dimensions (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, IntraDistance, InterDistance, Phi, Length2, WindowHandle)
*顯示文字信息Fuzzy measure results
disp_message (WindowHandle, 'Fuzzy measure results', 'window', 12, 12, 'black', 'true')
* 釋放已爲該度量分配的內存。
close_measure (MeasureHandle)

算子回顧

gen_measure_rectangle2:通過一個矩形創建一個線性測量對象

Row, Column: 直剖面線的中心
Phi:直線剖面的方向(in radians, counterclockwise to positive x-axis)
Length1:剖面線長度的一半(in sub- pixels)
Length2:投影區域高度的一半(in sub- pixels)
width, Height: 需要處理的圖像的範圍
Interpolation:插值方法
MeasureHandle:測量對象的句柄

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