基于灰度值的模板匹配,首先创建一个模板,在图像金字塔中去旋转缩放式搜索,寻找均值或方差与模板最接近的区域,即匹配到该模板。虽然受光照影响大,但如果是线性变换的光照,可以通过设置模板的均值偏差来解决这个问题,也叫模板自适应。
基于相关性的模板匹配不受线性光照影响,下面就拿基于相关性模板匹配例程中受光照程度逐渐增强的瓶盖图像来测试。
*基于灰度值的模板匹配
read_image (Image, 'cap_exposure_03.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
disp_image (Image, WindowHandle)
draw_circle (WindowHandle, Row1, Column1, Radius)
gen_circle (Circle, Row1, Column1, Radius)
reduce_domain (Image, Circle, ImageReduced1)
*创建模板
create_template (ImageReduced1, 255, 4, 'sort', 'original', TemplateID1)
*计算模板的均值和方差
intensity (Circle, ImageReduced1, Mean1, Deviation1)
dev_set_draw ('margin')
dev_set_line_width (3)
list_files ('C:/Users/Administrator/Desktop/halcon/基于灰度值的模板匹配', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
dev_display (Image)
*计算经过光照变换后的模板的均值和方差
intensity (Circle, Image, Mean2, Deviation2)
*设置模板的均值偏差
set_offset_template (TemplateID1, Mean2-Mean1)
*匹配模板
best_match_rot_mg (Image, TemplateID1, -0.39, 0.79, 40, 'false', 3, Row, Column, Angle, Error)
disp_circle (WindowHandle, Row, Column, Radius)
disp_continue_message (WindowHandle, 'black', 'true')
stop()
endfor
随机选取几张测试结果如下,可见利用模板自适应可解决基于灰度值模板匹配中的线性光照问题: