3

我是 Matlab 的新手,但我真的需要学习它。希望对我的研究非常有用。现在我面临以下问题。

我有两个图像(名为 A 和 B)。两者都有 7 层在同一维度 (4169,6289,7)。首先,我想在 A 图像中找到具有最大值的图层,然后在 B 图像中查找相应的值。例如:如果 A 图像中的第五层有最大值,我需要 B 图像中的第五层的值。我刚刚编写了这段代码 c=max(a,[],3) 来查找 A 图像中具有最大值的层,但不知道如何设置以获得 B 图像中的相应值。你能帮我吗?

非常感谢

4

2 回答 2

1

您不需要最大值的值,您需要第二个参数,即索引。

 [~,indexOfMax] = max(a,[],3); %#Get index of maximal element
 [g1,g2] = ndgrid( 1:size(a,1),1:size(a,2) );  %#Create all possible rows,cols
 linearIndex = sub2ind(size(a), g1(:),g2(:),index(:))  %#Linearize the index of the maximal elements
 value = b(linearIndex); %# Collect the maximal values from b

@RodyOldenhuis 关于内存消耗是正确的。这是一种更节省内存的 for 循环方法:(可能会或可能不会运行得更快,请自行检查)。

 vals = zeros(size(a(:,:,1)));
 [~,indexOfMax] = max(a,[],3);
 for i=1:size(a,1)
    for j=1:size(a,2)
        vals(i,j) = b(i,j, indexOfMax(i,j));
    end 
 end
于 2012-10-25T08:47:46.313 回答
0

这是另一种看法,它的内存效率更高:

% get the indices in 3rd dimension for the max values
[~,I] = max(imgA,[],3)

% Collect all values through double loop
Bvals = arrayfun(@(y) ...
    arrayfun(@(x) imgB(x,y, I(x,y)), 1:size(imgA,1)), ...
    1:size(imgA,2), 'UniformOutput',false);

% reshape them so they correspond to the images' size again
Bvals = reshape([Bvals{:}], size(I))
于 2012-10-25T09:33:17.613 回答