天天看點

Halcon卡尺測量

測量卡尺的使用

* 擷取圖像大小
get_image_size (Image, Width, Height)
*** 建立測量卡尺1
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, 'bilinear', MeasureHandle)
*** 卡尺1進行卡點,擷取點
measure_pos (Image, MeasureHandle, 1, 30, 'all', 'first', RowEdge1, ColumnEdge1, Amplitude, Distance)      

示例

******************************************************************
************************* 卡尺的使用 *********************
******************************************************************
******************************************************************
************************* 工業視覺應用一般流程 *********************
******************************************************************
 dev_get_window (WindowHandle)
 dev_set_draw ('margin')
 
****************************** 1.讀取圖像 ******************************
read_image (Image, 'fabrik')

****************************** 2.定位 *********************************
draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle1, Row, Column, Phi, Length1, Length2)
draw_rectangle2 (WindowHandle, Row1, Column1, Phi1, Length11, Length21)
gen_rectangle2 (Rectangle2, Row1, Column1, Phi1, Length11, Length21)
****************************** 3.擷取ROI(感興趣)區域 ******************
****************************** 4.圖像預處理 *****************************
****************************** 5.圖像算法處理 ***************************
* 擷取圖像大小
get_image_size (Image, Width, Height)
*** 建立測量卡尺1
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, 'bilinear', MeasureHandle)
*** 卡尺1進行卡點,擷取點
measure_pos (Image, MeasureHandle, 1, 30, 'all', 'first', RowEdge1, ColumnEdge1, Amplitude, Distance)


*** 建立測量卡尺2
gen_measure_rectangle2 (Row1, Column1, Phi1, Length11, Length21, Width, Height, 'bilinear', MeasureHandle)
*** 卡尺2進行卡點,擷取點
measure_pos (Image, MeasureHandle, 1, 30, 'all', 'first', RowEdge2, ColumnEdge2, Amplitude, Distance)
****************************** 6.結果輸出 *******************************
disp_line (WindowHandle, RowEdge1[0], ColumnEdge1[0], RowEdge2[0], ColumnEdge2[0])      

1.确定自己先自己設定的區域邊緣輪廓

2.沿着邊緣輪廓進行卡尺計算

3.最後得到真實輪廓邊緣

***************Step1 建立測量句柄*****************************
* 建立測量幾何形狀所需的資料結構
create_metrology_model (MetrologyHandle)
* 讀取圖像
read_image (Image, 'fabrik')
* 擷取圖像大小
get_image_size (Image, Width, Height)
***************Step2 設定計量對象圖像的大小*****************************
* 設定計量對象圖像的大小
set_metrology_model_image_size (MetrologyHandle, Width, Height)
***************Step3 參數設定*****************************
LinePar := [45,360,415,360]
RectPar1 := [270,232,rad(0),30,25]
RectPar2 := [360,230,rad(0),30,25]
LinePar := [45,360,415,360]
RectPar3 := [245,320,rad(-90),70,35]
***************Step4 将計量對象添加到計量模型*****************************
* 将計量對象添加到計量模型
* Add two rectangles
add_metrology_object_generic (MetrologyHandle, 'rectangle2', [RectPar1,RectPar2], \
                              20, 5, 1, 30, [], [], Indices)
* Add a rectangle and a line
add_metrology_object_generic (MetrologyHandle, ['rectangle2','line'], \
                             [RectPar3,LinePar], 20, 5, 1, 30, [], [], Index)
***************Step5 先定輪廓,并對固定輪廓進行卡尺測量,最後得到實際的輪廓對象*****************************
* 在圖像坐标中查詢計量對象的模型輪廓。
get_metrology_object_model_contour (Contour, MetrologyHandle, 'all', 1.5)
* 測量并拟合計量模型中所有計量對象的幾何形狀。
apply_metrology_model (Image, MetrologyHandle)
* 查詢計量對象的結果輪廓
get_metrology_object_result_contour (Contour1, MetrologyHandle, 'all', \
                                    'all', 1.5)
***************Step6 清除模型句柄 *****************************
clear_metrology_model (MetrologyHandle)      
* 在圖像坐标中查詢計量對象的模型輪廓。
get_metrology_object_model_contour (Contour, MetrologyHandle, 'all', 1.5)      
Halcon卡尺測量
* 測量并拟合計量模型中所有計量對象的幾何形狀。
apply_metrology_model (Image, MetrologyHandle)
* 查詢計量對象的結果輪廓
get_metrology_object_result_contour (Contour1, MetrologyHandle, 'all',      
Halcon卡尺測量
read_image (Image15, 'C:/Users/29092/Desktop/Test Images/15.bmp')
* read_image (Image15, 'C:/Users/29092/Documents/WXWork/1688853866237112/Cache/File/2021-11/27.bmp')
* read_image (Image16, 'C:/Users/29092/Documents/WXWork/1688853866237112/Cache/File/2021-11/17.bmp')

get_image_size (Image15, Width, Height)

r := []
c := []
row:= 750
column := 80

gen_measure_rectangle2 (row, column, rad(90), 100, 10, Width, Height, 'bilinear', MeasureHandle)
  
for Index := 10 to Width -200 by 10
    
    gen_rectangle2 (Rectangle, row, column, rad(90), 100, 10)
    measure_pos (Image15, MeasureHandle, 1, 30, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
 
    r:= [r,RowEdge]
    c :=[c,ColumnEdge]
    column := column +10
    translate_measure (MeasureHandle, row, column)
               
endfor

gen_cross_contour_xld (Cross, r, c, 60, 0.785398)
gen_contour_polygon_xld (Contour, r, c)
fit_line_contour_xld (Contour, 'tukey', -1, 0, 10, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)

* gen_region_line (RegionLines, RowBegin, ColBegin, RowEnd, ColEnd)
* gen_contour_polygon_xld (Contour1, [RowBegin,RowEnd], [ColBegin,ColEnd])

intersection_lines (RowBegin, ColBegin, RowEnd, ColEnd, RowBegin, 0, RowBegin + 500, 0, Row, Column, IsOverlapping)
gen_cross_contour_xld (Cross1, Row, Column, 60, 0.785398)

intersection_lines (RowBegin, ColBegin, RowEnd, ColEnd, RowEnd, Width, RowEnd + 500, Width, Row1, Column1, IsOverlapping1)
gen_cross_contour_xld (Cross2, Row1, Column1, 60, 0.785398)

gen_contour_polygon_xld (Contour1, [RowBegin,RowEnd], [ColBegin,ColEnd])


gen_region_polygon (Region, [RowBegin + 500, RowBegin, RowEnd, RowEnd + 500], [0,0,Width-1,Width-1])
shape_trans (Region, RegionTrans, 'convex')

reduce_domain (Image15, RegionTrans, ImageReduced)
threshold (ImageReduced, Region1, 128, 255)

dev_display (Image15)
dev_display (Region1)      
dev_get_window (WindowHandle)

* list_files ('E:/CG項目/圖像資料/Chipping ARR 1101-NG/Chipping ARR 1101-NG', 'directories', directories)
* 擷取第一級目錄下所有檔案夾
list_files ('20211109', 'directories', directories)

* 周遊第一級所有檔案夾
for Index1 := 0 to |directories|-1 by 1
    * 擷取目前檔案夾下所有檔案夾
    list_files (directories[Index1], 'directories', directories2)
    * 周遊第二級目前檔案夾
    for Index2 := 0 to |directories2|-1 by 1
        * 分割目前檔案夾路徑
        tuple_split ( directories2[Index2], '\\', Substrings)     
        * 擷取分割後字元串數組長度
        tuple_length (Substrings, SubstringsLen)
        * 擷取目錄(檔案夾)名
        tuple_split (Substrings[SubstringsLen-1], '', FileName)
        * parse_filename(directories2[Index2], BaseName, Extension, Directory)
        ***********************************************UpperNG*****************************************************
        * 如果目錄(檔案夾) 等于 ''UnderNG''
        if(FileName =='UnderNG')
            
            list_files (directories2[Index2], 'files', Files)
            for Index := 0 to |Files|-1 by 1
                parse_filename(Files[Index], BaseName, Extension, Directory)
                if(BaseName =='9' or BaseName =='16' or BaseName =='25' or BaseName =='32' )

                read_image (Image15, Files[Index])

                        ********* 一 定位到邊********* 
                        
                        * 1. 最小外接矩形,的角度
                        * 2. 全局特征是否等于-1 ,來判斷是否是直線
                        get_image_size (Image15, Width, Height)
                        * 擷取邊緣
                        gen_rectangle1 (Rectangle1, 0, 0, Width, Height)
                        reduce_domain (Image15, Rectangle1, ImageReduced1)
                        
                        threshold (ImageReduced1, Region1, 128, 255)
                        
                        gen_contour_region_xld (Region1, Contours, 'border')
                        
                        segment_contours_xld (Contours, ContoursSplit, 'lines_circles', 5, 4, 2)
                        
                        select_contours_xld (ContoursSplit, SelectedContours1, 'curvature', 1, 2, -0.5, 0.5)
                        
*                         edges_color_sub_pix (ImageReduced1, Edges, 'canny', 1, 20, 40)
                        
*                         union_adjacent_contours_xld (Edges, UnionContours, 10, 1, 'attr_keep')
                        
                        
                        * 選擇邊緣
                        select_contours_xld (Edges, SelectedContours, 'contour_length',300, 99999999, -0.5, 0.5)
                        * 擷取邊緣中心點
                        area_center_xld (SelectedContours, Area1, Row1, Column1, PointOrder)
                        
                        
                        ********* 二 卡尺掃描********* 
                        r := []
                        c := []
                        row:= Row1[0]
                        column := 0
                        
                        * 生成測量卡尺
                        gen_measure_rectangle2 (row, column, rad(45), 50, 10, Width, Height, 'bilinear', MeasureHandle)
                        
                        * 測量所有的點
                        for Index := 10 to Width+20 by 10
                        * 生成矩形框
                        gen_rectangle2 (Rectangle, row, column, rad(90), 50, 10)
                        * 測量
                        measure_pos (Image15, MeasureHandle, 1, 30, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
                        * 記錄測量點
                        r:= [r,RowEdge]
                        c :=[c,ColumnEdge]
                        * 前進步幅
                        column := column +10
                        * 再次測量
                        translate_measure (MeasureHandle, row, column)             
                        endfor
                        
                        
                        left_down := [Height/2,0]
                        right_down := [Height/2,Width]
                        
                        rrr := [Height/2,r]
                        ccc := [0,c] 
                        
                        rrr := [rrr,Height/2]
                        ccc := [ccc,Width] 
                        
                        rrr := [rrr,Height/2]
                        ccc := [ccc,0] 
                        
                        
                        ********* 三 構成多邊形ROI區域********* 
                        
                        gen_region_polygon (Region, rrr, ccc)
                else
                       read_image (Image15, Files[Index])

                        ********* 一 定位到邊********* 
                        
                        get_image_size (Image15, Width, Height)
                        * 擷取邊緣
                        gen_rectangle1 (Rectangle1, 0, 0, Width/3, Height)
                        reduce_domain (Image15, Rectangle1, ImageReduced1)
                        
                        edges_color_sub_pix (ImageReduced1, Edges, 'canny', 1, 20, 40)
                        * 選擇邊緣
                        select_contours_xld (Edges, SelectedContours, 'contour_length',300, 99999999, -0.5, 0.5)
                        * 擷取邊緣中心點
                        area_center_xld (SelectedContours, Area1, Row1, Column1, PointOrder)
                        
                        
                        ********* 二 卡尺掃描********* 
                        r := []
                        c := []
                        row:= Row1[0]
                        column := 0
                        
                        * 生成測量卡尺
                      
                        
                        gen_measure_rectangle2 (row, column, rad(90), 50, 10, Width, Height, 'bilinear', MeasureHandle)
                        
                        * 測量所有的點
                        for Index := 10 to Width+20 by 10
                        * 生成矩形框
                        gen_rectangle2 (Rectangle, row, column, rad(90), 50, 10)
                        * 測量
                        measure_pos (Image15, MeasureHandle, 1, 30, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
                        * 記錄測量點
                        r:= [r,RowEdge]
                        c :=[c,ColumnEdge]
                        * 前進步幅
                        column := column +10
                        * 再次測量
                        translate_measure (MeasureHandle, row, column)             
                        endfor
                        
                        
                        left_down := [Height/2,0]
                        right_down := [Height/2,Width]
                        
                        rrr := [Height/2,r]
                        ccc := [0,c] 
                        
                        rrr := [rrr,Height/2]
                        ccc := [ccc,Width] 
                        
                        rrr := [rrr,Height/2]
                        ccc := [ccc,0] 
                        
                        
                        ********* 三 構成多邊形ROI區域********* 
                        
                        gen_region_polygon (Region, rrr, ccc)
*                         stop()      

繼續閱讀