Combine Images to display on single page
Show older comments
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)
Image Analyst
on 2 Jan 2019
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.
1 Comment
Cg Gc
on 2 Jan 2019
Image Analyst
on 2 Jan 2019
Don't cat() them together into a 4-D array, just use montage():
multi = montage(img1,img2,img3,img4);
9 Comments
Cg Gc
on 2 Jan 2019
Image Analyst
on 3 Jan 2019
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)
Image Analyst
on 7 Jan 2019
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.
Cg Gc
on 7 Jan 2019
Image Analyst
on 7 Jan 2019
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:

You may have to upgrade to the current version or else stitch the images together yourself (not hard).
Cg Gc
on 7 Jan 2019
Image Analyst
on 9 Jan 2019
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.
Categories
Find more on Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!