How to read video frames the fastest.
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
OCDER
am 14 Jun. 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
am 5 Mär. 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{:});
Antworten (2)
Jan
am 14 Jun. 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
0 Kommentare
Álvaro Sacristán Gonzalez
am 17 Nov. 2021
Instead of manually calculating the grayscale, Id suggest using the image processing toolbox function:
frame = mat2gray(frame)
3 Kommentare
Walter Roberson
am 18 Nov. 2021
MATLAB introduced rescale() to replace mat2gray. It has a better name And is part of MATLAB itself not a toolbox
Siehe auch
Kategorien
Mehr zu Parallel Computing Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!