How to read video frames the fastest.

I want to read in a video file into matlab, find the average color of each frame and store that. I want to do to it as fast as possible.
v = VideoReader('File.mkv');
numFrames = floor(v.Duration*v.FrameRate); %Get number of frames
data = zeros(numFrames,3); %initialize data structure
parfor_progress(numFrames); %Just for tracking parfor progress
parfor cnt = 1:numFrames
frame = read(v, cnt); %Reading a frame
for i = 1:3
data(cnt,i) = mean2(frame(:,:,i)); %Average of each RGB value for each frame
end
parfor_progress; %Update parfor progress
end
parfor_progress(0) %end Parfor progress bar
toc
The limit seems to be the "read' function, I have tried using the newer 'readFrame' but I can't use parfor with it so it is ultimatly slower.

2 comentarios

OCDER
OCDER el 14 de Jun. de 2018
Just curious, how much slower/faster is the code when you use a normal for loop, and without using that parfor_progress?
Gail Distefano
Gail Distefano el 5 de Mzo. de 2021
Hi, I assume you have been successful reading the .mkv file with VideoReader. I get this error. Do you have any suggestion?
v = VideoReader('C:\users\gaila\Documents\output.mkv')
Error using VideoReader/initReader (line 734)
Unexpected exception in plug-in: 'No Frame Rate for this file Reason: The requested attribute was not found.'
Error in audiovideo.internal.IVideoReader (line 136)
initReader(obj, fileName, currentTime);
Error in VideoReader (line 104)
obj@audiovideo.internal.IVideoReader(varargin{:});

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 14 de Jun. de 2018
Replace:
for i = 1:3
data(cnt,i) = mean2(frame(:,:,i)); %Average of each RGB value for each frame
end
by
N = v.Height * v.Width; % Before the loop
...
data(cnt, :) = sum(reshape(frame, [], 3), 1) / N;
The progress might take longer than the actual processing. Do you really need it? Then reduce the number of calls, perhaps by:
if rem(cnt, 100)
parfor_progress;
end
Álvaro Sacristán Gonzalez
Álvaro Sacristán Gonzalez el 17 de Nov. de 2021
Instead of manually calculating the grayscale, Id suggest using the image processing toolbox function:
frame = mat2gray(frame)

3 comentarios

Walter Roberson
Walter Roberson el 18 de Nov. de 2021
rescale() is recommended now
What do you mean?
Walter Roberson
Walter Roberson el 18 de Nov. de 2021
MATLAB introduced rescale() to replace mat2gray. It has a better name And is part of MATLAB itself not a toolbox

Iniciar sesión para comentar.

Categorías

Más información sobre Parallel Computing Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 13 de Jun. de 2018

Comentada:

el 18 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by