Search code examples
matlabvideo-processing

MATLAB return image frames from video


I have a function that processes a video and return a couple of images from it.

I am doing so by creating a new video inside the function containing the frames I want and returning the video and is the next function I read the video again to process it.

Is there any faster way to do it? For e.g. returning an array with the images and reading them?


Solution

  • You can pass the decoded frames as a 3-D array. For instance, if you have two 2-D frames frame1 and frame2, you can concatenate them along the third dimension like so:

    M = cat(3, frame1, frame2);
    

    To extract the frames from the 3-D array, just specify the third coordinate. For example, to get frame1, you write:

    frame1 = M(:, :, 1);
    

    This allows you circumvent the issue of encoding and decoding the frames between function calls, as well as prevent any loss in video quality due to successive encoding.