create .gif animation with mixed subplots of grayscale and color figures
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi All,
I am running into difficulties of creating a matlab gif animation with two subplots running simultaneously, one grayscale matrix image and one running sinusoidal curve. I can get them to run separately, but don't know how to put them together using imwrite function. Below is what I have so far.
sinus curve:
T = linspace(0, 100, 101);
Y = sin(T/10*pi);
Z = cos(T/10*pi);
subplot(2,1,1)
plot(T, Y, 'LineWidth', 2)
hh1(1) = line(T(1), Y(1), 'Marker', '.', 'MarkerSize', 20, 'Color', 'b');
subplot(2,1,2)
plot(T, Z, 'LineWidth', 2)
hh2(1) = line(T(1), Z(1), 'Marker', '.', 'MarkerSize', 20, 'Color', 'r');
xlabel('time (sec)')
ylabel('P (pascal)')
% h = plot(hSubPlotAxes, NaN, NaN);
axis([min(T) max(T) min(Y) max(Y)]);
for jj = 1:length(T)
set(hh1, 'XData', T(1:jj), 'YData', Y(1:jj));
set(hh2, 'XData', T(1:jj), 'YData', Z(1:jj));
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if jj == 1;
imwrite(imind,cm,'test_sinus.gif','gif','Loopcount',inf,'DelayTime',0.01);
else
imwrite(imind,cm,'test_sinus.gif','gif','WriteMode','append','DelayTime',0.01);
end
end
and the grayscale gif movie
numImages = 20;
coronalSliceNum = 193;
viewWindow = [-300 - (1700/2), -300 + (1700/2)];
cView = zeros(414, 429, numImages);
% Load images, get coronal slices
for idx = 1:numImages;
phaseName = fullfile(imageFolder,sprintf('model_cont_phase_%d',idx));
vol_img = metaImageRead(phaseName);
cView(:,:,idx) = imrotate(squeeze(vol_img(coronalSliceNum,:,:)),90);
end
for idx = 1:numImages
% Convert to index image using window/level
frame = mat2gray(cView(:,:,idx),viewWindow);
[frameIndexed,frameMap] = gray2ind(frame,256);
% Write .gif. Set delay time here
if idx == 1
imwrite(frameIndexed, frameMap,fullfile(outputFolder,outputFilename),'gif','DelayTime', 0.15, 'LoopCount', inf);
else
imwrite(frameIndexed, frameMap,fullfile(outputFolder,outputFilename),'gif','DelayTime', 0.15, 'WriteMode','append');
end
end
any help is appreciated !!!
2 commentaires
Réponses (1)
Geoff Hayes
le 13 Juil 2015
derbruin - if you know the number of images that you have, numImages, then why not combine the two loops as
numImages = 20;
coronalSliceNum = 193;
viewWindow = [-300 - (1700/2), -300 + (1700/2)];
cView = zeros(414, 429, numImages);
T = linspace(0, 100, numImages);
% etc.
Now just have the one loop where you update the subplot with the sine curve and update the other subplot with the image. You will probably need to use a call to pause in order to halt execution temporarily so that the subplots refresh. Something like
for k=1:numImages
% update sine subplot
% update image subplot
% halt execution for half a second
pause(0.5);
end
0 commentaires
Voir également
Catégories
En savoir plus sur Animation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!