Halcon自定義函數:get_measure_positions ()標註/3D_coordinates.hdev

 get_measure_positions (Image, PlateRegion, CalibDataID, I, Distance, Phi, RowCenter, ColumnCenter)

* 提取特徵
threshold (Image, Region, 0, 120)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['holes_num','rect2_len1','rect2_len2'], 'and', [1,120,120], [1,200,200])
fill_up (SelectedRegions, PlateRegion)
*從表的邊框構造線條
gen_contour_region_xld (PlateRegion, Contours, 'center') //畫輪廓
segment_contours_xld (Contours, ContoursSplit, 'lines', 7, 4, 2)//分割輪廓
regress_contours_xld (ContoursSplit, RegressContours, 'no', 1)//計算迴歸(後一步的先決條件,下一步包含direction  or currature)
select_contours_xld (RegressContours, VerticalContours, 'direction', rad(45), rad(135), -0.5, 0.5)   //過濾輪廓
select_contours_xld (VerticalContours, LongContours, 'length', 150, 500, -0.5, 0.5)
* 獲取兩條線段的端點
select_obj (LongContours, Contour, 1)//選第一條線
get_contour_xld (Contour, Rows, Columns)
RowBegin1 := Rows[0] //兩端點
ColBegin1 := Columns[0]
RowEnd1 := Rows[|Rows| - 1]
ColEnd1 := Columns[|Columns| - 1]
select_obj (LongContours, Contour, 2)//第二條線
get_contour_xld (Contour, Rows, Columns)
RowBegin2 := Rows[0]
ColBegin2 := Columns[0]
RowEnd2 := Rows[|Rows| - 1]
ColEnd2 := Columns[|Columns| - 1]
get_calib_data_observ_points (CalibDataID, 0, 0, PoseIndex - 1, Row, Column, PoseIndex, _Pose)    //獲取點座標
Row1 := Row[find(PoseIndex,21)]  //第22個和28個點
Row2 := Row[find(PoseIndex,27)]
Column1 := Column[find(PoseIndex,21)]
Column2 := Column[find(PoseIndex,27)]
*找兩個交點座標
intersection_lines (Row1, Column1, Row2, Column2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowA, ColA, IsOverlapping)  //第一個交點
intersection_lines (Row1, Column1, Row2, Column2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, RowB, ColB, IsOverlapping)  //第二個交點
distance_pp (RowA, ColA, RowB, ColB, Distance)  //距離
line_orientation (RowA, ColA, RowB, ColB, Phi)  //角度
RowCenter := (RowA + RowB) / 2                  //中心點
ColumnCenter := (ColA + ColB) / 2
return ()

主程序

* 
* Initialize the program
dev_close_window ()
dev_open_window (0, 0, 768, 576, 'black', WindowHandle)
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (3)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* 
* Calibrate the camera
* 
CalTabDescrFile := 'caltab_big.descr'
gen_cam_par_area_scan_division (0.008, 0, 0.0000086, 0.0000086, 384, 288, 768, 576, StartCamPar)


create_calib_data ('calibration_object', 1, 1, CalibDataID)
set_calib_data_cam_param (CalibDataID, 0, [], StartCamPar)
set_calib_data_calib_object (CalibDataID, 0, CalTabDescrFile)


NumImages := 10
for I := 1 to NumImages by 1
    read_image (Image, 'calib/calib-3d-coord-' + I$'02d')
    dev_display (Image)
    Message := 'Find calibration plate in\nall calibration images (' + I + '/' + NumImages + ')'
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    * Find the calibration plate


    find_calib_object (Image, CalibDataID, 0, 0, I - 1, [], [])
    get_calib_data (CalibDataID, 'camera', 0, 'init_params', StartCamPar)
    //初始內部攝像機參數
    get_calib_data_observ_points (CalibDataID, 0, 0, I - 1, Row, Column, Index, Pose)
    get_calib_data_observ_contours (Contours, CalibDataID, 'caltab', 0, 0, I - 1)
    gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
    dev_set_color ('green')
    dev_display (Contours)
    dev_set_color ('yellow')
    dev_display (Cross)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
calibrate_cameras (CalibDataID, Error)
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)
//優化的內部攝像頭參數,後面用到
stop()
* 
* Perform measurements
* 
for I := 1 to NumImages by 1
    read_image (Image, 'calib/calib-3d-coord-' + I$'02d')
    * 
    * Now, measure the size of the black border of the plate
    get_measure_positions (Image, PlateRegion, CalibDataID, I, Distance, Phi, RowCenter, ColumnCenter)
    gen_rectangle2_contour_xld (Rectangle, RowCenter, ColumnCenter, Phi, Distance * 0.52, 8)
    gen_measure_rectangle2 (RowCenter, ColumnCenter, Phi, Distance * 0.52, 8, 768, 576, 'nearest_neighbor', MeasureHandle)   //設定測量區域
    * 
    measure_pos (Image, MeasureHandle, 1, 40, 'all', 'all', RowEdge, ColumnEdge, Amplitude, Distance1)    //尋找邊緣對
    Rows := [RowEdge[0],RowEdge[|RowEdge| - 1]]   //  |RowEdge| 元組長度
    Columns := [ColumnEdge[0],ColumnEdge[|RowEdge| - 1]]
    gen_cross_contour_xld (Cross, Rows, Columns, 16, Phi)
    * 
    * Transform the two border points into the world coordinate system

    get_calib_data (CalibDataID, 'calib_obj_pose', [0,I - 1], 'pose', Pose)
    //相對於當前參考相機,優化的校準對象姿態。
    image_points_to_world_plane (CamParam, Pose, Rows, Columns, 'm', SX, SY)
    //將圖像中點的座標轉換成實際座標
    distance_pp (SY[0], SX[0], SY[1], SX[1], Width)
    * 
    * Display results of width measurement
    dev_display (Image)
    dev_set_color ('white')
    dev_set_line_width (3)
    dev_display (Rectangle)
    dev_set_color ('green')
    dev_set_draw ('fill')
    dev_set_line_width (2)
    dev_display (Cross)
    dev_set_draw ('margin')
    disp_message (WindowHandle, 'Width = ' +( Width * 100)$'8.3f' + 'cm', 'window', 12, 12, 'black', 'true')        //$'8.3f'==8字節?小數點後三位
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
    * 

    * Now, measure the size of the calibration marks

    * 

    * Extract the ellipses in the image
    erosion_circle (PlateRegion, ROI, 17.5)   //侵蝕圓形區域,消除小於17.5的
    reduce_domain (Image, ROI, ImageReduced)  //縮小圖像定義域,獲取ROI區域
    edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 60)//使用濾鏡提取子像素精確邊緣
    select_contours_xld (Edges, SelectedEdges, 'contour_length', 20, 99999999, -0.5, 0.5)
    * Fit ellipses to extracted edges
    fit_ellipse_contour_xld (SelectedEdges, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder)//擬合圓,獲取座標,長軸短軸
    MeanRadius1 := mean(Radius1)
    MeanRadius2 := mean(Radius2)
    DevRadius1 := deviation(Radius1)
    DevRadius2 := deviation(Radius2)
    * Transform the ellipses to world coordinates, where they should be circles
    * and convert the circles from meters to millimeters so that we can see them.
    contour_to_world_plane_xld (SelectedEdges, WorldCircles, CamParam, Pose, 'mm')
    //將XLD輪廓轉換爲實際座標
    * Fit ellipses to the circles in world coordinates
    fit_ellipse_contour_xld (WorldCircles, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, RadiusW1, RadiusW2, StartPhi, EndPhi, PointOrder)
    MeanRadiusW1 := mean(RadiusW1)//獲取實際座標中擬合圓的信息
    MeanRadiusW2 := mean(RadiusW2)
    DevRadiusW1 := deviation(RadiusW1)
    DevRadiusW2 := deviation(RadiusW2)
    * 
    * Display results of ellipse measurement
    dev_display (Image)
    dev_set_color ('yellow')
    dev_set_line_width (3)
    dev_display (SelectedEdges)
    Message := 'Measured dimensions of the ellipses'
    Message[0] := '                    Mean Radius1; Mean Radius2; (Standard deviations [%])'
    Message[1] := 'Image coordinates:       ' + MeanRadius1$'5.2f' + 'px; ' + MeanRadius2$'5.2f' + 'px            (' + (DevRadius1 / MeanRadius1 * 100)$'4.2f' + ', ' + (DevRadius2 / MeanRadius2 * 100)$'4.2f' + ')'
    Message[2] := 'World coordinates:       ' + (MeanRadiusW1 / 10)$'5.2f' + 'cm; ' + (MeanRadiusW2 / 10)$'5.2f' + 'cm            (' + (DevRadiusW1 / MeanRadiusW1 * 100)$'4.2f' + ', ' + (DevRadiusW2 / MeanRadiusW2 * 100)$'4.2f' + ')'
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    if (I < 10)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

 

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