1

How can I use a 2D matrix as an index for 4D matrix?

I have:

  • A stack of seven images (15x10x3x7) in a 4D matrix called images in the form [Y, X, RGB, imageIndex]
  • A corresponding index matrix indexes (15x10) which stores which image each pixel come from
  • An output image 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

The code below works correctly: for each pixel, use indexes to choose which image it comes from

% 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);

This is slow for larger files. Is it possible to do something like images(indexes), or someFunction(images, indexes) to achieve the same result?

DarkMatterMatt
  • 576
  • 10
  • 22

0 Answers0