halcon之机器视觉测量-卡尺测量

流程:
halcon之机器视觉测量-卡尺测量_第1张图片
halcon代码如下:

ead_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()

******************************************************开始测试*************************************************
*加载离线图像
* Image Acquisition 01: Code generated by Image Acquisition 01
ImageFiles := []
ImageFiles[0] := 'image/dip_switch_model.png'
ImageFiles[1] := 'image/dip_switch_training_01.png'
ImageFiles[2] := 'image/dip_switch_training_02.png'
ImageFiles[3] := 'image/dip_switch_training_03.png'
ImageFiles[4] := 'image/dip_switch_training_04.png'
ImageFiles[5] := 'image/dip_switch_training_05.png'
ImageFiles[6] := 'image/dip_switch_training_06.png'
ImageFiles[7] := 'image/dip_switch_training_07.png'
ImageFiles[8] := 'image/dip_switch_training_08.png'
ImageFiles[9] := 'image/dip_switch_training_09.png'
ImageFiles[10] := 'image/dip_switch_training_10.png'
ImageFiles[11] := 'image/dip_switch_training_11.png'
ImageFiles[12] := 'image/dip_switch_training_12.png'

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)

你可能感兴趣的