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求取两点距离。

后记

大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。

你可能感兴趣的:(#,Halcon示例程序解读与剖析)