归一化二维互相关矩阵的计算

一. 定义

首先看Matlab help 里normxcorr2函数中关于归一化二维互相关矩阵的定义:
这里写图片描述
核心就是模板template 二维矩阵使用 (u,v) 的位移和fixed 矩阵做点乘求和.
其中template 比 fixed 要小. 假设template矩阵是 mn 的, fixed矩阵是 MN 的.

二. 计算过程

下面的坐标系都是以matlab的矩阵坐标系来说明的.也就是左上角是坐标原点, 竖直方向往下是x正方向,
水平方向往右是y的正方向.
u=0,v=0 时, 两矩阵左上角 (1,1) 位置对齐.
显然,
1. 当 u>0,v>0 时, template 相对于fixed 是往右下角移动了. x轴移动了 u 单位, y轴移动了v单位.
2. 当 u>0,v<0 时, template 相对于fixed 是往左下角移动了. x轴移动了 u 单位, y轴移动了v单位.
3. 当 u<0,v<0 时, template 相对于fixed 是往左上角移动了. x轴移动了 u 单位, y轴移动了v单位.
4. 当 u<0,v>0 时, template 相对于fixed 是往右上角移动了. x轴移动了 u 单位, y轴移动了v单位.
当然, u,v的取值是有限的, 因为必须要保证template和fixed矩阵是有相交部分的.
否则相关系数都是0. 没有计算意义.

所以,
u的取值是 [(m1),M1] , 共 M+m1 点,
v的取值是 [(n1),N1] , 共 N+n1 点.

所以,
最后的相关系数矩阵是 (M+m1)(N+n1) 大小的.

因为matlab是从 (1,1) 索引开始存储数据的.
所以,
(1,1) 索引处, u=(m1)=1m,v=(n1)=1n .

所以,
已知相关系数矩阵索引(i, j), 对应公式中:

u=im,v=jn

通过这个公式, 就可以把mat索引和相关矩阵的偏移量联系起来了.

下面是matlab自带的example:
通过normxcorr2函数求template和fixed的偏移量.
这种方法在图像模板匹配中是常用的方法.
注意: imshow输出的figure坐标是图像坐标, 和mat坐标是不同的.

template = .2*ones(11); % Make light gray plus on dark gray background
template(6,3:9) = .6;   
template(3:9,6) = .6;
BW = template > 0.5;      % Make white plus on black background
figure, imshow(BW), figure, imshow(template)
% Make new image that offsets the template
offsetTemplate = .2*ones(21); 
offset = [3 5];  % Shift by 3 rows, 5 columns
offsetTemplate( (1:size(template,1))+offset(1),...
                (1:size(template,2))+offset(2) ) = template;
figure, imshow(offsetTemplate)
% Cross-correlate BW and offsetTemplate to recover offset  
cc = normxcorr2(BW,offsetTemplate); 
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(template,1)) (xpeak-size(template,2)) ];
isequal(corr_offset,offset) % 1 means offset was recovered
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值