2

Matlab 中是否有一种优雅normxcorr2的方法可以将输出裁剪为图像的大小,或者仅裁剪到在计算中不使用零填充边缘的矩阵部分?

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

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

例如:

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

答案=

45    45

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

答案=

50    50

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

答案=

55    55

目前我编写了自己的函数,它执行以下操作:

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

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

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

4

3 回答 3

2

这样会简洁很多。我希望这是您正在寻找的:

function I = normxcorr2e(template,im,shape)

  args={'full','same','valid'};
  cropSize=(find(strcmp(shape,args))-1)*size(template);
  crop=@(x,r) x(1+floor(r(1)/2):end-ceil(r(1)/2),1+floor(r(2)/2):end-ceil(r(2)/2))
  I=crop(normxcorr2(template,im),cropSize);
于 2012-02-05T11:25:58.610 回答
2

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

  • 它允许您省略 shape 参数(默认为'full')。
  • 它只调用normxcorr2whenshape是一个有效的字符串。
  • 它使用逻辑索引在一行中执行索引。引线填充和所需中心区域的大小用于创建真值和假值的索引向量。不需要指定尾随填充,因为比它索引的维度短的逻辑索引将简单地用错误值填充。

这是代码:

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
于 2012-02-05T19:13:37.933 回答
1

这里没有太多的优雅——你运行相关性,然后删除你不能使用的东西。但它有效。

function I = normxcorr2e(template,im,shape)

%# perform cross correlation with automated zero-padding
I = normxcorr2(template,im);

switch shape
    case 'same'

        %# if we were guaranteed to have odd-sized templates only
        %# we would only need padLow
        templateSize = size(template);
        padLow = floor(templateSize/2);
        padHigh = templateSize - padLow - 1;

        I = I( (1+padLow(1)):(end-padHigh(1)), (1+padLow(2)):(end-padHigh(2)) );

    case 'full'
        %Do nothing
    case 'valid'
        %# with even size, we need to remove the larger of the two pad sizes
        %# i.e. padLow, on all sides
        templateSize = size(template);
        padLow = templateSize/2;

        I = I( (2*padLow(1)):(end-2*padLow(1)+1), (2*padLow(2)):(end-2*padLow(2)+1) );
    otherwise
        throw(Mexception('normxcorr2e:BadInput','shape %s is not recognized',shape));
end
于 2012-02-05T05:10:45.640 回答