细胞作为 normxcorr2 的输入参数

cell as an input argument for normxcorr2

我想计算具有大量模板(31 个模板)的图像的归一化互相关。当我将模板定义为单元格并编译时:

parfor ii =1 :100
   T {ii,:}=normxcorr2(template{:} ,image{ii});
end

returns 一个错误,因为 normxcorr2 的输入不能是单元格(只能是矩阵)。我当然可以使用 for 循环,例如:

parfor ii =1 :100
for j= 25:55
  % T {ii,j}=normxcorr2(template{j} ,image{ii});
end
end

但是它会花费更多时间(因为嵌套循环)。

我的问题是是否有解决方案可以不使用嵌套循环。

也许重新排列单元格数组中的模板,然后将 normxcorr2 应用为 cellfun 可以帮助您:

% generate random image and templates
image = rand(100);
% templates of 5X5
templates = rand(5,5,31);
% convert templates into cell array
templates = squeeze(mat2cell(templates,size(templates,1),...
    size(templates,2),ones(1,size(templates,3))));
% apply cellfun
T = cellfun(@(t) normxcorr2(t ,image),templates,'UniformOutput',0);

请注意,如果您的模板未在元胞数组中给出并且模板的大小不相等(此处为 5X5),那么您将需要以不同的方式创建 templates 元胞数组(尽管仍然很简单).