MATLAB Answers


How to resize a 3D image into a 4D image to use in montage?

Asked by matlab_user on 11 Jun 2018
Latest activity Answered by Arun Mathamkode on 15 Jun 2018

I have some images that I am trying to read in and display as one single image using montage.

At each for loop I read in a new image I and its edge-detected version E. I want to store each 3D array image(one dimension being color) into a cell array. I put the original images in the top row and the edge versions in the bottom row, with the intention that that is how I want them to be displayed in the final image.

In order to do this I have to resize all the images to make sure they are the same size.

However, I am running into errors with imresize. This is what I know so far:

  • imresize only resizes the first two dimensions
  • the montage function takes in a M-by-N-by-1-by-K array

So I believe the issue is that I can't resize it into a 4D array, but I need to in order to use montage. I considered using reshape but I don't think that will work since it doesn't permanently change the size.

Is there any way I can still use resize? Or are there other alternatives?

for i=1:num_imgs 
    I = imread(directory); %read in the image as a 3d matrix
    E = edgesDetect(I,model);    
    I = imresize(I,[600 800 1 3]); %resize into 600x800x1x3
    E = imresize(E, [600 800 1 3]);
    montageImage = cell(2,5); % create a cell with two rows, five columns
    for ii=1:num_imgs
        montageImage{1}{ii} = I;  %store original images in first row
        montageImage{2}{ii} =(1-E);

Note: If I try to simply resize it to be [600 800] without the extra dimensions, I get the following error in montage:

   [bigImage, cmap] = images.internal.createMontage(Isrc, thumbnailSize,...


Sign in to comment.


1 Answer

Answer by Arun Mathamkode on 15 Jun 2018

There are different ways to pass a set of images as input to montage fucntion. I would encourage you to go through the documentation of montage.

Following are the few suggestions,

  • When you are passing the image set as a cell array, each element should be either M by N (grayscale image) or M by N by 3 (RGB image) matrix or the filename of the image. You should not reshape it to M by N by 1 by 3. I think here you have confused it with providing Multi-image array as input.
  • The dimensions of the cell array does not have an impact on the final layout. Hence making your cell array 2 by 5 doesn't mean in the final montage image will have two image rows and five image columns. You need to mention it while calling montage. The Name-Value pair 'Size' represents the number of rows and number of columns in the montage.

There are some errors in your code as well. I think the inner for loop is not required and the cell assignment should be montageImage{1,i} = I; instead of montageImage{1}{ii} = I;


Sign in to comment.