Question

Hi all I am using montage command of matlab to display images. However I am facing a problem. The command that I use is given below:

dirOutput = dir('C:\Users\DELL\Desktop\book chapter\Journal chan vese\robust 

contour initialization\book for document\4 phase\*.jpg');
fileNames = {dirOutput.name}'
montage(fileNames, 'Size', [1 6]);
export_fig combined1.jpg -r300

I have 6 images (all grayscale). However, the command prompt immediately throws an error like this:

//Error using montage>getImagesFromFiles (line 349)
//FILENAMES must contain images that are the same size.

//Error in montage>parse_inputs (line 225)
//    [I,cmap] = getImagesFromFiles(varargin{1});

//Error in montage (line 112)
//[I,cmap,mSize,indices,displayRange] = parse_inputs(varargin{:});

//Error in montage_pics (line 3)
//montage(fileNames, 'Size', [1 6]);

I am even uploading some of my images here:

123

As can be seen clearly, all the images are grayscale. I then read the image size and they are as follows:

1.128X128 2.128X128*3 3.128X128*3 4.128X128 5.128X128*3 6.128X128*3. So some of the images are treated as indeed colour images.

My question is how to use the montage command for such images. Another problem is that montage command always needs images of similar sizes. So I wanted to avoid this loopholes.

Of course I could use a software tool to convert the images to the required format but that is a bad way to work. I believe the below code if added to my original code will solve this problem

%Read Each Image
I=imread('image');
I=imresize(I,[128 128]);
I=I(:,:,1);
%Apply montage command

However I have failed to integrate this code in my original code. Please help me to solve this problem. Thanks in advance guys for your valuable suggestions and help.

Was it helpful?

Solution

To montage you have to make sure

  • The size matches
  • The datatype matches
  • All images are grayscale (or all images are rgb, but do not mix)

.

images={'eight.tif','fabric.png','football.jpg'};
%intended size
ssize=128;
%preallocation
IALL=zeros(ssize,ssize,1,numel(images));
for idx=1:numel(images)
    %get image, ensure double to avoid issues with different colour depths
    I=im2double(imread(images{idx}));
    %resize
    I=imresize(I,[ssize,ssize]);
    %if rgb, change to gray
    if size(I,3)>1 %rgb image
        I=rgb2gray(I);
    end
    %insert
    IALL(:,:,:,idx)=I;
end
montage(IALL);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top