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

9 views (last 30 days)
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,

Accepted Answer

Ameer Hamza
Ameer Hamza on 3 Dec 2020
Edited: Ameer Hamza on 3 Dec 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 Comments
Image Analyst
Image Analyst on 3 Dec 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!

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 3 Dec 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

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!