Hello all, I have been having a lot of trouble trying to export a video from Matlab from a 3D matrix. The matrix dimensions indicate frame height, width and frame number. The element values are what is being used to form images with commands such as imagesc or imshow. I can create a video in Matlab by putting the imshow or imagesc command in a for loop but I want to extract something equivalent from Matlab to put in youtube or imbed in a powerpoint or soemthing. Here's an example of the situation:
IM % an mxnxz matrix
for i = 1:size(IM,3);
imshow(IM(:,:,i))
pause(.001);
end
Since the matrix has no color information after processing I have run into problems using WriteVideo or im2frame command. Thanks for any help.

 Accepted Answer

Stephen23
Stephen23 on 24 Feb 2017
Edited: Stephen23 on 24 Feb 2017
It is easy to use getframe and videoWriter object. Here is an example from the videoWriter object help:
v = VideoWriter('peaks.avi');
open(v);
Generate initial data and set axes and figure properties.
Z = peaks;
surf(Z);
axis tight manual
set(gca,'nextplot','replacechildren');
Create a set of frames and write each frame to the file.
for k = 1:20
surf(sin(2*pi*k/20)*Z,Z)
frame = getframe;
writeVideo(v,frame);
end
close(v);

3 Comments

Andrew Chen
Andrew Chen on 24 Feb 2017
Edited: Andrew Chen on 24 Feb 2017
Thanks for the response! Is there some easy way to iterate the VideoWriter command? What I mean by this is that I can see a "video" in Matlab by sequentially showing the frames by useing imshow in a for loop as shown above. The Video Writer has an example where the video object is opened and a .jpeg is written and the video object is closed:
A = imread('peppers.png');
% Create a |VideoWriter| object for a new uncompressed AVI file for RGB24 video.
v = VideoWriter('newfile.avi','Uncompressed AVI');
% Open the file for writing.
open(v)
% Write the image in |A| to the video file.
writeVideo(v,A)
% Close the file.
close(v)
Is it as easy as opening and closing the video pbject in the for loop I wrote above? Would I close the video object after the for loop is completed? Thanks for your time.
I Just saw your edits, I'll give it a try! Thanks!
Thanks! It worked!

Sign in to comment.

More Answers (1)

Guangxu Li
Guangxu Li on 9 Mar 2020
Edited: Guangxu Li on 9 May 2020

1 vote

Video to 3D image (single channel)
filename="./*.avi";
obj = VideoReader(filename);
vid = read(obj);
mImage=obj.Width;
nImage=obj.Height;
frames = obj.NumberOfFrames;
Image3D=zeros(nImage,mImage,frames,'uint8');
for x = 1 : frames
Image3D(:,:,x)=vid(:,:,1,x);
end
niftiwrite(Image3D,filename);

2 Comments

Hi Guangxu Li ,
Thankyou for your answer. I have a question. Why we should use rote90 in Image3D=rot90(Image3D); ?
I can`t understand the reason.
many thanks
Dear Bahmanabasdi,
I am very sorry. Please ignore the rotation and flipud lines. That is for my case.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!