I have an issue with Matlab r2015a. When I run this code, it take 1.7 secondes on my computer
clc;
clear all;
close all;
tic
Video = VideoReader('test_E.avi');
for k = 1:1:100
GrayImage0 = rgb2gray(read(Video, k));
end
imshow(GrayImage0);
elapsedTime = toc;
But when I run this other, it take 14.9 secondes:
clc;
clear all;
close all;
tic
Video = VideoReader('test_E.avi');
for k = 1:1:100
GrayImage0 = rgb2gray(read(Video, k));
GrayImage1 = rgb2gray(read(Video, k+1));
end
imshow(GrayImage0);
elapsedTime = toc;
I was expected just 3.4 seconds (just one more image to read for each loop). I am a newbie, I hope someone would help me to solve this issue... the video file can be found here : https://drive.google.com/file/d/0B6Kk7k9hLvjlbzZuQlJfRlpmZXM/view?usp=sharing
With a typical video codec you can't decode frames individually, decoding always starts at a keyframe. Let's assume keyframes are 1,11,21,... While the real strategies are probably a little more advanced, just assume the first 10 frames are decoded at once and put into a cache, assuming that frame 2 to 10 are needed slightly after reading frame 1. This means you get 10 frames at a price of processing 1 keyframe and 9 difference frames.
Now your loop, for the first iteration frame 1 and 2 is read and probably removed from the cache (who expects someone to read the same frame twice?). Now you read frame 2 and 3, 2 is not found so decoding has to start over again. For the 10th iteration where you read frame 10 and 11 it's the worse. The codec probably dropped frame 10 earlier so frame 1 to 10 are decoded again. Making this single last loop more expensive than reading full 10 frames in order, 2 keyframes and 9 difference frames are processed.
To summarize: Try to stick to the use-case which video codes are optimized for, read the frames in order whenever possible and store the frames on your side:
Video = VideoReader('test_E.avi');
GrayImage1 = rgb2gray(read(Video, 1));
for k = 1:1:100
GrayImage0 = GrayImage1;
GrayImage1 = rgb2gray(read(Video, k+1));
end
imshow(GrayImage0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With