Halcon_二维测量_Apply_bead_inspection_model

这个例子展示了如何使用胎圈检查算子来检验粘合剂。
正常涂胶
胎圈检查可用于检测以下错误:
1.缺少粘合胶的部分,断胶
2.粘合剂过多或过少的部分
3.粘合胶离其预定位置太远的部分
在这里插入图片描述

apply_bead_inspection_model要求要检查的图像已经与参考轮廓对齐。
在本例中,采用了基于平面形变匹配的对齐方法。

主要函数:
一、create_bead_inspection_model(BeadContour : : TargetThickness, ThicknessTolerance, PositionTolerance, Polarity, GenParamName, GenParamValue : BeadInspectionModel)
功能:创建一个模型,用于检查图像中的珠子或粘合剂。
为了检查粘合剂,必须设置以下参数:
1、BeadContour目标的粘合剂轮廓
2、TargetThickness:定义最优的粘合剂宽度像素
3、ThicknessTolerance:定义对粘合剂厚度的容忍,以像素为单位。如果珠子的厚度在以下范围被接受:[TargetThickness-ThicknessTolerance, TargetThickness+ThicknessTolerance]
4、位置容忍:定义对粘合剂位置的容忍,以像素为单位。“粘合剂位置”是检测到的粘合剂的中心。如果粘合剂的中心和轮廓位置之间的距离小于位置公差,则粘合剂被接受。
5、Polarity定义图像中要检查的粘合剂的极性。如果珠子比其背景颜色浅,则为“浅色”,否则为“深色”。
6、GenParamName与GenParamValue均可默认为[]
7、BeadInspectionModel为输出的模型

二、apply_bead_inspection_model(Image : LeftContour, RightContour, ErrorSegment : BeadInspectionModel : ErrorType)
功能:应用InspectionModel进行粘合剂检测
参数说明:
1、Image:输入的待检测的图像
2、LeftContour:检测到的粘合剂的左侧轮廓
3、RightContours:检测到的粘合剂右侧轮廓
4、ErrorSegment:用于输出检测到的错误的片段,是一个数组
5、BeadInspectionModel:用于检测的模型
6、ErrorType:错误类型,当ErrorType=0时表示无错误;当ErrorType!=0时可以分为4类,分别是:
‘no bead’:
缺胶
‘incorrect position’:
位置偏移过大
‘too thin’:
胶宽太细,少胶
‘too thick’:
胶宽过宽,多胶

源码及注释如下:

dev_update_off ()
*首先使用仿射变换将图片转正,之后创建平面可变形模模板
*这一步就是为了让每次处理的图片一致性好
prepare_alignment (RegionPart, RowT, ColumnT, ModelID)
*求取检测区域的最小矩形,这样的好处是只对需要进行图像处理的区域进行处理,使算法速度增加。
smallest_rectangle1 (RegionPart, PartRow1, PartColumn1, PartRow2, PartColumn2)
*定义粘合剂的参考轮廓,该操作可以通过draw_xld获取
gen_contour_nurbs_xld (ContourRef, [701.767,626.953,538.867,443.54,390.447,360.28,354.247,363.9,400.1,458.02,509.907,588.34,659.533,696.94], [319.24,336.133,367.507,431.46,489.38,546.093,646.247,722.267,776.567,826.04,869.48,912.92,934.64,929.813], 'auto', [15,15,15,15,15,15,15,15,15,15,15,15,15,15], 3, 1, 5)
*使用下列参数创建一个检测模型
*目标胶宽14
TargetWidth := 14
*宽度容差7
WidthTolerance := 7
*位置容差30
PositionTolerance := 30
*胶条为暗色
Polarity := 'dark'
create_bead_inspection_model (ContourRef, TargetWidth, WidthTolerance, PositionTolerance, Polarity, [], [], BeadInspectionModel)
*将参考的胶条轮廓显示到窗口
read_image (Image, 'bead/adhesive_bead_01')
align_bead (Image, ImageAligned, ModelID, RowT, ColumnT)
*定义两条平行的轮廓线用于限制最小的粘合剂宽度
gen_parallel_contour_xld (ContourRef, ModelSide1, 'regression_normal', TargetWidth * 0.5)
gen_parallel_contour_xld (ContourRef, ModelSide2, 'regression_normal', -TargetWidth * 0.5)
*将两个轮廓放到同意给元组中
concat_obj (ModelSide1, ModelSide2, ModelSides)
*定义两条平行的轮廓线用于限制最大的粘合剂位置偏移s
gen_parallel_contour_xld (ContourRef, PositionToleranceSide1, 'regression_normal', PositionTolerance)
gen_parallel_contour_xld (ContourRef, PositionToleranceSide2, 'regression_normal', -PositionTolerance)
concat_obj (PositionToleranceSide1, PositionToleranceSide2, PositionToleranceSides)
*显示图片以及粘合剂宽度及位置范围
dev_close_window ()
dev_open_window_fit_size (0, 0, PartColumn2 - PartColumn1 + 1, PartRow2 - PartRow1 + 41, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_part (PartRow1 - 20, PartColumn1, PartRow2 + 20, PartColumn2)
dev_display (ImageAligned)
dev_set_line_width (2)
dev_set_color ('green')
dev_display (ContourRef)
dev_set_line_width (1)
dev_display (ModelSides)
dev_set_color ('yellow')
dev_display (PositionToleranceSides)
Message := 'Correct adhesive bead and the reference contour. The'
Message[1] := 'yellow contours indicate the range of position tolerance.'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()

TextOffset := 20
NumImages := 7
for Index := 1 to NumImages by 1
    read_image (Image, 'bead/adhesive_bead_' + Index$'02')
*将输入的图片与参考轮廓对齐
    align_bead (Image, ImageAligned, ModelID, RowT, ColumnT)
*将检测模型应用于输入的图片中去    
    apply_bead_inspection_model (ImageAligned, LeftContour, RightContour, ErrorSegment, BeadInspectionModel, ErrorType)
*显示图片以及粘合剂宽度及位置范围,显示缺陷
    set_window_param (WindowHandle, 'flush', 'false')
    dev_clear_window ()
    dev_display (ImageAligned)
    dev_set_line_width (1)
    dev_set_color ('white')
    dev_display (ContourRef)
    dev_display (ModelSides)
    dev_display (PositionToleranceSides)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (LeftContour)
    dev_display (RightContour)
    dev_set_color ('red')
    dev_display (ErrorSegment)
*根据ErrorType判断粘合剂错误类型
    if (|ErrorType| == 0)
*未检测到错误
        Message := 'Adhesive bead is OK'
        dev_disp_text (Message, 'window', 12, 12, 'white', 'box_color', 'forest green')
        dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
*刷新界面
        flush_buffer (WindowHandle)
        set_window_param (WindowHandle, 'flush', 'true')
        stop ()
    else
*检测到错误,并按错误类型逐一显示
        Message[0] := 'Adhesive bead is not OK:'
*定义错误类型为4类:无胶,缺胶,多胶,位置偏移
        ErrorClasses := ['no bead','too thin','too thick','incorrect position']
        for ClassIndex := 0 to |ErrorClasses| - 1 by 1
            Class := ErrorClasses[ClassIndex]
*在ErrorType中检索错误类型,返回值为数组中的序号,若值非-1,则表示有错误
            ErrorIndices := find(ErrorType,Class)
            if (ErrorIndices != -1)
*选择发生错误的粘合剂,并显示
                select_obj (ErrorSegment, SelectedSegments, ErrorIndices + 1)
                dev_set_color ('red')
                dev_set_line_width (3)
                if (Class != 'no bead')
*若错误类型不是无胶,则显示左右侧轮廓
                    gen_display_segments (SelectedSegments, LeftContour, RightContour, ErrorParts)
                    dev_display (ErrorParts)
                else
*若错误类型为无胶,则显示正常轮廓
                    dev_display (SelectedSegments)
                endif
*计算几何中心
                area_center_points_xld (SelectedSegments, Area, Row, Column)
                for E := 0 to |ErrorIndices| - 1 by 1
                    dev_disp_text (ErrorIndices[E] + 1, 'image', Row[E], Column[E] - TextOffset, 'white', 'box_color', 'red')
                    TextOffset := 20 - TextOffset
                endfor
            endif
        endfor
        dev_disp_text (Message, 'window', 12, 12, 'white', 'box_color', 'red')
        dev_disp_text ([1:|ErrorType|] + ': ' + ErrorType, 'image', 500, 500, 'red', 'box', 'false')
        if (Index < NumImages)
            dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
            flush_buffer (WindowHandle)
            set_window_param (WindowHandle, 'flush', 'true')
            stop ()
        endif
    endif
endfor
flush_buffer (WindowHandle)
set_window_param (WindowHandle, 'flush', 'true')

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值