How to read video frames the fastest.

9 vues (au cours des 30 derniers jours)
Codey Nacke
Codey Nacke le 13 Juin 2018
Commenté : Walter Roberson le 18 Nov 2021
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 commentaires
OCDER
OCDER le 14 Juin 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 le 5 Mar 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{:});

Connectez-vous pour commenter.

Réponses (2)

Jan
Jan le 14 Juin 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
Instead of manually calculating the grayscale, Id suggest using the image processing toolbox function:
frame = mat2gray(frame)
  3 commentaires
Álvaro Sacristán Gonzalez
What do you mean?
Walter Roberson
Walter Roberson le 18 Nov 2021
MATLAB introduced rescale() to replace mat2gray. It has a better name And is part of MATLAB itself not a toolbox

Connectez-vous pour commenter.

Catégories

En savoir plus sur Parallel Computing Toolbox dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by