0
点赞
收藏
分享

微信扫一扫

基于Halcon学习的测量拟合【三】pm_measure_board.hdev例程

ixiaoyang8 2022-02-13 阅读 84
*关闭pc的更新
dev_update_pc ('off')
*关闭窗口的更新
dev_update_window ('off')
*关闭程序运行过程中变量窗口的更新
dev_update_var ('off')

*虚拟图像采集,seq文件是一个文本序列,对其中的文字逐行读取,打开对应文本路径的图像,模拟一个相机采集图像
open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'board/board.seq', 'default', -1, 1, FGHandle)
*采集单帧图像,获得图像宽高
grab_image (Image, FGHandle)

*获得图像的宽高
get_image_size (Image, Width, Height)
dev_close_window ()

*打开两个窗口
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_open_window (Height + 70, 0, Width, 120, 'black', WindowHandleText)
dev_set_window (WindowHandle)

*设置两个窗口的字体
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
set_display_font (WindowHandleText, 16, 'mono', 'true', 'false')
dev_set_color ('red')

*显示图像
dev_display (Image)

*定义一些参数画ROI
Row1 := 188
Column1 := 182
Row2 := 298
Column2 := 412
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
*获得ROI的行列坐标
area_center (Rectangle, Area, Row, Column)

*定义测量矩形的一些参数画ROI
Rect1Row := -102
Rect1Col := 5
Rect2Row := 107
Rect2Col := 5
RectPhi := 0

*这里是测量矩形的半宽半高
RectLength1 := 170
RectLength2 := 5

*基于中间的矩形的行列坐标进行画ROI
gen_rectangle2 (Rectangle1, Row + Rect1Row, Column + Rect1Col, RectPhi, RectLength1, RectLength2)
gen_rectangle2 (Rectangle2, Row + Rect2Row, Column + Rect2Col, RectPhi, RectLength1, RectLength2)

*开始创建模板
reduce_domain (Image, Rectangle, ImageReduced)

*第一个参数创建模板的ROI区域图像
*第二个参数金字塔层数
*第三个参数模板的起始角度
*第四个参数模板的总角度
*第五个参数是模板的角度步长,这里设置为1°,也就是说这里总共会创建出360个模板用于寻找图像
*第六个参数是否优化模板点,这里选择不优化
*第七个参数极性选择,选择使用极性则必须是在与模板相同的背景下选择物体,例如模板中是在白色背景下选择黑色的目标物,则查找的时候也是在白色背景下找黑色目标物
*第八个参数是模板灰度阈值,即灰度差值超过这个数值的点才会选择为模板点,
*第九个参数是最小灰度值,用于去除噪声影响的
*最后一个参数模板ID,后面需要用到模板就通过这个ID查找
create_shape_model (ImageReduced, 4, 0, rad(360), rad(1), 'none', 'use_polarity', 30, 10, ModelID)

*获得模板的轮廓线【注:轮廓线在原点】亚像素
get_shape_model_contours (ShapeModel, ModelID, 1)

*进行仿射变换,使得区域回到原来的位置
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DTranslate)

*------------------也可用下方的算子代替--------------------------
*vector_angle_to_rigid (0, 0, 0, Row, Column, 0, HomMat2DTranslate)

 

*显示图片,设置一系列参数
dev_display (Image)
dev_set_color ('green')
dev_display (ShapeModelTrans)

*设置测量矩形的参数
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Rectangle1)
dev_display (Rectangle2)

*设置提醒信息
dev_set_draw ('fill')
dev_set_line_width (1)
dev_set_color ('yellow')
disp_message (WindowHandle, ['Press left button to start','and stop the demo'], 'window', 12, 12, 'black', 'true')

*等待鼠标按钮按下,返回输出窗口中鼠标指针和按下鼠标按钮的像素精确图像坐标
get_mbutton (WindowHandle, Row3, Column3, Button1)
wait_seconds (0.5)

Button := 0
*一直执行,当鼠标左键按下Button=1时程序退出循环
while (Button != 1)
    dev_set_window (WindowHandle)
    dev_set_part (0, 0, Height - 1, Width - 1)
    *从虚拟相机里获取一帧图像换角度   
    grab_image (ImageCheck, FGHandle)
    dev_display (ImageCheck)
    *开始计时
    count_seconds (S1)
    *开始匹配
    
    *查找模板
    *第一个参数用于查找的图像
    *第二个参数模板ID
    *第三个参数起始角度
    *第四个参数查找的总角度值,这里是360度全方位查找
    *第五个参数最小分数,查找到的图像与原模板会有一个相似度比较,越接近1图像越相似
    *第六个参数查找的个数,为0时是把所有目标查找出来
    *第七个参数是最大重叠度,表示找到的两个目标间可以有多大比例重叠
    *第八个参数亚像素精度选择
    *第九个参数金字塔层数
    *第十个参数搜索贪婪度,越大搜索得越快,表示搜索得越不仔细
    *第十一个参数搜索到的目标物的行坐标,搜到多个目标时这个参数是一个数组
    *第十二个参数搜索到的目标物的列坐标,搜到多个目标时这个参数是一个数组
    *第十三个参数搜索到的目标物的角度值,搜到多个目标时这个参数是一个数组
    *第十四个参数搜索到的目标物的分数值,越接近1与模板越相似,搜到多个目标时这个参数是一个数组
    find_shape_model (ImageCheck, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 4, 0.7, RowCheck, ColumnCheck, AngleCheck, Score)
    count_seconds (S2)
    dev_display (ImageCheck)

    *判断窗口是否有图片,防止报错
    if (|Score| > 0)
        dev_set_color ('green')
        *对模板进行仿射变换,变换到搜索到的区域位置,方便在原图上直观显示
        hom_mat2d_identity (HomMat2DIdentity)
        hom_mat2d_translate (HomMat2DIdentity, RowCheck, ColumnCheck, HomMat2DTranslate)
        hom_mat2d_rotate (HomMat2DTranslate, AngleCheck, RowCheck, ColumnCheck, HomMat2DRotate)
        affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DRotate)

        *显示平移旋转后的模板轮廓
        dev_display (ShapeModelTrans)

        *应用任意仿射2D变换到像素坐标。
        affine_trans_pixel (HomMat2DRotate, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)
        affine_trans_pixel (HomMat2DRotate, Rect2Row, Rect2Col, Rect2RowCheck, Rect2ColCheck)
        gen_rectangle2 (Rectangle1Check, Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2)
        gen_rectangle2 (Rectangle2Check, Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2)

        *显示测量矩形
        dev_set_color ('blue')
        dev_set_draw ('margin')
        dev_set_line_width (3)
        dev_display (Rectangle1Check)
        dev_display (Rectangle2Check)

        *生成测量矩形
        dev_set_draw ('fill')
        count_seconds (S3)
        gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)
        gen_measure_rectangle2 (Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)
        
        *下面这个函数是提取与测试矩形垂直方向上的的边缘对,想象一下在矩形长轴方向上做灰度差分,
        *得到的曲线应该是每个一段距离出现一个上尖角与一个下尖角
        *第一个上尖角的X坐标RowEdgeFirst,第一个上尖角的Y坐标ColumnEdgeFirst
        *第一个下尖角的X坐标RowEdgeSecond,第一个下尖角的Y坐标ColumnEdgeSecond
        *第一个参数输入图像
        *第二个参数测量矩形的句柄
        *第三个参数(1.5)是高斯平滑sigma值
        *第四个参数(30)是最低阈值,对应上述尖角的高度值
        *第五个参数('negative')是上述差分值的黑白方向,为negative时是白到黑为第一点,如果是为positive时为黑到白为第一点
        *第六个参数是('all')是返回所有的测量到的值,即返回所有的尖角位置
        *第九个参数(AmplitudeFirst)是上尖角最大幅值的大小
        *(PinwWidth)是上尖角与下尖角之间的距离
        *(PinDistance)是下尖角与上尖角之间的距离
        
        measure_pairs (ImageCheck, MeasureHandle1, 2, 90, 'positive', 'all', RowEdgeFirst1, ColumnEdgeFirst1, AmplitudeFirst1, RowEdgeSecond1, ColumnEdgeSecond1, AmplitudeSecond1, IntraDistance1, InterDistance1)
        measure_pairs (ImageCheck, MeasureHandle2, 2, 90, 'positive', 'all', RowEdgeFirst2, ColumnEdgeFirst2, AmplitudeFirst2, RowEdgeSecond2, ColumnEdgeSecond2, AmplitudeSecond2, IntraDistance2, InterDistance2)
        close_measure (MeasureHandle1)
        close_measure (MeasureHandle2)

        count_seconds (S4)
        *显示直线
        dev_set_color ('red')
        disp_line (WindowHandle, RowEdgeFirst1 - RectLength2 * cos(AngleCheck), ColumnEdgeFirst1 - RectLength2 * sin(AngleCheck), RowEdgeFirst1 + RectLength2 * cos(AngleCheck), ColumnEdgeFirst1 + RectLength2 * sin(AngleCheck))
        disp_line (WindowHandle, RowEdgeSecond1 - RectLength2 * cos(AngleCheck), ColumnEdgeSecond1 - RectLength2 * sin(AngleCheck), RowEdgeSecond1 + RectLength2 * cos(AngleCheck), ColumnEdgeSecond1 + RectLength2 * sin(AngleCheck))
        disp_line (WindowHandle, RowEdgeFirst2 - RectLength2 * cos(AngleCheck), ColumnEdgeFirst2 - RectLength2 * sin(AngleCheck), RowEdgeFirst2 + RectLength2 * cos(AngleCheck), ColumnEdgeFirst2 + RectLength2 * sin(AngleCheck))
        disp_line (WindowHandle, RowEdgeSecond2 - RectLength2 * cos(AngleCheck), ColumnEdgeSecond2 - RectLength2 * sin(AngleCheck), RowEdgeSecond2 + RectLength2 * cos(AngleCheck), ColumnEdgeSecond2 + RectLength2 * sin(AngleCheck))
        dev_set_line_width (1)

        *显示总的引脚数,总数目是两边引脚数之和
        NumLeads := |IntraDistance1| + |IntraDistance2|
        MinDistance := min([InterDistance1,InterDistance2])
        dev_set_window (WindowHandleText)
        dev_set_part (0, 0, 119, Width - 1)
        dev_clear_window ()

        *显示结果
        disp_message (WindowHandleText, 'Matching: Time: ' + ((S2 - S1) * 1000)$'5.2f' + 'ms , Score: ' + Score$'7.5f', 'image', 20, 20, 'green', 'false')
        disp_message (WindowHandleText, 'Measure:  Time: ' + ((S4 - S3) * 1000)$'5.2f' + ' ms, Num. leads: ' + NumLeads$'2d', 'image', 50, 20, 'red', 'false')
        disp_message (WindowHandleText, '          Min. lead dist: ' + MinDistance$'6.3f', 'image', 80, 20, 'red', 'false')
    endif
    dev_error_var (Error, 1)
    dev_set_check ('~give_error')
    get_mposition (WindowHandle, R, C, Button)
    dev_error_var (Error, 0)
    dev_set_check ('give_error')
    if (Error != H_MSG_TRUE)
        Button := 0
    endif
endwhile
dev_set_window (WindowHandleText)
dev_close_window ()

*清除模板
clear_shape_model (ModelID)
close_framegrabber (FGHandle)


举报

相关推荐

0 条评论