Displaying a difference image as a video

1 view (last 30 days)
Hello All,
I have a video with 18000 frames in it and also a frame rate of 30 fps and I wrote a small code.
vidobj = mmreader('myvideo.asf');
lframe = read(vidobj,Inf);
numFrames = vidobj.NumberOfFrames;
vidHeight = vidobj.Height;
vidWidth = vidobj.Width;
for k=1:numFrames
sFrame = rgb2gray(read(vidobj,k);
if k<numFrames
pFrame = rgb2gray(read(vidobj,k+1);
else
pFrame = sFrame;
temp{k} = pFrame - sFrame;
end
end
Now what should I do to play all temp{k} (where k=1:numFrames) as a video ? I have tried something like this though I know it's not correct
movie(temp);
But it doesn't work!!!Apart from that, the difference image would be a binary image and I would like to plot row vs column for maximum index (pixel intensity) and display it as a video too. So at the end of my program the user should be able to see three videos 1.My original video. 2.My Difference Video. 3.My row vs column video.
Any help would be sincerely appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jun 2011
After the loop,
mymovie(1:numFrames).cdata = deal(temp{:});
mymovie(1:numFrames).map = [];
then you can
movie(mymovie)
If you want to write the movie out to disk then if you have 2010b or onwards, consider using the new VideoWriter class. Otherwise,
aviobj = avifile('MyMovie.avi');
aviobj = addframe(aviobj, temp{:});
close(aviobj);
Caution: your sFrame and pFrame are most likely of unsigned integer data type, so when you construct the difference image, the place where the second image is greater than the first would want to go negative but cannot due to the unsigned data type; negatives will be replaced by 0 in that case. That might not be what you want, so you might have to change the data type and rescale the data and then translate and rescale the difference image. There is an Image Processing Toolbox command that will change the data type and rescale for you, but I have forgotten the name of the command.
  4 Comments
Harsha Vardhan Rao  Avunoori
@Walter It's not working I have made changes to mymovie as you suggested but still I have been getting the same error "The number of outputs should match the number of inputs".
Walter Roberson
Walter Roberson on 28 Jun 2011
Ah, that is due to you only processing part of the file. Use the following line for your partial file:
[mymovie(17000:17499).cdata] = deal(temp{17000:17499});

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 28 Jun 2011
Efficiency improvement to your posted code; I will leave it to you to adapt the other changes:
vidobj = mmreader('myvideo.asf');
read(vidobj,Inf); %to count variable-rate frames
numFrames = vidobj.NumberOfFrames;
vidHeight = vidobj.Height;
vidWidth = vidobj.Width;
%no need to read every frame twice, just remember the previous frame
sFrame = rgb2gray(read(videobj,1));
for k=1:numFrames-1
pFrame = rgb2gray(read(vidobj,k+1);
temp{k} = pFrame - sFrame;
sFrame = pFrame;
end
temp{numFrames} = 0 .* sFrame;
  2 Comments
Harsha Vardhan Rao  Avunoori
@Walter...Hey Walter I thought of asking this before.Why do I always get a warning when I use mmreader 'Warning number of frames cannot be determined' and when I run the read(vidobj,Inf) it is almost taking more than 20 mins to print the number of frames ?I use a Core 2 Duo Processor,4GB RAM laptop and MATLAB 2009b.
Harsha Vardhan Rao  Avunoori
Why does it always time out when I use read(vidobj,Inf). I got this error message.
??? MATLAB:read:readTimedOut
Error in ==> mmreader.read at 74
videoFrames = read(getImpl(obj), index);
Error in ==> tetid at 5
lframe = read(vidobj,Inf); %to count variable-rate frames

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!