0

I am trying to make a function that takes in a movie (.AVI) and produce series of images (.TIF).

I know that I can use this in the command line:

obj = VideoReader('movie.m');
vid = read(obj);
frames = obj.NumberOfFrames;
for x = 1 : frames
    imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.tif'));
end

How do I make it into a function that inputs a movie and generate a series of tif images?

For example, I have a function that, when executed, generates "movie.avi" into the current folder. I want a function avitotif(movie.avi), that takes movie.avi and make series of tif images.

Thank you

  • I don't understand your question, what are you trying to change? Do you want to read an avi while it is written? – Daniel May 22 '14 at 20:26
  • @Daniel I want to create a function that inputs a avi file directory and produces series of tif images. For example, I have a function makemovie(x,y), that takes in x and y coordinates of a particle with time, and makes a movie of the moving particle. Running this function will give me a movie saved in the current folder. I want another function that takes that movie and make series of tif images in the same folder. – Nitsorn Wongsajjathiti May 22 '14 at 20:31

1 Answers1

0

There are two things you need to do if you want to convert this into a function:

  1. Wrap this code with a function header and specify one parameter for the input: The path of where the video is being stored. Let's call this input pathToMovie.
  2. Change the first line of the code after the function header so that it takes in a string from the input.

When you're done, save this file as avitotif.m, set your working directory to be where this file is located, then run avitotif('movie.avi'); assuming that the AVI file is also in the same directory.

Here is what the code will look like:

function [] = avitotif(pathToMovie)
obj = VideoReader(pathToMovie);
vid = read(obj);
frames = obj.NumberOfFrames;
for x = 1 : frames
    imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.tif'));
end

Now go ahead and call avitotif('movie.avi');

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • @user3590969: Took you long enough eh!? Just kidding. Thank you for accepting my answer :) – rayryeng Jun 02 '14 at 17:09
  • sorry! Thanks again! Just one more question, if you don't mind. In the path to movie, if it is simply the current working folder, how do I write it? is it just 'pwd'? Thanks once again – Nitsorn Wongsajjathiti Jun 03 '14 at 18:30
  • If it's the current working directory, you don't need to do `pwd`. Relative filenames assume the current working directory already. As such you don't need to specify it. If you did `avitotif('movie.avi');`, this looks for `movie.avi` in the current working directory already. However, FWIW, yes, the command is `pwd` to get the current working directory – rayryeng Jun 03 '14 at 19:33