一种以类似于'conv2'的方式获取`normxcorr2`的输出的优雅方法-(删除不需要的边缘) [英] An elegant way to get the output of `normxcorr2` in a manner similar to 'conv2' - (removing the unwanted edges)

查看:126
本文介绍了一种以类似于'conv2'的方式获取`normxcorr2`的输出的优雅方法-(删除不需要的边缘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中是否有优雅的方式来将normxcorr2的输出裁剪到图像的大小,或者只裁剪到矩阵的在计算中不使用零填充边的部分?

Is there an elegant way in Matlab to get the output of normxcorr2 cropped to the size of the image or cropped only to the part of the matrix that does not use zero padded edges in computation?

要理解我的意思,请考虑使用conv2命令.有一个名为shape的可选参数,可以将其设置为samevalid.

To understand what I mean, consider the conv2 command. There is an optional parameter called shape that can be set to same or valid.

C = conv2(A,B,'same');
C = conv2(A,B,'valid');

例如:

size( conv2( rand(50,50) , rand(6,6), 'valid') ) 

ans =

45    45


size( conv2( rand(50,50) , rand(6,6), 'same') )

ans =

50    50


size( conv2( rand(50,50) , rand(6,6)) )

ans =

55    55

当前,我编写了自己的函数,该函数是这样的:

Currently I wrote my own function, that does something like this:

function I = normxcorr2e(template,im,shape)
    switch shape
        case 'same'
            I = normxcorr2(template,im);
            r = size(I,1)-size(im,1);
            c = size(I,2)-size(im,2);

            m1=floor(r/2);
            n1=floor(c/2);
            m2=ceil(r/2);
            n2=ceil(c/2);

            I(1:m2,:) = [];
            I(end-m1+1:end,:) = [];

            I(:,1:n2) = [];
            I(:,end-n1+1:end) = [];
        case 'full'
            %Do nothing
        case 'valid'
            %TODO - write this case...
        otherwise
            throw(Mexception('normxcorr2e:BadInput','shape %s is not recognized',shape));
    end

end

您有更好的主意吗?成功答案的主要标准将是所提出解决方案的优雅.

Do you have a better idea? The main criterion for a successful answer will be the elegance of the proposed solution.

编辑(1)首先,感谢您的所有回答.他们都是好人,我对此表示赞同.我还没有决定哪一个最好.顺便说一句,我最近正在考虑模板与图像相比较大的情况.在这种情况下,通过在运行normxcorr2之前裁剪image参数来加快计算速度是很有意义的.

Edit(1) First of all, thanks for all of your answers. All of them are good and upvoted by me. I still haven't decided which is the best. By the way, I am thinking recently about the case where the template is large compared to the image. In that case, it makes sense to speedup the computation by cropping the image argument before running normxcorr2.

推荐答案

相对于其他答案,这是一个具有一些额外功能的变体:

Here's a variant that has a few extra features relative to the other answers:

  • 它允许您省略shape参数(默认为'full').
  • shape时,它仅调用 normxcorr2 是有效的字符串.
  • 它使用逻辑索引在一行中执行索引.引线填充的大小和所需的中心区域用于创建真值和假值的索引向量.不需要指定尾随填充,因为比其索引的维短的逻辑索引将简单地填充上错误的值.
  • It allows you to omit the shape argument (default is 'full').
  • It only calls normxcorr2 when shape is a valid string.
  • It performs the indexing in one line using logical indexing. The sizes of the lead padding and desired center region is used to create index vectors of true and false values. The trailing padding doesn't need to be specified since a logical index that is shorter than the dimension it indexes will simply be padded with false values.

这是代码:

function I = normxcorr2e(template, im, shape)

  if (nargin == 2) || strcmp(shape,'full')
      I = normxcorr2(template, im);
      return
  end

  switch shape
      case 'same'
          pad = floor(size(template)./2);
          center = size(im);
      case 'valid'
          pad = size(template) - 1;
          center = size(im) - pad;
      otherwise
          throw(Mexception('normxcorr2e:BadInput',...
              'SHAPE must be ''full'', ''same'', or ''valid''.'));
  end

  I = normxcorr2(template, im);
  I = I([false(1,pad(1)) true(1,center(1))], ...
        [false(1,pad(2)) true(1,center(2))]);

end

这篇关于一种以类似于'conv2'的方式获取`normxcorr2`的输出的优雅方法-(删除不需要的边缘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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