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 件のコメント

OCDER
OCDER 2018 年 6 月 14 日
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 2021 年 3 月 5 日
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{:});

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2018 年 6 月 14 日

0 投票

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 2021 年 11 月 17 日

0 投票

Instead of manually calculating the grayscale, Id suggest using the image processing toolbox function:
frame = mat2gray(frame)

3 件のコメント

Walter Roberson
Walter Roberson 2021 年 11 月 18 日
rescale() is recommended now
Álvaro Sacristán Gonzalez
Álvaro Sacristán Gonzalez 2021 年 11 月 18 日
What do you mean?
Walter Roberson
Walter Roberson 2021 年 11 月 18 日
MATLAB introduced rescale() to replace mat2gray. It has a better name And is part of MATLAB itself not a toolbox

サインインしてコメントする。

製品

リリース

R2017b

質問済み:

2018 年 6 月 13 日

コメント済み:

2021 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by