其他分享
首页 > 其他分享> > halcon视觉卡尺测量-@龙熙视觉培训

halcon视觉卡尺测量-@龙熙视觉培训

作者:互联网

read_image (ImageModel, 'image/dip_switch_model.png')
get_image_size (ImageModel, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (ImageModel)

在这里插入图片描述

dev_set_draw ('margin')
dev_set_color ('yellow')

*********测量窗口制作
*左边测量直线
draw_line (WindowHandle, HoriLeftBeginRowMode, HoriLeftBeginColumnMode, HoriLeftEndRowMode, HoriLeftEndColumnMode)
gen_region_line (HoriLeftLinesMode, HoriLeftBeginRowMode, HoriLeftBeginColumnMode, HoriLeftEndRowMode, HoriLeftEndColumnMode)
dev_display (HoriLeftLinesMode)

*右边测量直线
draw_line (WindowHandle, HoriRightBeginRowMode, HoriRightBeginColumnMode, HoriRightEndRowMode, HoriRightEndColumnMode)
gen_region_line (HoriRightLinesMode, HoriRightBeginRowMode, HoriRightBeginColumnMode, HoriRightEndRowMode, HoriRightEndColumnMode)
dev_display (HoriRightLinesMode)

在这里插入图片描述

stop()

*********创建形状模板
*画矩形
draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)
*生成矩形
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
*剪切矩形区域图像
reduce_domain (ImageModel, Rectangle, ImageReduced)
*创建形状模型
create_shape_model (ImageReduced, ‘auto’, 0, rad(360), ‘auto’, ‘none’, ‘use_polarity’, 30, 10, ModelID)
*create_shape_model_xld (SelectedContoursModel, ‘auto’, rad(0), rad(90), ‘auto’, ‘auto’, ‘ignore_local_polarity’, 3, ModelID)
*在搜索图像中搜索模版
find_shape_model (ImageModel, ModelID, 0, rad(360), 0.4, 1, 0, ‘least_squares’, 0, 0.7, ModelRow, ModelColumn, ModelAngle, ModelScore)
*获取模板轮廓
get_shape_model_contours (ShapeModel, ModelID, 1)

stop ()

*****测量模型创建

*创建测量模型
create_metrology_model (MetrologyHandle)
*设置测量对象的图像大小
set_metrology_model_image_size (MetrologyHandle, Width, Height)
*添加测量直线对象到测量模型中
Line1 := [HoriLeftBeginRowMode, HoriLeftBeginColumnMode, HoriLeftEndRowMode, HoriLeftEndColumnMode]
Line2 := [HoriRightBeginRowMode, HoriRightBeginColumnMode, HoriRightEndRowMode, HoriRightEndColumnMode]
add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 20, [], [], MetrologyLine)
*获取测量模型里的模型轮廓
get_metrology_object_model_contour (ModelContour, MetrologyHandle, ‘all’, 1.5)
*获取测量模型里的测量区域
get_metrology_object_measures (MeasureContour, MetrologyHandle, ‘all’, ‘all’, Row, Column)
*显示图像及轮廓
dev_display (ImageModel)
dev_display (ModelContour)
*设置测量对象的参考坐标系原点在模板坐标位置
set_metrology_model_param (MetrologyHandle, ‘reference_system’, [ModelRow, ModelColumn,0])
stop()

*****开始测试
*加载离线图像

for Index := 0 to |ImageFiles| - 1 by 1

    *设置区域填充模式
    dev_set_draw ('fill')
    *读取图像
    read_image (Image, ImageFiles[Index])
    *显示图像
    dev_display (Image)
    *在搜索图像中寻找模板
    find_shape_model (Image, ModelID, 0, rad(360), 0.4, 1, 0, 'least_squares', 0, 0.7, SearchRow, SearchColumn, SearchAngle, SearchScore)
    *对齐测量模型
    align_metrology_model (MetrologyHandle, SearchRow, SearchColumn, SearchAngle)
    *测量并对测量区域拟合几何形状
    apply_metrology_model (Image, MetrologyHandle)
    *获取测量结果里的开始行坐标
    get_metrology_object_result (MetrologyHandle, MetrologyLine, 'all', 'result_type', 'row_begin', RowBegin)
    *获取测量结果里的开始列坐标
    get_metrology_object_result (MetrologyHandle, MetrologyLine, 'all', 'result_type', 'column_begin', ColBegin)
    *获取测量结果里的结束行坐标
    get_metrology_object_result (MetrologyHandle, MetrologyLine, 'all', 'result_type', 'row_end', RowEnd)
    *获取测量结果里的结束列坐标
    get_metrology_object_result (MetrologyHandle, MetrologyLine, 'all', 'result_type', 'column_end', ColEnd)
    *获取测量区域
    get_metrology_object_measures (SearchContour, MetrologyHandle, 'all', 'all', Row, Column)
    *获取测量结果轮廓
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
    *显示测量区域和测量结果轮廓
    dev_set_color ('green')
    dev_display (SearchContour)
    dev_set_color ('blue')
    dev_display (MeasuredLines)

在这里插入图片描述

    ******************************************************距离计算*****************************************************************************
    *生成两个直线对象并连接起来
    gen_empty_obj (RegionLine)
    for i:=0 to |RowBegin|-1 by 1
            gen_region_line (Lines, RowBegin[i], ColBegin[i], RowEnd[i], ColEnd[i])
            concat_obj (RegionLine, Lines, RegionLine)
    endfor
    
    *计算两个直线对象之间的距离
    select_obj (RegionLine, FirstLine, 1)
    select_obj (RegionLine, SecondLine, 2)
    distance_rr_min (FirstLine,SecondLine, MinDistance, Row1, Column1, Row2, Column2)
    *在屏幕坐上方0,100位置数值长度值
    dev_set_color ('green')
    set_tposition (WindowHandle, 0, 100)
    write_string (WindowHandle, '两条直线之间的距离='+MinDistance)
    stop()

endfor
stop()
*清除测量模型
clear_metrology_model (MetrologyHandle)

标签:MetrologyHandle,metrology,image,测量,dev,halcon,龙熙,ImageFiles,视觉
来源: https://blog.csdn.net/weixin_45247360/article/details/105933456