Matching

来源:互联网 发布:桌面便签软件 知乎 编辑:程序博客网 时间:2024/06/11 17:41

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)

原创粉丝点击