halcon创建矩形模板时,让模板中心位于矩形中心

read_image (Image, 'Image_20201218105714642.jpg')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_system ('border_shape_models', 'false')
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('C:/Users/22967/Desktop/方形卡尺测量创建模板', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    
    * Matching 01: BEGIN of generated code for model initialization
*     gen_rectangle1 (ModelRegion, 129.1, 743.5, 916.3, 1569.1)
    draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)
    gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
    reduce_domain (Image, Rectangle, TemplateImage)
    create_shape_model (TemplateImage, 7, rad(0), rad(360), rad(0.2359), ['point_reduction_high','no_pregeneration'], 'use_polarity', [29,39,9], 3, ModelId)
    get_shape_model_contours (ModelContours, ModelId, 1)

    find_shape_model (Image, ModelId, rad(0), rad(360), 0.5, 0, 0.5, 'least_squares', [7,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
    * Matching 01: transform the model contours into the detected positions
    for MatchingObjIdx := 0 to |ModelScore| - 1 by 1
        hom_mat2d_identity (HomMat)
        hom_mat2d_rotate (HomMat, ModelAngle[MatchingObjIdx], 0, 0, HomMat)
        hom_mat2d_translate (HomMat, ModelRow[MatchingObjIdx], ModelColumn[MatchingObjIdx], HomMat)
        affine_trans_contour_xld (ModelContours, TransContours, HomMat)
        dev_display (TransContours)
    endfor
    
    * 根据卡尺测量设置模板中心
    MetrologyRectangleMeasure (TransContours, Image, Contour, Parameter)
    
    DisCenterToModelX:=Parameter[1]-ModelColumn
    DisCenterToModelY:=Parameter[0]-ModelRow
    AngleCenterToModel:=Parameter[2]
    set_shape_model_origin (ModelId, DisCenterToModelY, DisCenterToModelX)
    
    
   
    find_shape_model (Image, ModelId, rad(-180), rad(360), 0.5, 0, 0.5, 'least_squares', [7,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)

    * Matching 01: transform the model contours into the detected positions
    get_shape_model_contours (ModelContours1, ModelId, 1)
    for MatchingObjIdx := 0 to |ModelScore| - 1 by 1
        hom_mat2d_identity (HomMat)
        hom_mat2d_rotate (HomMat, ModelAngle[MatchingObjIdx], 0, 0, HomMat)
        hom_mat2d_translate (HomMat, ModelRow[MatchingObjIdx], ModelColumn[MatchingObjIdx], HomMat)
        affine_trans_contour_xld (ModelContours1, TransContours, HomMat)
        dev_display (TransContours)
    endfor
    stop()
    * Matching 01: Clear model when done
    clear_shape_model (ModelId)
    * Matching 01: END of generated code for model application
    * Image Acquisition 01: Do something
endfor

MetrologyRectangleMeasure函数实现

    union_adjacent_contours_xld (TransContours, UnionContours, 500000, 500000, 'attr_keep')
    length_xld (UnionContours, Length)
    tuple_sort_index (Length, Indices)
    select_obj (UnionContours, ObjectSelected, Indices[|Indices|-1]+1)
    smallest_rectangle2_xld (ObjectSelected, Row1, Column1, Phi1, Length11, Length21)
    
    angle:=deg(Phi1)
    if (angle<0)
        if (-45>angle and angle>=-90)
            angle:=90+angle
        endif
    else
        if (45<angle and angle<=90)
            angle:=angle-90
        endif
    endif
    
    gen_rectangle2_contour_xld (Rectangle1, Row1, Column1, rad(angle), Length11, Length21)
    
    create_metrology_model (MetrologyHandle)
    add_metrology_object_rectangle2_measure (MetrologyHandle, Row1, Column1, rad(angle), Length11, Length21,10,10, 1, 10, ['measure_distance','min_score'], [10,0.2], Index1)
    apply_metrology_model (Image, MetrologyHandle)
    gen_empty_obj (Contours)
    get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', Parameter)
    get_metrology_object_result_contour (Contour, MetrologyHandle, 'all', 'all', 1.5)
    clear_metrology_model (MetrologyHandle)
return ()

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/cashmood/article/details/111632212