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
• 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)