imwrite error for saving contents of a cell array in a for loop - "colourmap should have 3 columns"?
Show older comments
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
More Answers (1)
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 Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!