Tuesday, February 2, 2010

How To Use Aviread in Matlab For Read Video Files

Matlab users and working for the Video Processing toolbox you have great trick for read any format video using in Matlab. Now you can experiment and video for your results using this trick which I have given over here. As you are facing this problem that when you try to read video using this AVIREAD command for read video you will get error that use decompress video, now you not have to use other software for decompress and also not have to use Avi format of video. Because by this trick you can read some format of video on you Matlab such are .MPG, .AVI and so on.

How To Use Aviread in Matlab & Problem Using Aviread

As user of Matlab and using video precessing toolbox of Matlab you can read video using Matlab by Matlab command Aviread. Below you can see how to use Aviread in Matlab.

I=aviread('Yourvideo Name (Without Format) ');

Suppose you having video stored in your Matlab Work folder named as "video.avi" and you want to read this video then you write Matlab code like

I=aviread('video'); where I is the name of variable in which video get stored.

But this command having some problem in short while you run this code then you will getting error as shown below ::::

??? Error using ==> readavi
Unable to locate decompressor to decompress video stream

Error in ==> aviread at 64
X = readavi(info.Filename,-1);

It mean that your video is not decompress one so you have to decompress that video by using any software then after you can run you code and it will give you answer. And it only supports .Avi formats only.

Now I would like to give the full solution for read this video using Matlab. And it's really one of the best approach for read and process video in Matlab.

Requirement For read Video In Matlab

For read video in Matlab some requirement which are as follow :

1). You have to use Matlab 7.4 or higher version of it.
2). Your video be stored on Matlab Work folder with proper name and format .avi or .mpg.

If you not having this format video then download total video converter and convert video in this format and save this video in Matlab Work folder in you C:/ drive.

How To Read Any Video using Matlab

After completing this now paste below code to your matlab screen.

obj=mmreader('Video File Name With format');
a=read(obj);
frames=get(obj,'numberOfFrames');
for k = 1 : frames-1
I(k).cdata = a(:,:,:,k);
I(k).colormap = [];
end

At red line you have to provide your video which stored in work folder of Matlab with Video File Name With format. So now your video get stored in I variable now you can use it and use for further processing steps. Here you can see one demo for the same so watch and enjoy your study !!!

29 comments:

  1. Do I need to change anything in the code other than the file name.
    Becoz I got the following error on executing the codes.
    error:
    ??? The frame range requested is beyond the end of the file.

    Error in ==> mmreader.read at 67
    videoFrames = read(getImpl(obj));

    Error in ==> readmovie at 2
    a=read(obj);

    Please help me out.

    ReplyDelete
  2. No shrat nothing you have to introduce with it..
    but you confirm that your video should having extension .mpg or .avi .... so that mmreader can make it's object file and run it ....

    after all you can't got the answer then tell me .. ok

    ReplyDelete
  3. I executed the following command.

    obj=mmreader('Matlab_Test.avi');
    a=read(obj);
    frames=get(obj,'numberOfFrames');
    for k = 1 : frames-1
    I(k).cdata = a(:,:,:,k);
    I(k).colormap = [];
    end
    implay(I)

    But I am getting the same error


    error:
    ??? The frame range requested is beyond the end of the file.

    Error in ==> mmreader.read at 67
    videoFrames = read(getImpl(obj));

    Error in ==> readmovie at 2
    a=read(obj);

    I have saved the video in avi format in my matlab lab working folder only.
    Do I need to change the number of frames?
    Please help me find a solution for this.

    ReplyDelete
  4. ya...
    absolutely i helps you ...
    but first you check it out that you having matlab 7.4 or higher version then it must be run .. because i completed my project on that matlab ...
    check it our then tell me ok ..

    ReplyDelete
  5. Thanks Sagar.Actually when I tried it in my mac I got the above mentioned error.But when I tried it on my windows in which I have installed matlab 7.8 R 2009 (a).It worked perfectly.

    When I am trying the same command on windows with Matlab R 2010(b).It gave me the following error.

    Warning: The end of file was reached before the requested frames were read completely. Frames 1 through 318 were
    returned.
    ??? Undefined function or method 'implay' for input arguments of type 'struct'.

    Error in ==> readmovie at 8
    implay(I)

    Do you have idea why is it not working?
    Am I missing any file or do I need to install anything to make the implay(I) command work?

    Thanks for time.your command is really helpful.I would be happy if can give some explanation on how this command works?

    ReplyDelete
  6. Hi Sagar! Please help me out. I am able to run the code in version 7.8 but I am not able to run it in version 7.11 which is the latest one.Is it a bug in this version or Do I need to change video to any other format and change the code accordingly?

    ReplyDelete
  7. No sharat we worked on Matlab 7.8 ...
    after all it will be work on because 7.11 is the latest version of matlab...
    By the way i am not working on the Matlab till last one year because after all i leave my project then I not touch it ...

    ReplyDelete
  8. hi sagar! Thanks. I found that in the latest version the mmreader class is renamed as VideoReader.I am able to load the file but I am not able to read the file.Could you please tell me how your code works for setting the frames?

    Do you know how to find the distance between the pixels in a video?

    ReplyDelete
  9. Hey sharat ya you can read frames by using ".cdata" command ..

    in this code sinario if you want to use the first frame then the code should be
    I1=I(1).cdata;

    and other question distance i can't understand it ...
    so can you specify it please .....

    ReplyDelete
  10. Thanks Sagar.I have written a mail to support team for Matlab to find out the error with the VideoReader Code.

    The video I am loading is a sports video and I need to calculate the distance between two points (A,C) on the ground in the video. Exactly what I need is,If I select two points (A,C) on the video I need to get the distance(given that a particular points A,B a constant value.

    Thanks for all your time.

    If you help me in this,It will be really useful for my project.

    ReplyDelete
  11. Hi,

    I am using Matlab 7.8. I tryed this code and got the following message...

    obj=mmreader('mon_vidéo_1.avi');
    a=read(obj);
    frames=get(obj,'numberOfFrames');
    for k = 1 : frames-1
    I(k).cdata = a(:,:,:,k);
    I(k).colormap = [];
    end
    ??? Initialization failed. (No combination of intermediate filters could be found to
    make the connection.)

    Error in ==> mmreader.mmreader>mmreader.init at 364
    obj.MMReaderImpl = audiovideo.mmreader(fullName);

    Error in ==> mmreader.mmreader>mmreader.mmreader at 133
    obj.init(fileName);

    Can you help me?

    Best

    ReplyDelete
  12. hi henrique!

    Please check if you are having the video in the matlab workspace.I also used the same code and It worked really well.Just change the file name from what sagar has posted. It will work fine.


    @Sagar:

    I contacted the support team of the matlab and found the work around for the error I was getting in Version 7.11. Now I need to give the video name in command line so that when ever I run the M file It should ask for the video it has to play.What is the code I need to add to the existing codes.

    ReplyDelete
  13. ok sharat ....

    now best of luck for your project work ....

    ReplyDelete
  14. hi sagar,
    could u upload a sample video ie the avi file which worked for u??....

    ReplyDelete
  15. Hi Nihil ,,

    I have not it ..
    but i will try that for you only ..
    I will find it out and send you a link of it ..

    ReplyDelete
  16. Does that work with videos encoded with the huffyuv or ffv1 codec ?

    ReplyDelete
  17. can you please tell me which is the final video extension .

    ReplyDelete
  18. hi!
    i have the same problem with enrique..

    I'm currently using MATLAB 2011a (7.10)

    everytime i run the program I always get this error:

    ??? Initialization failed. (No combination of intermediate filters could be found to
    make the connection.)

    Error in ==> mmreader.mmreader>mmreader.init at 423
    obj.MMReaderImpl = audiovideo.mmreader(fullName);

    Error in ==> mmreader.mmreader>mmreader.mmreader at 133
    obj.init(fileName);

    I already checked the directory of the video...

    any idea what the problem is? thanks!

    ReplyDelete
  19. i too hav the same promblem while read... same undefined mmreader... u hav any other coding ideas for reading

    ReplyDelete
  20. obj=mmreader('sample.mpg');
    a=read(obj);
    frames=get(obj,'numberOfFrames');
    for k = 1 : frames-1
    I(k).cdata = a(:,:,:,k);
    I(k).colormap = [];
    end
    implay(I)

    while running the above code in matalab 2010a, I got error below,


    "??? Undefined function or method 'video' for input arguments of type 'char'."

    and unable to run video file please help me

    Raju

    ReplyDelete
  21. i played the video but i need a code for compressing that video.plz anyone help me

    ReplyDelete
  22. please i have a problem when i test this code the program (matlab) ask me that i havn't codec
    error: The file requires the following codec(s) to be installed on your system:
    Unknown Codec

    ReplyDelete
  23. i have a problem when i test this code
    obj=mmreader('v.avi');
    a=read(obj);
    frames=get(obj,'numberOfFrames');
    for k = 1 : frames-1
    I(k).cdata = a(:,:,:,k);
    I(k).colormap = [];
    end
    error in: The file requires the following codec(s) to be installed on your system:
    Unknown Codec
    please if any one have an idea for the name of codec or link to download it please help me

    ReplyDelete
  24. i have a pb with my avi sequence when i run this pg i get
    Warning: MMREADER will be removed in a future release. Use
    VIDEOREADER instead.
    > In mmreader.mmreader>mmreader.mmreader at 120
    Warning: Unable to determine the number of frames in this
    file.
    Undefined function 'implay' for input arguments of type
    'struct'.

    someone help!!

    ReplyDelete
  25. how can i extract a frame from video??

    ReplyDelete
  26. hi! i have some trouble running this program. it doesn't have any error but the output is not the video i expected it to be. it only shows pixelated image.

    ReplyDelete
  27. Not enough memory available for 901 frames.

    Error in ==> mmreader.read at 67
    videoFrames = read(getImpl(obj));

    need help plz

    ReplyDelete
  28. how can i compress video in matlab and decompress and convert avi file video into another format also in matlab

    ReplyDelete
  29. how can i creat video with sound in matlab and how can i compress that video and decompress and convert it to another format in matlab

    ReplyDelete