Combine Images to display on single page

I have multiple sets of 21 images that I would like to display either as a single image or as on one page, which ever is easier. For simplicity I only uploaded 4 here.
I have tried the code below, but I received error messages.
fileFolder = fullfile(matlabroot,'toolbox','images','imdata');
dirOutput = dir(fullfile(fileFolder,'trajplot_*.png'));
fileNames = string({dirOutput.name});
montage(fileNames, 'Size', [2 2]);
Error using montage>getImagesFromFiles (line 366)
FILENAMES must be a cell array of strings or character vectors.
Error in montage>parse_inputs (line 243)
[I,cmap] = getImagesFromFiles(varargin{1});
Error in montage (line 114)
[I,cmap,mSize,indices,displayRange,parent] = parse_inputs(varargin{:});
img1 = imread('trajplot_C1_0012_10m.png');
img2 = imread('trajplot_C2_0012_10m.png');
img3 = imread('trajplot_C3_0012_10m.png');
img4 = imread('trajplot_C5_0012_10m.png');
>> multi = cat(3,img1,img2,img3);
>> montage(multi);
Error using images.internal.imageDisplayValidateParams>validateCData (line
115)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 241)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in montage (line 156)
hh = imshow(bigImage, displayRange,parentArgs{:});
>> multi = cat(4,img1,img2,img3,img4);
>> montage(multi);
%%The image shows up, but is one giant black square.%%
img1 = imread('trajplot_C1_0012_10m.jpeg');
img2 = imread('trajplot_C2_0012_10m.jpeg');
img3 = imread('trajplot_C3_0012_10m.jpeg');
>> multi = cat(3,img1,img2,img3);
montage(multi);
Error using images.internal.imageDisplayValidateParams>validateCData
(line 115)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 241)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in montage (line 156)
hh = imshow(bigImage, displayRange,parentArgs{:});
Does anyone know what is going on? Are my images in the wrong format or possibly too big?
The original files are in .ps format and were converted to .png using an online converter. If they need to be converted into something else, please let me know.

Answers (2)

They appear, from the error message, to not be RGB images. Do they have an alpha plane or something?
What does this say?
whos img1
size(img1)
If it's not 3 planes, then you might take the first 3, if those represent R, G, and B.
img1 = img1(:,:,1:3);
or somehow otherwise process it until you get 3 planes. Attach two of your PNG files if you need more help.
Don't cat() them together into a 4-D array, just use montage():
multi = montage(img1,img2,img3,img4);

9 Comments

Were you able to see the images? When I tried, all I saw was black.
Not in MATLAB, but in Windows photo viewer I could. In Photoshop it showed that most of the image was transparent. What kind of image is this? The entire image is a binary image in the overlay (alpha channel) for some strange reason. What made these images? So you hae to read the alpha channel, not the main image. This code works. Adapt as needed:
files = dir('traj*.png')
allFileNames = {files.name}
hFig1 = figure;
numImages = length(allFileNames)
allImages = cell(1, numImages);
for k = 1 : numImages
thisFileName = allFileNames{k}
[indexedImage, cmap, alpha] = imread(thisFileName);
alpha = 255-alpha;
subplot(2, 2, k);
imshow(alpha, 'ColorMap', cmap);
allImages{k} = alpha;
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Now create the montage
hFig2 = figure;
montage(allImages)
I guess most of the image is transparent. I use GSview to open the file and I see trajectories over Anarctica. It was created by HYSPLIT. Why it creates these types of files is beyond my abilities. It would be super helpful to have other image files and I can reformat them online, but imagine doing so with 500+ images.
I did get a mini montage despite the error message below. Thank you!
thisFileName =
'trajplot_C3_0012_10m_1.png'
Error using images.internal.getImageFromFile (line 7)
The specified filename is not a string.
Error in montage>getImageFromFilenames (line 357)
[I, map] = images.internal.getImageFromFile(fileNames{idx});
Error in montage>getImagesFromFiles (line 371)
[img,map] = getImageFromFilenames(fileNames,1);
Error in montage>parse_inputs (line 243)
[I,cmap] = getImagesFromFiles(varargin{1});
Error in montage (line 114)
[I,cmap,mSize,indices,displayRange,parent] =
parse_inputs(varargin{:});
With this type of bizarre image, you're going to have to pass in allImages to montage() like I showed you.
montage(allImages)
You cannot pass in filename strings because montage will not be able to figure out the secret of how to extract the image from the alpha channel like I did. Did you pass in filenames rather than image arrays? If so, do it like I said instead. If you passed in image arrays, then replicate the problem with just two images and then attach those images so I can fix it.
files = dir('traj*_1_1.png')
allFileNames = {files.name}
hFig1 = figure;
numImages = length(allFileNames)
allImages = cell(1, numImages);
for k = 1 : numImages
thisFileName = allFileNames{k}
[indexedImage, cmap, alpha] = imread(thisFileName);
alpha = 255-alpha;
subplot(2, 2, k);
imshow(alpha, 'ColorMap', cmap);
allImages{k} = alpha;
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Now create the montage
hFig2 = figure;
montage(allImages)
files =
2×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
allFileNames =
1×2 cell array
{'trajplot_C2_0012_10m_1_1.…'} {'trajplot_C3_0012_10m_1_1.…'}
numImages =
2
thisFileName =
'trajplot_C2_0012_10m_1_1.png'
thisFileName =
'trajplot_C3_0012_10m_1_1.png'
Error using images.internal.getImageFromFile (line 7)
The specified filename is not a string.
Error in montage>getImageFromFilenames (line 357)
[I, map] = images.internal.getImageFromFile(fileNames{idx});
Error in montage>getImagesFromFiles (line 371)
[img,map] = getImageFromFilenames(fileNames,1);
Error in montage>parse_inputs (line 243)
[I,cmap] = getImagesFromFiles(varargin{1});
Error in montage (line 114)
[I,cmap,mSize,indices,displayRange,parent] =
parse_inputs(varargin{:});
Two figure boxes actually pop up. Figure 1 has the images. Figure 2, the other, is a blank figure box.
You might have an old version of MATLAB before montage accepted image arrays. It used to just accept a cell array of filenames. I tried your code and it brought up the montage image just fine:
0000 Screenshot.png
You may have to upgrade to the current version or else stitch the images together yourself (not hard).
Those are the images. I will try a newer version. I am running 2017b. Thank you for your help.
Cg Gc
Cg Gc on 8 Jan 2019
Edited: Cg Gc on 9 Jan 2019
I finally upgraded to 2018b and the code works. Thank you!
Last questions. Can the images be in color? Do you know how to get rid of the huge border and make the images larger and less blurry? The border is at least 2 inches wide and makes printing the images a nightmare.
I think the alpha channel can only be gray scale, not RGB, so you're better off trying to get them saved in a normal way in the first place, rather than how they are now.
To get rid of the border, try thresholding and cropping after the montage has been made.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 2 Jan 2019

Commented:

on 9 Jan 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!