如何使用2D矩阵作为4D矩阵的索引?
我有:
images
的形式在称为[Y, X, RGB, imageIndex]
的4D矩阵中堆叠的七个图像(15x10x3x7)的堆栈indexes
(15x10),用于存储每个像素来自哪个图像output = zeros(10, 15, 3, 'uint8')
% for example, every pixel in image-1 is (250, 250, 250)
images = zeros(10, 15, 3, 7, 'uint8');
images(:,:,:,1) = 250;
images(:,:,:,2) = 230;
images(:,:,:,3) = 210;
images(:,:,:,4) = 190;
images(:,:,:,5) = 170;
images(:,:,:,6) = 150;
images(:,:,:,7) = 130;
% this is the index for each pixel
indexes = randi(7, 10, 15); % randomly select each pixel from the seven images
下面的代码可以正常工作:对于每个像素,请使用indexes
选择它来自哪个图像
% create the output image
output = zeros(10, 15, 3, 'uint8');
for y = 1:10
for x = 1:15
output(y, x, :) = images(y, x, :, indexes(y, x));
end
end
image(output);
这对于较大的文件来说很慢。是否可以执行images(indexes)
或someFunction(images, indexes)
之类的操作以达到相同的结果?