Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

How to get frames from a video

MATLAB CODE to get images from video:
To extract frames from a video.
              Frames can be obtained from a video and converted into images. To convert a video frame into an image, the MATLAB function ‘frame2im’ is used.  
                                      To read a video in avi format, the function ‘aviread’ is used.  The original format of the video that I am using as an example is .gif file format. I converted the .gif file format image into an avi format video. Learn how to convert a gif image into a video...
 For instance, in the ‘fun.avi’ video I am interested in the frame number 58. So I am converting the 58th frame into an image.
MATLAB CODE:
%How to get frames from a video

mov=aviread('fun.avi');

Im=frame2im(mov(1,58));
figure,imshow(Im);


Frame 58
The above process can be done using 'mmreader' function also.

MATLAB CODE:

Vobj=mmreader('fun.avi');

Im=read(Vobj,58);
imshow(Im);

After playing the video, I thought of modifying some frames. So I converted the frames from 58 to 64 into images and added a smiley to the images by performing image arithmetic operations.

Then I converted those images into frames and saved as a video file in ‘smiley.avi’.

Lmov=aviread('smiley.avi');

MIm=frame2im(Lmov(1,1));
figure,imshow(MIm);

Modified Frame 58


%Preallocating the structure array
frame=struct('cdata',1,'colormap',cell([1 91]));


I added the modified frames to the original video.


%Modify the video in the middle
frame(1,1:57)=mov(1,1:57);
frame(1,58:87)=Lmov(1,1:30);
frame(1,88:91)=mov(1,64:67);
implay(frame,5);












  If we are interested in playing only some of the frames in the original video then, we can view those frames alone. Here I am interested in viewing the frames from 27 to 33.


%Part of the video
scz=figure;
set(scz, 'position', [300 100  size(Im,1)+130 size(Im,2)-100]);
FRange=[1 27:33];
%Here 1 is for the number of times the video has to be repeated
%27:33 indicates the frames from 27 to 33.
movie(scz,mov,FRange,5);

We can also save the desired frames into a file. An avifile object is created with the file name ‘VidPart.avi’. Then the desired frames from 27 to 33 are added by using the syntax ‘addframe’.


%To save the desired part alone

obj=avifile('VidPart.avi','FPS',5);
obj=addframe(obj,mov(27:33));
obj=close(obj);
like button Like "IMAGE PROCESSING" page

15 comments:

Anonymous said... Reply to comment

Nice article. Thanks for your tips, I really appreciate it.

Anonymous said... Reply to comment

Hi,
Thanks for your tips, I really appreciate it.
If you need a free video player, check this link: Best Free Video Player

Neha Masand said... Reply to comment

ok thanks....this article was very useful
but i hav one doubt....
what if i want to extract the images and save in a seprate folder on the hard disk ??

Aaron Angel said... Reply to comment

@RG Masand

Use 'imwrite' function to store the images separately in a folder.

navya said... Reply to comment


Hi there, awesome site. I thought the topics you posted on were very interesting.
Image Processing India

Unknown said... Reply to comment

Index exceeds matrix dimensions.
im gettin this error wht t do?

Unknown said... Reply to comment

Index exceeds matrix dimensions.
im grttin this error while running frame2im wht t do?

Aaron Angel said... Reply to comment

@Suhan Das
kindly check whether you are using 'frame2im' for one frame only or if you are using looping concept then check for the index range. If you are still getting the error, mail me with details.

Happy Reading!

Unknown said... Reply to comment

what if i want to extract the images and save in a seprate folder on the hard disk ??

Unknown said... Reply to comment

what if i want to extract the images and save in a separate folder on the hard disk ??
plz give an example of 'imwrite' function.. thankyou

Unknown said... Reply to comment

Nice.. thanks

Unknown said... Reply to comment

nice thanks

Unknown said... Reply to comment

`How to display all frames simultaneously ????

Unknown said... Reply to comment

how to display all frames simultaneously...???

Unknown said... Reply to comment

i need code for Unusual Event Detection in Low Resolution
Video for enhancing....

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com