天天看點

HALCON示例程式inspect_bga.hdev測量bga焊點缺陷HALCON示例程式inspect_bga.hdev測量bga焊點缺陷

HALCON示例程式inspect_bga.hdev測量bga焊點缺陷

示例程式源碼(加注釋)

  • 關于顯示類函數解釋

    read_image (Image, ‘bga_14x14_model’)

    get_image_pointer1 (Image, Pointer, Type, Width, Height)

    dev_update_off ()

    dev_close_window ()

    dev_open_window (0, 0, Width, Height, ‘light gray’, WindowHandle)

    set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)

    dev_set_part (0, 0, Height - 1, Width - 1)

    dev_display (Image)

    dev_set_line_width (1)

    dev_set_color (‘green’)

    VisualizeIntermediate := true

  • 快速二值化

    fast_threshold (Image, Region, 95, 255, 3)

  • 分割定義域

    connection (Region, ConnectedRegions)

  • 通過面積與長短軸比篩選區域

    select_shape (ConnectedRegions, SelectedRegions, [‘area’,‘anisometry’], ‘and’, [20,1.0], [100,1.7])

  • 使用矩形元素膨脹區域

    dilation_rectangle1 (SelectedRegions, RegionDilation, 3, 3)

  • 求取灰階重心

    area_center_gray (RegionDilation, Image, Volume, Row, Column)

  • 建立像素點表示的區域

    gen_region_points (RegionBGACenters, Row, Column)

  • 求取可旋轉最小外接矩形

    smallest_rectangle2 (RegionBGACenters, RowBGARect, ColumnBGARect, PhiBGARect, Length1BGARect, Length2BGARect)

    if (VisualizeIntermediate)

    dev_display (Image)

    dev_set_color (‘red’)

    dev_display (RegionBGACenters)

    dev_set_color (‘green’)

    dev_set_draw (‘margin’)

    gen_rectangle2 (BGARect, RowBGARect, ColumnBGARect, PhiBGARect, Length1BGARect, Length2BGARect)

    dev_display (BGARect)

    dev_set_draw (‘fill’)

    endif

    disp_message (WindowHandle, ‘Reference BGA’, ‘window’, 10, 10, ‘black’, ‘false’)

    disp_continue_message (WindowHandle, ‘black’, ‘true’)

    stop ()

  • 定義變量并初始化

    BallsPerRow := 14

    BallsPerCol := 14

  • 二維仿射變換

    BallDistCol := 2 * Length1BGARect / (BallsPerCol - 1)

    BallDistRow := 2 * Length2BGARect / (BallsPerRow - 1)

    hom_mat2d_identity (HomMat2DIdentity)

  • 旋轉

    hom_mat2d_rotate (HomMat2DIdentity, -PhiBGARect, RowBGARect, ColumnBGARect, HomMat2DRotate)

  • 移動

    hom_mat2d_translate (HomMat2DRotate, -RowBGARect + Length2BGARect, -ColumnBGARect + Length1BGARect, HomMat2DTranslate)

  • 縮放

    hom_mat2d_scale (HomMat2DTranslate, 1 / BallDistRow, 1 / BallDistCol, 0, 0, HomMat2DScale)

  • 對點進行仿射變換

    affine_trans_point_2d (HomMat2DScale, Row, Column, RowNormalized, ColNormalized)

  • round轉換為整數

    BGARowIndex := round(RowNormalized)

    BGAColIndex := round(ColNormalized)

    NumBalls := |Row|

    dev_set_color (‘black’)

    if (VisualizeIntermediate)

    dev_clear_window ()

    dev_set_draw (‘margin’)

    for i := 0 to NumBalls - 1 by 1

    繪十字線
      disp_cross (WindowHandle, 10 + BGARowIndex[i] * 10, 10 + BGAColIndex[i] * 10, 6, 0)
               

    endfor

    dev_set_draw (‘fill’)

    endif

  • gen_tuple_const生成常量數組

    BallMatrix := gen_tuple_const(BallsPerRow * BallsPerCol,-1)

    for i := 0 to NumBalls - 1 by 1

    BallMatrix[BGARowIndex[i] * BallsPerCol + BGAColIndex[i]] := i

    endfor

    disp_continue_message (WindowHandle, ‘black’, ‘true’)

    stop ()

    read_image (Image, ‘bga_14x14_defects’)

    dev_display (Image)

    disp_message (WindowHandle, ‘BGA to be checked’, ‘window’, 10, 10, ‘black’, ‘false’)

    disp_continue_message (WindowHandle, ‘black’, ‘true’)

    stop ()

  • 線面一段代碼和上邊建立模闆時一樣

    fast_threshold (Image, Region, 95, 255, 3)

    connection (Region, ConnectedRegions)

    select_shape (ConnectedRegions, SelectedRegions, [‘area’,‘anisometry’], ‘and’, [20,1.0], [100,1.7])

    dilation_rectangle1 (SelectedRegions, RegionDilation, 3, 3)

    area_center_gray (RegionDilation, Image, VolumeCheck, RowCheck, ColumnCheck)

    elliptic_axis_gray (RegionDilation, Image, RaCheck, RbCheck, PhiCheck)

    AnisometryCheck := RaCheck / RbCheck

    gen_region_points (RegionBGACenters, RowCheck, ColumnCheck)

    smallest_rectangle2 (RegionBGACenters, RowBGACheck, ColumnBGACheck, PhiBGACheck, Length1BGACheck, Length2BGACheck)

    if (VisualizeIntermediate)

    dev_display (Image)

    dev_set_color (‘red’)

    dev_display (RegionBGACenters)

    dev_set_color (‘green’)

    dev_set_draw (‘margin’)

    gen_rectangle2 (BGARect, RowBGACheck, ColumnBGACheck, PhiBGACheck, Length1BGACheck, Length2BGACheck)

    dev_display (BGARect)

    dev_set_draw (‘fill’)

    endif

    BallDistCol := 2 * Length1BGACheck / (BallsPerCol - 1)

    BallDistRow := 2 * Length2BGACheck / (BallsPerRow - 1)

    hom_mat2d_identity (HomMat2DIdentity)

    hom_mat2d_rotate (HomMat2DIdentity, -PhiBGACheck, RowBGACheck, ColumnBGACheck, HomMat2DRotate)

    hom_mat2d_translate (HomMat2DRotate, -RowBGACheck + Length2BGACheck, -ColumnBGACheck + Length1BGACheck, HomMat2DTranslate)

    hom_mat2d_scale (HomMat2DTranslate, 1 / BallDistRow, 1 / BallDistCol, 0, 0, HomMat2DScale)

    affine_trans_point_2d (HomMat2DScale, RowCheck, ColumnCheck, RowNormalized, ColNormalized)

    BGARowIndex := round(RowNormalized)

    BGAColIndex := round(ColNormalized)

    NumBalls := |RowCheck|

    dev_set_color (‘black’)

    if (VisualizeIntermediate)

    dev_clear_window ()

    dev_set_draw (‘margin’)

    for i := 0 to NumBalls - 1 by 1

    disp_cross (WindowHandle, 10 + BGARowIndex[i] * 10, 10 + BGAColIndex[i] * 10, 6, 0)

    endfor

    dev_set_draw (‘fill’)

    endif

    BallMatrixCheck := gen_tuple_const(BallsPerRow * BallsPerCol,-1)

    for i := 0 to NumBalls - 1 by 1

    BallMatrixCheck[BGARowIndex[i] * BallsPerCol + BGAColIndex[i]] := i

    endfor

    disp_continue_message (WindowHandle, ‘black’, ‘true’)

    stop ()

    Rows1 := gen_tuple_const(NumBalls,0)

    Cols1 := gen_tuple_const(NumBalls,0)

    Rows2 := gen_tuple_const(NumBalls,0)

    Cols2 := gen_tuple_const(NumBalls,0)

    Phi2 := gen_tuple_const(NumBalls,0)

    Ra2 := gen_tuple_const(NumBalls,0)

    Rb2 := gen_tuple_const(NumBalls,0)

    Anisometry2 := gen_tuple_const(NumBalls,0)

    Volume2 := gen_tuple_const(NumBalls,0)

    j := 0

    for i := 0 to BallsPerRow * BallsPerCol - 1 by 1

    if (BallMatrix[i] >= 0 and BallMatrixCheck[i] >= 0)

    Rows1[j] := Row[BallMatrix[i]]

    Cols1[j] := Column[BallMatrix[i]]

    Rows2[j] := RowCheck[BallMatrixCheck[i]]

    Cols2[j] := ColumnCheck[BallMatrixCheck[i]]

    Phi2[j] := PhiCheck[BallMatrixCheck[i]]

    Ra2[j] := RaCheck[BallMatrixCheck[i]]

    Rb2[j] := RbCheck[BallMatrixCheck[i]]

    Anisometry2[j] := AnisometryCheck[BallMatrixCheck[i]]

    Volume2[j] := VolumeCheck[BallMatrixCheck[i]]

    j := j + 1

    endif

    endfor

  • 從至少兩個點對應關系近似剛性仿射變換,即由旋轉和平移組成的變換

    vector_to_rigid (Rows1, Cols1, Rows2, Cols2, HomMat2D)

    affine_trans_point_2d (HomMat2D, Rows1, Cols1, RowTrans, ColumnTrans)

    affine_trans_point_2d (HomMat2D, Row, Column, RowTransFull, ColumnTransFull)

    if (VisualizeIntermediate)

    dev_clear_window ()

    for i := 0 to |Rows2| - 1 by 1

    * 通過元組元素生成XLD邊緣輪廓

    gen_contour_polygon_xld (Lines, [Rows2[i],RowTrans[i]], [Cols2[i],ColumnTrans[i]])

    dev_display (Lines)

    endfor

    endif

  • 兩個點之間的距離

    distance_pp (Rows2, Cols2, RowTrans, ColumnTrans, Distance)

  • 求取最大距離

    MaxDistance := max(Distance)

dev_display (Image)

dev_set_draw (‘margin’)

gen_empty_obj (EllipseCorrect)

gen_empty_obj (EllipseDeviation)

gen_empty_obj (EllipseDeformation)

gen_empty_obj (EllipseVolume)

gen_empty_obj (Missing)

dev_set_color (‘white’)

VolumeMean := mean(Volume)

VolumeMin := min(Volume)

VolumeMax := max(Volume)

VolumecheckMean := mean(VolumeCheck)

VolumeCheckMin := min(VolumeCheck)

VolumeCheckMax := max(VolumeCheck)

j := 0

for i := 0 to BallsPerRow * BallsPerCol - 1 by 1

if (BallMatrix[i] >= 0 and BallMatrixCheck[i] >= 0)

gen_ellipse (Ellipse, Rows2[j], Cols2[j], Phi2[j], Ra2[j], Rb2[j])

if (Distance[j] > 0.05)

concat_obj (EllipseDeviation, Ellipse, EllipseDeviation)

else

if (Anisometry2[j] > 1.2)

concat_obj (EllipseDeformation, Ellipse, EllipseDeformation)

else

if (Volume2[j] < 5500 or Volume2[j] > 10000)

concat_obj (EllipseVolume, Ellipse, EllipseVolume)

else

concat_obj (EllipseCorrect, Ellipse, EllipseCorrect)

endif

endif

endif

j := j + 1

else

if (BallMatrix[i] >= 0)
    生成XLD十字線
        gen_cross_contour_xld (Cross, RowTransFull[BallMatrix[i]], ColumnTransFull[BallMatrix[i]], 10, 0.785398)
        concat_obj (Missing, Cross, Missing)
    endif
endif
           

endfor

dev_set_line_width (2)

dev_display (Image)

disp_message (WindowHandle, ‘White cross: segmentation of ball failed’, ‘window’, 10, 10, ‘black’, ‘false’)

dev_set_color (‘white’)

dev_display (Missing)

disp_continue_message (WindowHandle, ‘black’, ‘true’)

stop ()

dev_display (Image)

disp_message (WindowHandle, ‘Ellipse: center positions deviate’, ‘window’, 10, 10, ‘black’, ‘false’)

dev_set_color (‘white’)

dev_display (EllipseDeviation)

disp_continue_message (WindowHandle, ‘black’, ‘true’)

stop ()

dev_display (Image)

disp_message (WindowHandle, ‘Ellipse: deformed balls’, ‘window’, 10, 10, ‘black’, ‘false’)

dev_set_color (‘white’)

dev_display (EllipseDeformation)

disp_continue_message (WindowHandle, ‘black’, ‘true’)

stop ()

dev_display (Image)

disp_message (WindowHandle, ‘Ellipse: gray value volume out of range’, ‘window’, 10, 10, ‘black’, ‘false’)

dev_set_color (‘white’)

dev_display (EllipseVolume)

處理思路

這個例子是主要講解了焊點完整性檢測。gen_region_points生成點區域;hom_mat2d_identity、hom_mat2d_rotate、hom_mat2d_translate、hom_mat2d_scale、affine_trans_point_2d、vector_to_rigid仿射變換; gen_contour_polygon_xld;繪制十字線distance_pp求取兩點距離。

後記

大家有什麼問題可以向我提問哈,我看到了第一時間回複,希望在學習的路上多多結交良師益友。

繼續閱讀