imwrite error for saving contents of a cell array in a for loop - "colourmap should have 3 columns"?

5 visualizaciones (últimos 30 días)
I am trying to save a cell array containing figures from a batch image processing pipeline (read in and processed in a for loop where k is the number of iterations) but I keep getting an error in the imwrite line, saying that "the colormap should have three columns". Could anyone shed any light on this or help me with my imwrite code?
After the current image has been processed at the end of the for loop, I have:
fig = gcf;
F = getframe(fig); % convert the figure to a frame
[figureIm, map] = frame2im(F); % convert the frame to an image w/ colourmap
figure{k} = figureIm;
end
%% Save figures
cd 'C:\MATLAB\Results\Figures\'; % would rather specify as a variable and concatenate below rather than having to change the cd
figure2 = cellfun(@im2double, figure, 'UniformOutput', false); % change the data format to double (neccessary?)
for count = 1:length(figure)
imwrite(figure2{1},map, sprintf(char(strcat(['Figure_'],[imageName(count)])))); % this line looks hideous because I've struggled with accepted data formats
end
Thanks very much,

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 3 de Dic. de 2020
Editada: Ameer Hamza el 3 de Dic. de 2020
Why are you getting the indexed image in frame2im(). Getting an RGB image will make things easier. Change the line to
figureIm = frame2im(F); % convert the frame to an image w/ colourmap
Then inside for-loop, write
filename = sprintf('Figure_%s.png', imageName(count))
imwrite(figure2{count}, filename);
  2 comentarios
Lydia Bradley
Lydia Bradley el 3 de Dic. de 2020
Thank you! All I had to do was change the line to figureIm = frame2im(F)!
Image Analyst
Image Analyst el 3 de Dic. de 2020
Regarding your wise wish about not using cd, see the FAQ.
Here is a better way:
%% Save figures
folder = 'C:\MATLAB\Results\Figures\'; % would rather specify as a variable and concatenate below rather than having to change the cd
for count = 1 : numImages
figureIm = whatever...............
baseFileName = sprintf('Figure %d.png', count);
fullFileName = fullfile(folder, baseFileName);
imwrite(figureIm, fullFileName);
end
Do NOT use figure as the name of any variable, like your cell array. Very bad idea since figure is an important built in function that you don't want to blow away!

Iniciar sesión para comentar.

Más respuestas (1)

Mathieu NOE
Mathieu NOE el 3 de Dic. de 2020
hello Lydia
I'm not an expert in image processing , but my lille modified code seems to work
a few things I know :
  • no need to convert to double if your output format (for imwrite) is coherent with the data type of your input images
  • in my demo , the image is an uint8 format
  • input images : If the frame contains true-color data, the MxNx3 matrix MAP is empty. that is the case in my demo, so I don't use map in the imwrite arguments
  • suggestion for another output file name and output directory handling
hope it helps !
fig = gcf;
F = getframe(fig); % convert the figure to a frame
[figureIm, map] = frame2im(F); % convert the frame to an image w/ colourmap
% NB : If the frame contains true-color data, the MxNx3 matrix MAP is empty.
figure{1} = figureIm;
dir_out = 'C:\MATLAB\Results\Figures\'; % define out (save) directory
%% Save figures
for count = 1:length(figure)
outfile = ['Figure_',num2str(count),'.png'];
imwrite(figure{count}, fullfile(dir_out,outfile),'PNG');
end

Categorías

Más información sobre Images en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by