Error using montage function
6 views (last 30 days)
Show older comments
I have a script that, given 16 images,creates eight montages
I have read in the documentation that it is possible to use an handle to call the montage, and i have done that because i want to be able to show every single montage using imshow to be able to give titles to every one of them.
Running the script causes errors to occur.
Here is the script
clear all;
close all;
clc;
%Importare le immagini
%Posizioni centroidi
PosClust1=imread("Cent_col_cluster1.png");
PosClust2=imread("Cent_col_cluster2.png");
PosClust3=imread("Cent_col_cluster3.png");
PosClust4=imread("Cent_col_cluster4.png");
PosClust5=imread("Cent_col_cluster5.png");
PosClust6=imread("Cent_col_cluster6.png");
PosClust7=imread("Cent_col_cluster7.png");
PosClust8=imread("Cent_col_cluster8.png");
PosClust9=imread("Cent_col_cluster9.png");
%Colori centroidi
Cent_col_Clust1=imread("Cent_pos_cluster1.png");
Cent_col_Clust2=imread("Cent_pos_cluster2.png");
Cent_col_Clust3=imread("Cent_pos_cluster3.png");
Cent_col_Clust4=imread("Cent_pos_cluster4.png");
Cent_col_Clust5=imread("Cent_pos_cluster5.png");
Cent_col_Clust6=imread("Cent_pos_cluster6.png");
Cent_col_Clust7=imread("Cent_pos_cluster7.png");
Cent_col_Clust8=imread("Cent_pos_cluster8.png");
Cent_col_Clust9=imread("Cent_pos_cluster9.png");
%-------------------------------------------------------------------------
%Trasformazione in imagetipe double
%Posizioni centroidi
PosClust1Double=im2double(PosClust1);
PosClust2Double=im2double(PosClust2);
PosClust3Double=im2double(PosClust3);
PosClust4Double=im2double(PosClust4);
PosClust5Double=im2double(PosClust5);
PosClust6Double=im2double(PosClust6);
PosClust7Double=im2double(PosClust7);
PosClust8Double=im2double(PosClust8);
PosClust9Double=im2double(PosClust9);
%Colori centroidi
Cent_col_Clust1Double=im2double(Cent_col_Clust1);
Cent_col_Clust2Double=im2double(Cent_col_Clust2);
Cent_col_Clust3Double=im2double(Cent_col_Clust3);
Cent_col_Clust4Double=im2double(Cent_col_Clust4);
Cent_col_Clust5Double=im2double(Cent_col_Clust5);
Cent_col_Clust6Double=im2double(Cent_col_Clust6);
Cent_col_Clust7Double=im2double(Cent_col_Clust7);
Cent_col_Clust8Double=im2double(Cent_col_Clust8);
Cent_col_Clust9Double=im2double(Cent_col_Clust9);
%Creazione montaggi
Clust1=montage(Cent_col_Clust1Double,PosClust1Double);
Clust2=montage(Cent_col_Clust2Double,PosClust2Double);
Clust3=montage(Cent_col_Clust3Double,PosClust3Double);
Clust4=montage(Cent_col_Clust4Double,PosClust4Double);
Clust5=montage(Cent_col_Clust5Double,PosClust5Double);
Clust6=montage(Cent_col_Clust6Double,PosClust6Double);
Clust7=montage(Cent_col_Clust7Double,PosClust7Double);
Clust8=montage(Cent_col_Clust8Double,PosClust8Double);
Clust9=montage(Cent_col_Clust9Double,PosClust9Double);
%Plot
%Cluster1
imshow(Clust1)
title('Cluster1')
%Cluster2
figure
imshow(Clust2)
title('Cluster2')
%Cluster3
figure
imshow(Clust3)
title('Cluster3')
%Cluster4
figure
imshow(Clust4)
title('Cluster4')
%Cluster5
figure
imshow(Clust5)
title('Cluster5')
%Cluster6
figure
imshow(Clust6)
title('Cluster6')
%Cluster7
figure
imshow(Clust7)
title('Cluster7')
%Cluster8
figure
imshow(Clust8)
title('Cluster8')
%Cluster9
figure
imshow(Clust9)
title('Cluster9')
These errors appear
Error using images.internal.imageDisplayParsePVPairs
Invalid input arguments.
Error in images.internal.imageDisplayParseInputs (line 70)
[common_args,specific_args] = images.internal.imageDisplayParsePVPairs(varargin{:});
Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in montage (line 231)
hh = imshow(bigImage,cmap,parentArgs{:},interpolationArgs{:});
Error in Montaggi_kmeans (line 52)
Clust1=montage(Cent_col_Clust1Double,PosClust1Double);
All Cent_col_Clust images are 420x560x3 uint8
All PosClust images are 595x842x3 uint8
All Cent_col_ClustDouble images are 420x560x3 double
All PosClustDouble images are 595x842x3 double
I have no clue what the problem is. Any ideas?
I cannot post all images, daily attachments limit is 10.
2 Comments
Rik
on 16 Aug 2024
Since you have an error on a particular line and all images will behave the same way, we can cut most of the lines and reduce your code to a MWE (minimal working (or in this case; failing) example):
PosClust1=imread("Cent_col_cluster1.png");
%Colori centroidi
Cent_col_Clust1=imread("Cent_pos_cluster1.png");
%-------------------------------------------------------------------------
%Trasformazione in imagetipe double
%Posizioni centroidi
PosClust1Double=im2double(PosClust1);
%Colori centroidi
Cent_col_Clust1Double=im2double(Cent_col_Clust1);
%Creazione montaggi
Clust1=montage(Cent_col_Clust1Double,PosClust1Double);
This code only require 2 files, after which it can be run (as long as you post it with this format ('code') instead of what you used in your question ('example').
Accepted Answer
Rik
on 16 Aug 2024
If you check the documentation, which of the syntaxes are you actually using?
It looks to me like montage(I,map), while you probably intended montage(imagelist).
The fix is easy: just wrap the variable names in a cell array. Even better if you forgo numbered variable by reading the files cell vectors in the first place.
arrayfun(...
@(index) fprintf('this would have read %s\n',sprintf('Cent_col_cluster%d.png',index)),...
1:9,'Uniform',false) % show result with 1:9
PosClust = arrayfun(...
@(index) imread(sprintf('Cent_col_cluster%d.png',index)),...
1,'Uniform',false); % replace with 1:9 in your code
%Colori centroidi
Cent_col_Clust = arrayfun(...
@(index) imread(sprintf('Cent_pos_cluster%d.png',index)),...
1,'Uniform',false); % replace with 1:9 in your code
%-------------------------------------------------------------------------
%Trasformazione in imagetipe double
%Posizioni centroidi
for n=1:numel(PosClust)
PosClustDouble{n} = im2double(PosClust{n});
end
%Colori centroidi
for n=1:numel(PosClust)
Cent_col_ClustDouble{n} = im2double(Cent_col_Clust{n});
end
%Creazione montaggi
Clust{1}=montage({Cent_col_ClustDouble{1},PosClustDouble{1}});
1 Comment
Rik
on 16 Aug 2024
Edited: Rik
on 16 Aug 2024
Just a minor note: you may want to flip the direction of the loops that create the new variables to automatically ensure they are the correct size. Otherwise you might have left-over variables (when using a script during debugging) with the previous values:
% replace this
for n=1:numel(Cent_col_Clust)
Cent_col_ClustDouble{n} = im2double(Cent_col_Clust{n});
end
% with this
Cent_col_ClustDouble = cell(1,numel(Cent_col_Clust));
for n=1:numel(Cent_col_Clust)
Cent_col_ClustDouble{n} = im2double(Cent_col_Clust{n});
end
% or this
clear Cent_col_ClustDouble
for n=numel(Cent_col_Clust):-1:1
Cent_col_ClustDouble{n} = im2double(Cent_col_Clust{n});
end
The optimal solution to this potential issue is to use functions. Functions provide a stable interface that you can document. When you have improvements for your function, it also allows you to have a drop-in replacement.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!