基於灰度的模板匹配(帶旋轉角度)

原圖

選擇模板

旋轉180度進行識別

繼續旋轉 ,依然可以識別

代碼:

* Searching the best matching of a template in an image (with rotation).
* 
dev_close_window ()
read_image (For6, 'for6')
get_image_size (For6, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
* Interpolate 2 video half images
fill_interlace (For6, ImageFilled, 'odd')
* gen_rectangle1 (Rectangle, 266, 390, 348, 435)
gen_rectangle1 (Rectangle, 78, 292, 129, 402)
* gen_rectangle1 (Rectangle, 258, 214, 296, 258)
area_center (Rectangle, Area, Row, Column)
reduce_domain (ImageFilled, Rectangle, ImageReduced)
pi := acos(0.0) * 2
* Preparing a pattern for template matching with rotation
create_template_rot (ImageReduced, 4, -pi, 2 * pi, pi / 45, 'sort', 'original', TemplateID)
dev_set_color ('red')
SumError := 0.0
Count := 0
for Phi := -180 to 180 by 5.0
    hom_mat2d_identity (HomMat2DIdentity)
    hom_mat2d_rotate (HomMat2DIdentity, rad(Phi), 256, 256, HomMat2DRotate)
    affine_trans_image (ImageFilled, ImageAffinTrans, HomMat2DRotate, 'constant', 'false')
*     add_noise_white (ImageAffinTrans, ImageAffinTrans, 30)
    best_match_rot_mg (ImageAffinTrans, TemplateID, -pi, 2 * pi, 40, 'true', 4, Row, Column, Angle, Error)
    disp_arrow (WindowID, Row, Column, Row - cos(Angle) * 50, Column - sin(Angle) * 50, 1)
    AngleDeg := deg(Angle)
    DiffAngle := abs(AngleDeg - Phi)
    if (DiffAngle > 360)
        DiffAngle := abs(360 - DiffAngle)
    endif
    SumError := SumError + DiffAngle
    Count := Count + 1
endfor
TotalError := SumError / Count
clear_template (TemplateID)

 

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