[機器視覺]halcon應用例項 邊緣檢測

baivfhpwxf發表於2024-06-20

一個學習找邊的例項

邊緣檢測的步驟圖解


步驟
1.透過Blob方法獲取需要測量的Region

1.1 主要運用影像形態學、二值化

2.建立測量控制代碼

2.1 create_metrology_model (MetrologyHandle)

3.設定目標影像大小

3.1 set_metrology_model_image_size (MetrologyHandle, Width, Height)

4.新增測量物件

4.1 (通用運算元) 預設值:‘circle’,參考值:‘circle’圓,‘ellipse’橢圓,‘line’線,‘rectangle2’矩形,長方形;

add_metrology_object_generic (MetrologyHandle, 'line', LineParam, 20, 5, 1, 30, [], [], Index)

4.2 *circle 圓形工具
add_metrology_object_circle_measure (MetrologyHandle, Row1, Column1, Row1, 20, 5, 1, 30, [], [], Index1)

4.3 circle 橢圓工具
add_metrology_object_ellipse_measure (MetrologyHandle, Row1, Column1, Index1, Index1, Index1, 20, 5, 1, 30, [], [], Index2)

4.4 rectangle 矩形工具
add_metrology_object_rectangle2_measure (MetrologyHandle, Row1, Column1, Index3, Index3, Index3, 20, 5, 1, 30, [], [], Index4)

4.5 line 直線工具

add_metrology_object_line_measure (MetrologyHandle, Row1, Column1, Row2, Column2, 20, 5, 1, 30, [], [], Index)

5.設定測量引數

5.1 *設定找線的方式(positive,negative,all)從黑到白,從白到黑
set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'all')

5.2 *預期測量的區域個數
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 12)

5.3*擬合數
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 6)

5.4*高斯平滑係數,值越大,唯一的邊緣越清晰
set_metrology_object_param (MetrologyHandle, 'all', 'measure_sigma', 5)

5.5 *區域,垂直與邊界的測量區域的一半長度
set_metrology_object_param (MetrologyHandle, 'all', 'measure_length1', 80)

5.6 *區域,相切於邊緣的測量區域的一半長度
set_metrology_object_param(MetrologyHandle, 'all', 'measure_length2', 10)

5.7 *最小邊緣幅度越大,要求找到的邊緣越銳利(灰度變換明顯),反而不容易找到邊緣
set_metrology_object_param (MetrologyHandle, 'all', 'measure_threshold', 20)

5.8 *測量雙立方插入值,區別與bilinear雙線性
set_metrology_object_param (MetrologyHandle, 'all', 'measure_interpolation', 'bicubic')

5.9 *取值 all,first,best,last
set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'all')

5.10 *設定最小分數0.7
set_metrology_object_param (MetrologyHandle, 'all', 'min_score', 0.7)

6.執行測量,獲取邊緣點集

7.獲取最終測量資料和輪廓

8.釋放測量控制代碼

程式碼

dev_get_window (WindowHandle)
*讀取影像
read_image (Image, 'printer_chip/printer_chip_01')
 
*獲取影像大小
get_image_size (Image, Width, Height)
 
*方式一繪製測量位置直線
*draw_line (WindowHandle, Row1, Column1, Row2, Column2)
 
*方式二 固定測量位置直線座標
Row1 := 19.9
Column1 := 810
Row2 := 271.9
Column2 := 808.5
 
*根據繪製的直線座標來構建引數物件
LineParam := [Row1, Column1,Row2, Column2]
 
*繪製測量位置直線 時把直線的座標寫入文字檔案,以變檢視。這樣就可以知道繪製的座標。
*實際測量時是不可能讓人,手動的去繪製每張要測量的圖片的
open_file ('LineParam.txt', 'output', FileHandle)
fileStr := '線的座標Row1:'+Row1+',Column1:'+Column1+',Row2:'+Row2+',Column2:'+Column2
fwrite_string (FileHandle, fileStr)
disp_message (WindowHandle, fileStr, 'window', 0, 0, 'red', 'true')
 
*建立計算測量控制代碼
create_metrology_model (MetrologyHandle)
 
*控制代碼關聯測試的圖座標位置
*設定測量物件影像的大小
*引數:
*    MeasureHandle:輸入測量模型的控制代碼;
*    Width:輸出影像寬;
*    Height:輸出影像高;
set_metrology_model_image_size (MetrologyHandle, Width, Height)
 
*新增測量模型物件(此處是線)
*將測量物件新增到測量模型中
*運算元引數:
*    MeasureHandle:輸入測量模型的控制代碼;
*    Shape:輸入要測量物件的型別;預設值:‘circle’,參考值:‘circle’圓,‘ellipse’橢圓,‘line’線,‘rectangle2’矩形,長方形;
*    ShapeParam:要新增的測量物件的引數。
*    MeasureLength1:輸入垂直於邊界的測量區域的一半長度;預設值:20,參考值:102030;最小增量:1.0;建議增量:10.0
*    MeasureLength2:輸入與邊界相切的測量區域的一半長度;預設值:5,參考值:3510;最小增量:1.0;建議增量:10.0
*    MeasureSigma:輸入用於平滑的高斯函式的sigma;預設值:1,參考值:0.40.60.81.01.52.03.04.05.07.010.0;最小增量:0.01;建議增量:0.1;限制:(0.4 <= MeasureSigma)&&(MeasureSigma <= 100*    MeasureThreshold:輸入測量閾值/最小邊緣幅度;預設值:30,參考值:5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 90.0, 110.0;最小增量:0.5;建議增量:2*    GenParamName:輸入引數名稱;參考: ‘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’;
*    GenParamValue:輸入引數值;參考:1, 2, 3, 4, 5, 10, 20, ‘all’, ‘true’, ‘false’, ‘first’, ‘last’, ‘positive’, ‘negative’, ‘uniform’, ‘nearest_neighbor’, ‘bilinear’, ‘bicubic’;
*    Index:輸出建立測量物件的索引值;
add_metrology_object_generic (MetrologyHandle, 'line', LineParam, 20, 5, 1, 30, [], [], Index)
 
*為測量模型的測量物件設定引數 set_metrology_object_param
*根據需求設定測量的引數
*設定找線的方式(positive,negative,all)從黑到白,從白到黑
*運算元引數:
*    MeasureHandle:輸入測量模型的控制代碼;
*    Index:指定測量物件,為all時所有計量物件的引數都被設定(‘all’);
*    GenParamName:輸入引數名稱;預設:‘num_instances’,參考:
*            'measure_length1':區域,垂直於邊界的測量區域的一半長度
*            'measure_length2':區域,相切於邊界的測量區域的一半長度
*            'measure_distance':區域,兩個測量區域中心之間的距離
*            'num_measures':區域,測量區域數
*            'measure_sigma':測量,用於平滑的高斯函式的 Sigma
*            'measure_threshold':測量,最小邊緣幅度閾值
*            'measure_select':測量,邊緣端點的選擇('last''first')
*            'measure_transition':測量,方向('all''negative''positive')
*            'measure_interpolation':測量,插值型別
*            'min_score':擬合,最小分數
*            'num_instances':擬合,成功擬合例項的最大數量
*            'distance_threshold':擬合,距離閾值 
 
set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'all')
 
*預期測量的區域個數
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 12)
 
*擬合數
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 6)
 
*高斯平滑係數,值越大,唯一的邊緣越清晰
set_metrology_object_param (MetrologyHandle, 'all', 'measure_sigma', 5)
 
*區域,垂直與邊界的測量區域的一半長度
set_metrology_object_param (MetrologyHandle, 'all', 'measure_length1', 80)
 
*區域,相切於邊緣的測量區域的一半長度
set_metrology_object_param(MetrologyHandle, 'all', 'measure_length2', 10)
 
*最小邊緣幅度越大,要求找到的邊緣越銳利(灰度變換明顯),反而不容易找到邊緣
set_metrology_object_param (MetrologyHandle, 'all', 'measure_threshold', 20)
 
*測量雙立方插入值,區別與bilinear雙線性
set_metrology_object_param (MetrologyHandle, 'all', 'measure_interpolation', 'bicubic')
 
*取值 all,first,best,last
set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'all')
 
*設定最小分數0.7
set_metrology_object_param (MetrologyHandle, 'all', 'min_score', 0.7)
 
*開始找邊緣
apply_metrology_model (Image, MetrologyHandle)
 
*獲取測量區域找到的邊緣座標集合
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row, Column)
 
*把找到的點透過十字叉來顯示
gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
 
*得到線的起點座標並顯示出來
get_metrology_object_result (MetrologyHandle, 0, 'all', 'result_type', 'all_param', Parameter)
 
*獲取計量模型的計量結果輪廓
get_metrology_object_result_contour (Contour, MetrologyHandle, 0, 'all', 1.5)
*取第一條邊放到ObjectSelected變數中
select_obj (Contour, ObjectSelected, 1)
*取第二條邊放到ObjectSelected1變數中
select_obj (Contour, ObjectSelected1, 2)
 
*測量兩個輪廓之間的距離
distance_cc (ObjectSelected, ObjectSelected1, 'point_to_point', DistanceMin, DistanceMax)
disp_message (WindowHandle, 'length:'+DistanceMin, 'window', 20, 0, 'green', 'true')
*清除控制代碼
clear_metrology_model (MetrologyHandle)

結果


部分引數說明
*line 直線工具
*新增直線測量物件到模型
*函式引數:
*    MeasureHandle:輸入測量模型的控制代碼;
*    RowBegin:輸入測量區域行座標起點;(測量直線的第一個點X座標)
*    ColumnBegin:輸入測量區域列座標起點;(測量直線的第一個點Y座標)
*    RowEnd:輸入測量區域行座標終點;(測量直線的第二個點X座標)
*    ColumnEnd:輸入測量區域列座標終點;(測量直線的第二個點Y座標)
*    MeasureLength1:輸入垂直於邊界的測量區域的一半長度;預設值:20,參考值:10,20,30;
*    MeasureLength2:輸入與邊界相切的測量區域的一半長度;預設值:5,參考值:3,5,10;
*    MeasureSigma:輸入用於平滑的高斯函式的sigma;預設值:1,參考值:0.4,0.6,0.8,1.0,1.5,2.0,3.0,4.0,5.0,7.0,10.0;
*    MeasureThreshold:輸入測量閾值;預設值:30,參考值:5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 90.0, 110.0;
*    GenParamName:輸入引數名稱;參考: ‘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’;
*    GenParamValue:輸入引數值;參考:1, 2, 3, 4, 5, 10, 20, ‘all’, ‘true’, ‘false’, ‘first’, ‘last’, ‘positive’, ‘negative’, ‘uniform’, ‘nearest_neighbor’, ‘bilinear’, ‘bicubic’;
*    Index:輸出建立測量物件的索引值;
add_metrology_object_line_measure (MetrologyHandle, Row1, Column1, Row2, Column2, 20, 5, 1, 30, [], [], Index)

————————————————

版權宣告:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結和本宣告。

原文連結:https://blog.csdn.net/cjh16606260986/article/details/136291046

相關文章