Halcon例程分析17:查找各向異性模板(基於形狀)

打開halcon,按下ctrl+e打開halcon自帶例程。方法->模板匹配(基於形狀)->find_aniso_shape_model.hdev

* This example program shows how to use HALCON's shape-based matching
* to find SMD capacitors that exhibit independent size changes in the
* row and column direction in images with a depth of 10 bits
* (i.e., in images of type uint2).
* 
dev_update_off ()
dev_close_window ()
* 
* For visualization purposes, we specify the number of significant
* bits in the uint2 images.  We do this because this information
* cannot be stored in the image files themselves.
set_system ('int2_bits', 10)
read_image (Image, 'smd/smd_capacitors_01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
Message := 'This program shows how to use shape-based matching'
Message[1] := 'to find SMD capacitors that exhibit independent'
Message[2] := 'size changes in the row and column direction in'
Message[3] := 'images with a depth of 10 bits.'
Message[4] := 'First a synthetic model for the SMD capacitors'
Message[5] := 'is created. In the next step the created model'
Message[6] := 'is used to find the SMD capacitors.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_color ('green')
dev_set_line_width (3)
* 
* Create a synthetic model for the SMD capacitors.  This is just
* a rectangle with rounded corners.
*生成模板輪廓
gen_contour_polygon_rounded_xld (Contour, [50,100,100,50,50], [50,50,150,150,50], [6,6,6,6,6], 1)
*生成一幅圖像,大小爲200×150
gen_image_const (Image, 'byte', 200, 150)
*把一個輪廓繪製在圖像上,輪廓的灰度值爲128
paint_xld (Contour, Image, ImageModel, 128)
dev_display(ImageModel)
dev_display(Contour)
create_aniso_shape_model (ImageModel, 'auto', -rad(10), rad(20), 'auto', 0.9, 1.7, 'auto', 0.9, 1.1, 'auto', 'none', 'use_polarity', 5, 20, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
dev_display(ModelContours)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 
* Find the models for the SMD capacitors in the images
* and display the number of found models an the recognition time
* and the model's scale
for J := 1 to 4 by 1
    read_image (Image, 'smd/smd_capacitors_' + J$'02d')
    dev_display (Image)
    count_seconds (S1)
    *查找模板,
    find_aniso_shape_model (Image, ModelID, -rad(10), rad(20), 0.9, 1.7, 0.9, 1.1, 0.7, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, ScaleR, ScaleC, Score)
    count_seconds (S2)
    Time := (S2 - S1) * 1000
    Num := |Score|
    * Display the found models and the recognition time
    disp_message (WindowHandle, Num$'d' + ' models found in ' + Time$'5.2f' + ' ms', 'window', 12, 12, 'black', 'true')
    MeanColumn := mean(Column)
    for I := 0 to Num - 1 by 1
        * Display the found model.
        *顯示查找到的模板
        hom_mat2d_identity (HomMat2D)
        *對模板做縮放,ScaleR[I]是找到的第I個模板行縮放比例,ScaleC[I]是找到的第I個模板列縮放比例
        hom_mat2d_scale (HomMat2D, ScaleR[I], ScaleC[I], 0, 0, HomMat2D)
        *對查找到的模板旋轉
        hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
        *對查找到的模板平移
        hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
        *對原模板進行旋轉平移縮放變換,得到找到的模板的位置,方便顯示出來
        affine_trans_contour_xld (ModelContours, ContoursTrans, HomMat2D)
        dev_display (ContoursTrans)
        * 
        * Display the model's scale next to the found model
        *在模板周圍顯示縮放尺度信息
        ScaleRowStr := 'ScaleRow=' + ScaleR[I]$'5.3f'
        ScaleColStr := 'ScaleCol=' + ScaleC[I]$'5.3f'
        get_string_extents (WindowHandle, ScaleRowStr, AscentStr, DescentStr, WidthStr, HeightStr)
        if (Column[I] <= MeanColumn)
            disp_message (WindowHandle, [ScaleRowStr,ScaleColStr], 'image', Row[I] - 20, Column[I] - 60 - WidthStr, 'green', 'false')
        else
            disp_message (WindowHandle, [ScaleRowStr,ScaleColStr], 'image', Row[I] - 20, Column[I] + 60, 'green', 'false')
        endif
    endfor
    * Deactivate the following lines to run the programm without breaks
    if (J < 4)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
* Reset the number of significant bits of uint2 images to unknown.
set_system ('int2_bits', -1)

手動創建的模板圖像

待查找原圖像

查找結果圖像

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章