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

조회 수: 5 (최근 30일)
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,

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 3일
편집: Ameer Hamza 2020년 12월 3일
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
Lydia Bradley
Lydia Bradley 2020년 12월 3일
Thank you! All I had to do was change the line to figureIm = frame2im(F)!
Image Analyst
Image Analyst 2020년 12월 3일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2020년 12월 3일
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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by