使用归一化互相关匹配对象外形 [英] Matching object outer shape using normalized cross correlation

查看:213
本文介绍了使用归一化互相关匹配对象外形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中使用 normxcorr2 函数进行模板匹配。但是,我想要做的是与 normxcorr2 的做法不同。内置的 normxcorr2 计算交叉相关性,同时考虑矩形模板中的所有像素。但我只想让某些像素参与归一化的互相关过程。

I am working with normxcorr2 function in Matlab for template matching. However, what I want to do is different from what normxcorr2 does. The built-in normxcorr2 computes cross-correlation taking into account all the pixels in a rectangular template. But I only want certain pixels to participate in the normalized cross correlation process.

例如,我想只使用下图中的环状白色区域作为关联时的模板。 (内部的黑色区域不应用于计算)

For example, I want only the ring-like white region in the following image to be used as a template while correlating. (the black region inside should not be used for computation)

因此,如果我将上述模板与下图相关联,我可能会得到一个大约为1.0的标准化值(考虑到外部两个图像中的圆圈直径相同)

And thus if I correlate the above template with the following image, I am likely to get a normalized value of around 1.0 (considering the outer circles are of same diameter in both images)

我已经检查了这个解决方案: - matlab模板仅匹配矩阵中的0(或1),但它不是通用的。

I have checked out this solution:- matlab template matching only for 0 (or 1) in matrix but it is not generic.

有人可以帮助我在matlab中使用更通用的解决方案吗?这可用于通过外部形状跟踪对象

Can anybody help me with a more general solution in matlab? This can be used to track objects by their outer shapes

编辑: - 这是想要查看的人的整个图像它。我想仅通过外圆形边缘检测球,而不是内部细节。

EDIT:- This is the entire image for those who want to have a look at it. I want to detect the ball only by the outer circular rim, not the inner details.

推荐答案

好的,我们试一试......这个解决方案尝试使用现有的 normxcorr2 实现并修改它以解决yoru问题。

Ok, let's give it a try... This solution tries to use existing normxcorr2 implementation and modify it to solve yoru problem.

规范化互相关的公式为:

The formula for normalized cross correlation is:

在这种情况下,您希望更改每个窗口的集成边界。这会影响标准偏差和相关性本身。让我们分几步解决它:

In this case you want to change the integration boundaries for every window. This is turn affects both standard deviations and the correlation itself. Lets tackle it in several steps:

步骤1:获得正确的相关性

我们可以通过修改模板图像来做到这一点:

We can do this by modifying the template image:

template_fix = template;
mean_template_mask = mean(template(mask == 1));
template_fix(mask == 0) = mean_template_mask;
result = normxcorr2(template_fix, query)

请注意,通过进行此更改,我们将模板的平均值等于掩码侧面模板的平均值。这样,掩码外的所有模板像素都不会对整合产生影响,因为它们等于平均值​​。

Notice that by making this change we make the mean value of the template to be equal to the mean value of the template in side the mask. this way all template pixels outside the mask don't contribute to the integration as they are equal to the mean value.

步骤2:修复模板std

size_mask = sum(mask(:));
size_template = numel(template);
std_template = std2(template);
std_template_masked = sqrt(sum((template_fix(:) - mean_template_mask).^2)/size_mask);
result = result * (std_template/std_template_masked);

步骤3:修复查询标准

sum_filt = ones(size(template));
std_query = filter2(query.^2, sum_filt) - filter2(query, sum_filt).^2/size_template;
std_query = sqrt(std_query/size_template);

std_query_mask = filter2(query.^2, mask) - filter2(query, mask).^2/size_mask;    
std_query_mask = sqrt(std_query_mask/size_mask);

result = result .* std_query ./ std_query_mask;

我的Matlab没有回应,所以我没有机会在实践中测试它。除非我错过了一些错误,否则应该在数学上等同。

My Matlab is not responding so I didn't have the chance to test it in practice. Unless I missed some errors it should be mathematically equivalent.

此解决方案会进行一些额外的回旋,但它不会多次处理重叠的像素。

This solution does some extra convolutions but it doesn't process overlapping pixels more than once.

如果你多次使用相同的模板图像然后你可以重构步骤1& 2只运行一次进行预处理。虽然两者的计算成本都不高。

If you use the same template image multiple times then you could refactor steps 1 & 2 to run only once for preprocessing. Although both shouldn't be computationally expensive.

不同方法:直接前进

Different approach: Straight forward

这是一种不同的直接方法,它不使用原始的 normxcorr2 函数。此代码可以很容易地针对内存使用进行优化,但会降低可读性。

Here is a different, straightforward approach that doesn't use the original normxcorr2 function. This code can be easily optimized for memory usage in expense of readability.

% q for query, t for template and mask for mask
% shape = 'full' or 'same' or 'valid'

t_mask = t .* mask;
n      = numel(mask);
tq_sum = filter2(t_mask,q, shape);

q_sum  = filter2(mask, q, shape);    
q_mean = q_sum/n;
t_sum  = sum(t_mask(:));
t_mean = t_sum/n;

res1 = tq_sum - t_mean*q_sum - t_sum*q_mean + t_mean*q_mean*n;

t_std = sqrt((sum(t_mask(:).^2) - sum(t_mask(:)).^2/n)/n);
q_std = sqrt((filter2(mask, q.^2, shape) - q_sum.^2/n)/n);

res = res1 ./ (n * t_std * q_std)

这篇关于使用归一化互相关匹配对象外形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆