How to read video frames the fastest.
9 views (last 30 days)
Show older comments
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 Comments
OCDER
on 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
on 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{:});
Answers (2)
Jan
on 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 Comments
Álvaro Sacristán Gonzalez
on 17 Nov 2021
Instead of manually calculating the grayscale, Id suggest using the image processing toolbox function:
frame = mat2gray(frame)
3 Comments
Walter Roberson
on 18 Nov 2021
MATLAB introduced rescale() to replace mat2gray. It has a better name And is part of MATLAB itself not a toolbox
See Also
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!