Matching (2)

 

HALCON 10.0  II-B

 

 

3.1 Gray-Value-Based Matching

 

 基于灰度的匹配是一个非常经典的方法,它仅适用于对象的不模糊,不缺失,灰度值不变化。适用于对象旋转转。注意对

 

于所有的应用,相关性和基于形状的匹配是要优先考虑的。使用灰度匹配的理由可能是由于匹配的对象和灰度有关系。

 

The rare cases in which the very slow classical gray-valuebased matching is to be preferred comprise the case that the   matching must be illumination-variant. If, e.g. a colored pattern has to be found and the hue value of the object in the search image must not deviate from the hue value of the object in the template image, the illumination-invariant approaches might be less suitable, as they use normalized gray values, i.e., they evaluate the relative differences between the grayvalues instead of the absolute values.

 

使用灰度值匹配的步骤:

 

• Create a model with create_template if the object is expected to be only translated but not

       rotated or create_template_rot(与create_template不同,支持旋转) if the object has to be found also in a
       rotatedposition in the search image.
 
    • Search the model in images with best_match, best_match_mg, best_match_pre_mg,
       best_match_rot, best_match_rot_mg, fast_match, or fast_match_mg (see below for the
       differences between the operators).
       
        best_ 返回的最佳匹配点 是一个点
        fast_ 返回一个区域,是匹配到的区域
        ***_mg 是支持金字塔.
        **_rot_** 支持旋转 

• Clear the model from memory with clear_template.

 

 

一个例子,扣件匹配。

 

 
 

dev_close_window()

read_image(Image,'E:/钢轨缺陷/扣件下/227 1.bmp')

get_image_size(Image,Width,Height)

dev_open_window(0,0,Width,Height,'black',WindowHandle)

dev_display(Image)

*draw_rectangle1(WindowHandle,Row1,Column1,Row2,Column2)

*gen_rectangle1(TemplateRegion,Row1,Column1,Row2,Column2)
gen_rectangle1(TemplateRegion,5.0,38.5,84.0,186.5)

*equ_histo_image(Image,Image)

reduce_domain(Image,TemplateRegion,ImageTemplate)

*创建模板
**************************************************
* create_template_rot(Template 用于做模板的图像
* : : NumLevel, 金字塔数
* AngleStart, 最小旋转角度
* AngleExtend,最大旋转角度
* AngleStep,旋转步长
* Optimize, 优化选项 sort /none (sort 创建模板的时间会长点,随之
* 带来的是匹配更准确    
* GrayValues : 原始灰度值: original
* normalized 
* 梯度:gradient sobel 
* TemplateID) 
******************************************************
create_template_rot (ImageTemplate, 4, rad(0), rad(360), rad(1), 'sort', 'sobel', TemplateID)

dev_set_draw('margin')

*匹配
for i :=67 to 200 by 1 
    read_image(ImageDst,'E:/钢轨缺陷/扣件下/'+i+' 1.bmp'
    dev_display(ImageDst)
    count_seconds(beginTime)
    *
    *MaxEorr (0~255) 容许的最大匹配偏差
    *Error 返回实际匹配到的偏差 。如果是255 则未匹配到 , 此时应该增加MaxEorr 
    *
    best_match_rot_mg (ImageDst, TemplateID, rad(0), rad(360), 100,\
                  'false', 4, Row, Column, Angle, Error)
    count_seconds(endTime)
    Time :=round(1000 * (endTime - beginTime))
    disp_message(WindowHandle,Time+' ms','image',-1,-1,'black','true')
    gen_rectangle2(Rectangle,Row,Column,Angle,75,40)
    dev_display(Rectangle)
    stop()
endfor

clear_template(TemplateID)

 

 
 
3.2 Correlation-Based Matching 基于相关性的匹配
 
    This approach uses a normalized cross correlation (NCC 归一化互相关系数)to evaluate the correspondence between a model and a search image。它比传统的基于灰度的匹配更高效,对于变形,纹理缺失,图像模糊的对象也能找到。
 

 
dev_close_window()
 
read_image(Image,'E:/钢轨缺陷/扣件下/227 1.bmp')
 
get_image_size(Image,Width,Height)
 
dev_open_window(0,0,Width,Height,'black',WindowHandle)
 
dev_display(Image)
 
*draw_rectangle1(WindowHandle,Row1,Column1,Row2,Column2)
 
*gen_rectangle1(TemplateRegion,Row1,Column1,Row2,Column2)
gen_rectangle1(TemplateRegion,5.0,38.5,84.0,186.5)
area_center(TemplateRegion,Area,RowRef,ColumnRef)
 
 
reduce_domain(Image,TemplateRegion,ImageTemplate)
 
create_ncc_model (ImageTemplate, 'auto',rad(-20), rad(20), 'auto', 'use_polarity', ModelID)
 
dev_set_draw('margin')
 
*匹配
for i :=103 to 200 by 1 
    read_image(ImageDst,'E:/钢轨缺陷/扣件下/'+i+' 1.bmp') 
    count_seconds(beginTime)
 
 
    *NumLevel 金字塔提高了速度 同时也会损失精准度
    find_ncc_model (ImageDst, ModelID, rad(-20), rad(20), 0.2, 1, 0, 'true',0 , Row, Column, Angle, Score)
    count_seconds(endTime)
    Time :=round(1000 * (endTime - beginTime))
 
  * gen_rectangle2(Rectangle,Row,Column,Angle,75,40)
    vector_angle_to_rigid(RowRef,ColumnRef,0,Row,Column,Angle,HomMat2D)
    affine_trans_region(TemplateRegion,ReginAffineTrans,HomMat2D,'false')
 
    dev_display(ImageDst)
  * dev_display(Rectangle)
    dev_display(ReginAffineTrans)
    disp_message(WindowHandle,Time+' ms','image',-1,-1,'black','true')
    stop()
endfor
 
clear_ncc_model(ModelID)





posted @ 2013-10-24 15:16  小马_xiaoLV2  阅读(1488)  评论(0编辑  收藏  举报