Using VideoWriter Function in Matlab to Create Video File of MRIstack

Hello, I am trying to create a .avi file to play the frames from mristack in matlab. I have been trying unsuccessfully to use the function VideoWriter to accomplish this. I know that mristack provides you with 21, 256 by 256 frames and I am not sure how to write them to a video file using VideoWriter. Do you write frame by frame into the .avi file? I tried doing that but it would still not work (it only shows as a black image for a couple seconds). This is the code I am trying.
%%Convert video into binary/monochrome
load mristack
rawData = mristack;
rawData(rawData<127) = 0;
rawData(rawData>=127) = 1;
implay(255*rawData);
trial=rawData(:,:,1);
V= VideoWriter('newtrial2.avi');
open(V);
for t=1:21
writeVideo(V,rawData(:,:,t));
end
close(V);
Sorry if this seems strikingly obvious to someone out there. But I will truly appreciate any help.

 Accepted Answer

You implay(255*rawData) but you imwrite() rawData not multiplied by 255.
I would suggest:
load mristack
rawData = mristack >= 127;
implay(rawData);
V = VideoWriter('newtrial2.avi');
open(V);
for t = 1 : size(rawData,3)
writeVideo(V, 0+rawData(:,:,t));
end
close(V);

2 Comments

Walter Roberson Thank you so much! It worked. Also I have a quick question, why do you add a 0 when doing writeVideo(V, 0+rawData(:,:,t))?
Adding 0 is a quick type conversion. The data type of rawData is logical because it is the output of a relational operator. false corresponds to the value 0 and true has value 1, but MATLAB knows that false and true are not numeric values.
MATLAB accepts two conventions for image data. The data can be represented as one of the integer data types, in which case each value is considered to be intensity in proportion to the maximum value for that data type, such as uint8(180) corresponding to relative intensity 180/255 . Or the data can be represented as floating point data, in which case it has to be relative intensity directly, 0 to 1.
So where before you were converting the 0 and 1 to the uint8 range by multiplying by 255, I took the easier approach and here convert logical(0) and logical(1) to the floating point range 0 to 1. It would have been entirely valid (and probably marginally more efficient) to have coded
writeVideo(V, double(rawData(:,:,t)) )
but a very inexpensive way to do double() of a logical value is to add floating point 0 to the logical. This would not work for the integer data types -- 0+uint8(180) would result in uint8(180) not in double(180) -- but it works for logical.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!