【基于描述符的模板匹配】

文章目录


以特征点进行匹配 主要用于模板与图像拍摄角度不相同的场景使用


*本示例在图片数据库中查找文章页面。
*在第一步中,训练不同的页面并创建模型。
*然后,搜索未知图像并检测正确的文章页面。
*请注意,这个示例需要一些内存来训练模型。

dev_update_off ()
dev_close_window ()
read_image (Image, 'brochure/brochure_page_01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_display (Image)

*清除所有已创建的描述符模型。
ModelIDs := []
ModelsFound := 0
NumPoints := []
NumModels := 3
TotalTime := 0

*为可视化目的创建区域。
RowRoi := [10,10,Height - 10,Height - 10]
ColRoi := [10,Width - 10,Width - 10,10]
gen_rectangle1 (Rectangle, 10, 10, Height - 10, Width - 10)
disp_message (WindowHandle, ['Press \'Run\' to start model creation ...','(may take a few minutes)'], 'window', 10, 10, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
***为每个页面创建描述符模型。
for Index := 1 to NumModels by 1
    read_image (Image, 'brochure/brochure_page_' + Index$'.2')
    rgb1_to_gray (Image, ImageGray)
    get_image_size (ImageGray, Width, Height)
    reduce_domain (ImageGray, Rectangle, ImageReduced)
    dev_clear_window ()
    dev_display (ImageGray)
    disp_message (WindowHandle, 'Creating model no. ' + Index + '/' + NumModels + ' ... please wait.', 'window', 10, 10, 'black', 'true')

    *使用默认参数创建描述符模型(缩放除外)
    *为了快速检测,选择了harris二项式点检测器。
    count_seconds (Seconds1)
    create_uncalib_descriptor_model (ImageReduced, 'harris_binomial', [], [], ['min_rot','max_rot','min_scale','max_scale'], [-90,90,0.2,1.1], 42, ModelID)
    count_seconds (Seconds2)
    TotalTime := TotalTime + (Seconds2 - Seconds1)
    *为了在后面的步骤中正确投影矩形,必须将模型的原点设置为图像原点
    set_descriptor_model_origin (ModelID, -Height / 2, -Width / 2)
    ModelIDs := [ModelIDs,ModelID]

    *存储从模型中提取的点,以便以后进行匹配。
    get_descriptor_model_points (ModelID, 'model', 'all', Row_D, Col_D)
    NumPoints := [NumPoints,|Row_D|]
endfor

*模型创建完成。
dev_display (ImageGray)
disp_message (WindowHandle, NumModels + ' models created in ' + TotalTime$'.4' + ' seconds.', 'window', 10, 10, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

*再次初始化窗口,因为图像大小已更改。
read_image (Image, 'brochure/brochure_01')
dev_resize_window_fit_image (Image, 0, 0, -1, -1)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')

*主回路:
*搜索所有图像中的模型
for Index1 := 1 to 12 by 1
    OutputString := []
    NumMsgs := 0
    ModelsFound := 0
    TotalTime := 0
    read_image (Image, 'brochure/brochure_' + Index1$'.2')
    rgb1_to_gray (Image, ImageGray)
    dev_display (Image)
    disp_message (WindowHandle, 'Searching image ...', 'window', 10, 10, 'black', 'true')

    *搜索每个图像中的每个模型
    for Index2 := 0 to |ModelIDs| - 1 by 1

        *查找模型(使用默认参数)
        count_seconds (Seconds1)
        find_uncalib_descriptor_model (ImageGray, ModelIDs[Index2], 'threshold', 600, ['min_score_descr','guided_matching'], [0.003,'on'], 0.25, 1, 'num_points', HomMat2D, Score)
        count_seconds (Seconds2)
        Time := Seconds2 - Seconds1
        TotalTime := TotalTime + Time
        
        *根据考虑的点数,检查找到的实例是否被认为是可能的正确匹配
        if ((|HomMat2D| > 0) and (Score > NumPoints[Index2] / 4))
            get_descriptor_model_points (ModelIDs[Index2], 'search', 0, Row, Col)
            gen_cross_contour_xld (Cross, Row, Col, 6, 0.785398)

            *投影ROI矩形和点
            projective_trans_region (Rectangle, TransRegion, HomMat2D, 'bilinear')
            projective_trans_pixel (HomMat2D, RowRoi, ColRoi, RowTrans, ColTrans)
            angle_ll (RowTrans[2], ColTrans[2], RowTrans[1], ColTrans[1], RowTrans[1], ColTrans[1], RowTrans[0], ColTrans[0], Angle)
            Angle := deg(Angle)
            *根据右上边缘的角度,检查投影的矩形是否被视为右匹配。
            if (Angle > 70 and Angle < 110)
                area_center (TransRegion, Area, Row, Column)
                ModelsFound := ModelsFound + 1
                dev_set_color ('green')
                dev_set_line_width (4)
                dev_display (TransRegion)
                dev_set_colored (12)
                dev_set_line_width (1)
                dev_display (Cross)
                disp_message (WindowHandle, 'Page ' + (Index2 + 1), 'window', Row, Column, 'black', 'true')
                OutputString := [OutputString,'Page ' + (Index2 + 1) + ' found in ' + (Time * 1000)$'.4' + ' ms\n']
            endif
        endif
    endfor
    if (ModelsFound == 0)
        OutputString := 'No model found'
    endif
    NumMsgs := NumMsgs + 1
    OutputString := ['Search time over all pages: ' + (TotalTime * 1000)$'.4' + ' ms',OutputString]
    disp_message (WindowHandle, OutputString, 'window', 10, 10, 'black', 'true')
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
dev_display (ImageGray)
disp_message (WindowHandle, 'Program finished.\nPress \'Run\' to clear all descriptor models.', 'window', 10, 10, 'black', 'true')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【网络星空】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值