一、原理
归一化相关性.NCC,(normalization cross-correlation),顾名思义,就是用于归一化待匹配目标之间的相关程度,注意这里比较的是原始像素。通过在待匹配像素位置p(px,py)构建3*3邻域匹配窗口,与目标像素位置p'(px+d,py)同样构建邻域匹配窗口的方式建立目标函数来对匹配窗口进行度量相关性。它是基于图像灰度信息的匹配方法。
NCC的定义
在[-1,1]绝对尺度范围之间衡量两者的相似性。相关系数刻画了两者之间的近似程度的线性描述。一般说来,越接近于1,两者越近似的有线性关系。
二、实例
原图
NCC模板
需要识别的图像
识别效果
我们发现,即使光照强度很差,NCC模板匹配依然有很好的识别效果
代码
* This example program shows how to use HALCON's correlation-based
* matching. In particular it demonstrates the robustness of this method against
* linear illumination changes. The training is performed in an image with good
* illumination. The matching is applied in images where the exposure time varies
* extremely from very short to very long.
read_image (Image, 'cap_exposure/cap_exposure_03')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_update_window ('off')
gen_circle (Circle, 246, 336, 150)
area_center (Circle, Area, RowRef, ColumnRef)
reduce_domain (Image, Circle, ImageReduced)
create_ncc_model (ImageReduced, 'auto', 0, 0, 'auto', 'use_polarity', ModelID)
dev_set_draw ('margin')
dev_display (Image)
dev_display (Circle)
stop ()
Rows := []
Cols := []
for J := 1 to 10 by 1
read_image (Image, 'cap_exposure/cap_exposure_' + J$'02')
find_ncc_model (Image, ModelID, 0, 0, 0.5, 1, 0.5, 'true', 0, Row, Column, Angle, Score)
vector_angle_to_rigid (RowRef, ColumnRef, 0, Row, Column, 0, HomMat2D)
affine_trans_region (Circle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
Rows := [Rows,Row]
Cols := [Cols,Column]
dev_display (Image)
dev_display (RegionAffineTrans)
stop ()
endfor
* Compute the standard deviation of the found positions. If the individual
* positions in Rows and Cols are examined, it can be seen that the standard
* deviation is caused mainly by the last four images, which are severely
* overexposed.
StdDevRows := deviation(Rows)
StdDevCols := deviation(Cols)
clear_ncc_model (ModelID)
参考:
https://blog.csdn.net/qq_24946843/article/details/82117655?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.nonecase