二维位置定位--使用自适应灰度模板匹配

对应示例程序:
     adapt_pattern.hdev

目标:使用自适应灰度模板对由图像采集设备获取的图像执行在线模式匹配

思路为:
      1.窗口初始化
      2.设置是在线视频,还是本地图片组成的一个视频流。取第一幅图像为模板图像,在线视频的话就手动画个矩形,确定后续的匹配ROI,离线视频的话就读入已经设置好的参数。这一步主要是确定一个基于灰度值的匹配模板。
      3.对后续的视频图像进行匹配,然后从能匹配到的候选区域中找到最好的一个,如果最后一个最佳匹配的灰度差小于15,则为匹配创建一个新模板。其实就是从将最佳匹配作为新模板,一直迭代。
      4.进行图像的显示,以及最后的关闭句柄等。

图像:
*                                                                                                       原图:
在这里插入图片描述

*                                                                             绿色矩形包围的模板匹配ROI
在这里插入图片描述

*                                                                                 匹配结果

在这里插入图片描述
代码:

* ***************************************************************

*这个例子展示了如何使用自适应灰度模板对由图像采集设备获取的图像执行在线模式匹配。

*如果变量“Online”设置为“true”,则图像将由open_framegrabber()中指定的图像采集设备采集。

*否则,使用虚拟图像获取设备来读取图像序列。

*

*有关模板匹配的其他示例,请检查程序pm*.hdev
dev_update_off ()
dev_close_window ()
* 
* Open an image acquisition device
* -----------------------------------------------------------------
* If Online is set to 'true' the specified image acquisition device
* is opened. Otherwise a virtual image acquisition device is opened.
Online := false
if (Online)
    open_framegrabber ('GigEVision', 0, 0, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', 'default', 0, 0, AcqHandle)
else
    open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'card/card', '', 1, 2, AcqHandle)
endif
* 
* Grab and display an image
grab_image (Image, AcqHandle)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
* 
* Display settings
dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width (3)
* 
* Set up
if (Online)
    for Index := 0 to 100 by 1
        grab_image (Image, AcqHandle)
        dev_display (Image)
    endfor
endif
* 
* Define a template for the pattern matching
* --------------------------------------------------------
* Define a pattern for the matching which is then further
* preprocessed for the template matching.
* For Online = true, a pattern is generated by drawing a rectangle.
* For Online = false, a default pattern is loaded.
if (Online)
    * 
    * Generate a pattern
    draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
    gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
    dev_display (Rectangle)
    reduce_domain (Image, Rectangle, ImageReduced)
    create_template (ImageReduced, 5, 4, 'sort', 'original', TemplateID)
else
    * 
    * Load a default pattern
    Row1 := 164.5
    Row2 := 196.5
    Column1 := 152.5
    Column2 := 229.5
    gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
    dev_display (Rectangle)
    reduce_domain (Image, Rectangle, ImageReduced)
    create_template (ImageReduced, 5, 4, 'sort', 'original', TemplateID)
endif
disp_message (WindowHandle, 'Initial template for pattern matching', 'window', 10, 10, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Determine the half of template width and height
TemplateHeight2 := (Row2 - Row1) / 2.0
TemplateWidth2 := (Column2 - Column1) / 2.0
* 
* Pattern matching
* --------------------------------------------------------
* Search for best matches of the template in the image and
* display the found template. If matches with small errors
* were found, adapt the pattern for the next matching.
for Index := 0 to 80 by 1
    * 
    * Grab and display an image for the matching
    if (Online)
        grab_image_async (Image, AcqHandle, -1)
    else
        grab_image (Image, AcqHandle)
    endif
    dev_display (Image)
    * 
    * Adapt the template to the size of the image 使模板适应图像的大小
    adapt_template (Image, TemplateID)
    * 
    * Search all matches of the template in the image and
    * return all points showing an error smaller than 20
    *搜索图像中模板的所有匹配项,并返回所有显示误差小于20的点
    fast_match_mg (Image, Matches, TemplateID, 20, 3)
    area_center (Matches, Area, Row, Column)
    if (Area > 0)
        dilation_circle (Matches, MergedMatches, 3.5)
        connection (MergedMatches, ConnMatches)
        add_channels (ConnMatches, Image, ImageMatches)
        * 
        * Search the best match of the template and display
        * the found template
        *搜索模板的最佳匹配并显示找到的模板
        best_match (ImageMatches, TemplateID, 20, 'true', Row, Column, Error)
        NumMatches := |Row|
        BestMatchIndex := -1
        BestMatchError := 255
        for I := 0 to NumMatches - 1 by 1
            if (Error[I] < 255)
                disp_message (WindowHandle, 'Template found', 'window', 10, 10, 'black', 'true')
                disp_rectangle1 (WindowHandle, Row[I] - TemplateHeight2, Column[I] - TemplateWidth2, Row[I] + TemplateHeight2, Column[I] + TemplateWidth2)
                BestMatchIndex := I
                BestMatchError := Error[I]
            endif
        endfor
        * 
        * Adapt the pattern
        * --------------------------------------------------------
        * Create a new template for the matching if the gray value
        * difference of the last best match is smaller than 15
        *调整模式
        * 如果最后一个最佳匹配的灰度差小于15,则为匹配创建一个新模板
        if (BestMatchIndex >= 0)
            if (BestMatchError < 15)
                clear_template (TemplateID)
                gen_rectangle1 (Rectangle, Row[BestMatchIndex] - TemplateHeight2, Column[BestMatchIndex] - TemplateWidth2, Row[BestMatchIndex] + TemplateHeight2, Column[BestMatchIndex] + TemplateWidth2)
                reduce_domain (Image, Rectangle, ImageReduced)
                create_template (ImageReduced, 5, 4, 'sort', 'original', TemplateID)
            endif
        endif
    endif
endfor
clear_template (TemplateID)
close_framegrabber (AcqHandle)

用到的几个算子:
    create_template --创建一个灰度值匹配的模板
    adapt_template–使模板适应图像的大小
    fast_match_mg–查找匹配图像
    best_match–搜索模板的最佳匹配

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值