Creating movie from Images from a for loop

조회 수: 325 (최근 30일)
Robert Roy
Robert Roy 2015년 5월 29일
댓글: Marcus Lehr 2018년 8월 24일
Hi there, I am currently producing images from a for loop, however I would like put these images into a movie or slideshow,however I am struggling to do this. Any help would be appreciated.
if true
% code
end
for i=t:Images
figure(i)
B=readimx(fullfile(filename,['B000',int2str(i),'.im7']));
C=B.Frames{1}.Components{1};
V = C.Planes;
I2=V{1,1};
Array3D(:,:,i-t+1)=I2;
K=imagesc(flipud(Array3D(:,:,i-t+1)));
set(gca,'YDir','normal');
end

채택된 답변

Oliver Woodford
Oliver Woodford 2015년 5월 29일
Produce your images programmatically, rather than by exporting a figure, if you can, as it will be much faster. SC (on the FEX) is good for this. Editing Joseph Cheng's code, you would do the following:
[y, x] = ndgrid(1:256);
vidfile = VideoWriter('testmovie.mp4','MPEG-4');
open(vidfile);
for ind = 1:256
z = sin(x*2*pi/ind)+cos(y*2*pi/ind);
im = sc(z, 'hot');
writeVideo(vidfile, im);
end
close(vidfile)
  댓글 수: 1
Marcus Lehr
Marcus Lehr 2018년 8월 24일
Hi Oliver,
I'm trying to use this method however the video file I'm trying to make is of contour plots. writeVideo() gives me the following error:
IMG must be of one of the following classes: double, single, uint8
I've tried converting the data with C2xyz and saving it with SC but it hasn't helped. sc() returns
Conversion to double from cell is not possible.
Any idea how I can convert data created by contour() into a normal image file that I can write? The only workaround I've so far is saving each plot with saveas(), then converting the resulting image files to a stack with imagej. Any better solutions would be appreciated.
Thanks

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

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2015년 5월 29일
편집: Joseph Cheng 2015년 5월 29일
you can follow the example of getframe() in the documentation located here:
example:
x=1:256;
[x y] = meshgrid(x,x);
figure(1)
vidfile = VideoWriter('testmovie.mp4','MPEG-4');
open(vidfile);
for ind = 1:256
z=sin(x*2*pi/ind)+cos(y*2*pi/ind);
imagesc(z),colormap(hot)
drawnow
F(ind) = getframe(gcf);
writeVideo(vidfile,F(ind));
end
close(vidfile)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by