当前位置:网站首页>Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine

Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine

2022-07-05 07:55:00 BoomBiuBiu

* This sample program demonstrates how to use HALCON Shape based matching , Find multiple different models in one call to find _shape_ Model . Please note that this is a frequently useful mode of operation .

Multi template matching routine


* Turn off computer updates during program execution .
dev_update_pc ('off')
* During the execution of the shutdown program , Switch the automatic output of the icon output object to the graphics window .
dev_update_window ('off')
* Close the update of the variable window during program execution .
dev_update_var ('off')
* close window 
dev_close_window ()
* Open a new window 
dev_open_window (0, 0, 646, 482, 'black', WindowHandle)

* Adaptive picture 
dev_set_part (0, 0, 481, 645)

* Set parameters 
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
* These colors will be used to graphically identify different models in the following visualization code .
Colors := ['red','green','cyan']

* The model will be generated from a hard coded rectangle given by the following coordinates :
Row1 := [135,150,185]
Column1 := [250,170,220]
Row2 := [375,310,335]
Column2 := [355,395,375]

* The object model will contain a set of XLD outline . They are used below to overlay the found model on the current image .
* Use XLD Contours are because they can transform faster than regions . This will cause a small problem , Because there are usually multiple XLD
* Contour lines will represent a model . Therefore, different models will be stored in IndexS and IndexE in .

* Set an empty array 
gen_empty_obj (Models)
IndexS := []
IndexE := []

* Variable ModelIDs Include the different models created below .
ModelIDs := []

* Again ,RowsRef and ColumnsRef Store reference points for different models .
* They are necessary to transform the model into an instance found in the current image .
for J := 1 to 3 by 1
* Read the picture 
    read_image (Image, 'metal-parts/metal-part-model-' + J$'02d')
    dev_display (Image)
    dev_set_color ('green')
    * Set the position of the text cursor 
    set_tposition (WindowHandle, 20, 20)
    * Print text in the window .
    write_string (WindowHandle, 'Generating shape model ' + J$'d')

    * use ROI Draw rectangular matting 
    gen_rectangle1 (Rectangle, Row1[J - 1], Column1[J - 1], Row2[J - 1], Column2[J - 1])
    * Get row and column coordinates 
    area_center (Rectangle, Area, Row, Column)
    * Cutout 
    reduce_domain (Image, Rectangle, ImageReduced)
  

    * Form different connected domains 
    connection (ModelRegions, ConnectedRegions)
    * Select according to the area characteristics 
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 100000)
    * Joint region 
    union1 (SelectedRegions, ModelRegions)

    * Convert the skeleton to XLD outline .
    gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter') 
    
    * Set the color 
    dev_set_color ('red')
    dev_display (ModelContours)
    * Create a template 
    create_shape_model (ImageReduced, 5, rad(0), rad(360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID)
    * Get the outline of the template 
    get_shape_model_contours (ModelCont, ModelID, 1)

    * Returns the outline representation of the shape model .
    select_shape_xld (ModelCont, ModelContours, 'contlength', 'and', 20, 1000)

    * Calculate the current model and how many in the model XLD Contour stored model . This is calculation Index and IndexE Necessary .
    *Models The above defines null 
    count_obj (ModelContours, NumModel)
    * Calculate the outline of the existing container 
    count_obj (Models, NumModels)
    * Similar to building a container , Put the outline of the control in 
    concat_obj (Models, ModelContours, Models)
    *
    IndexS := [IndexS,NumModels + 1]
    * The number represents the number of contours 
    IndexE := [IndexE,NumModels + NumModel]
    * Model handle 
    ModelIDs := [ModelIDs,ModelID]
endfor

dev_set_color ('yellow')
* Set the position of the text cursor .
set_tposition (WindowHandle, 50, 20)
* Display text on the window 
write_string (WindowHandle, 'Press left button to start')

set_tposition (WindowHandle, 80, 20)
write_string (WindowHandle, 'and stop the demo.')

* Wait for the mouse button to press .
get_mbutton (WindowHandle, Row3, Column3, Button1)
wait_seconds (0.5)
dev_set_color ('red')

* Define some parameters 
Button := 0
ImgNo := 1
while (Button != 1)
    * Read the picture 
    read_image (Image, 'metal-parts/metal-parts-' + ImgNo$'02d')
    * Calculate the time before recognition 
    count_seconds (S1)

    * Note that this is models
    find_shape_models (Image, ModelIDs, rad(0), rad(360), 0.5, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, Score, Model)
    * Calculate the time after recognition 
    count_seconds (S2)
    Time := (S2 - S1) * 1000
    dev_display (Image)

    * Define how many objects there are 
    Num := |Score|
    for J := 0 to Num - 1 by 1
        ** Select the correct... From the model object XLD outline .
        * Copy HALCON Icon objects in the database .
        copy_obj (Models, ModelSelected, IndexS[Model[J]], IndexE[Model[J]] - IndexS[Model[J]] + 1)
        * Affine transformation 
        vector_angle_to_rigid (0, 0, 0, Row[J], Column[J], Angle[J], HomMat2D)
        affine_trans_contour_xld (ModelSelected, ModelTrans, HomMat2D)
        dev_set_color (Colors[Model[J]])
        dev_display (ModelTrans)
    endfor
    dev_set_color ('yellow')
    set_tposition (WindowHandle, 20, 20)
    if (Num == 1)
        write_string (WindowHandle, Num$'1d' + ' object found in ' + Time$'4.2f' + 'ms')
    else
        write_string (WindowHandle, Num$'1d' + ' objects found in ' + Time$'4.2f' + 'ms')
    endif
    ImgNo := ImgNo + 1
    if (ImgNo > 15)
        ImgNo := 1
    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
for J := 0 to |ModelIDs| - 1 by 1
    clear_shape_model (ModelIDs[J])
endfor

原网站

版权声明
本文为[BoomBiuBiu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140548302562.html