1

What is the efficient way (meaning using less loops and shorter run time) in MATLAB to read a video file say:vid1.wmv for example with this specifications (length: 5 min, frame width: 640, frame height: 480, frame rate: 30 frames/sec) and extract the time stamps of frames where all the pixels were in same color (say: black) with a tolerance. The following is my code which is very time-consuming. It takes around three minutes for each frame!

clear
close all
clc

videoObject = VideoReader('vid1.wmv');
numFrames = get(videoObject, 'NumberOfFrames');
all_same_color_frame=[];
for i=1:numFrames
    frame = read(videoObject,i); % reading the 10th frame
    W = get(videoObject, 'Width');
    H = get(videoObject, 'Height');
    q=1;
    for j=1:H
        for k=1:W
            rgb(q).pixl(i).frm = impixel(frame,j,k);
            q=q+1;
        end
    end
    Q=1;
    for x=1:q-1
        if std(rgb(x).pixl(i).frm)==0 % strict criterion on standard deviation
            Q=Q+1;
        end
    end
    if Q>0.9*q % if more than 90 percent of all frames had the same color
        all_same_color_frame = [all_same_color_frame i];
    end
end

Thanks in advance

Remy
  • 97
  • 11

2 Answers2

0

You can parallelize your frame for loop since there is no dependency.

It is not exactly clear what you're trying to do, but you should definitely try to compute your standard deviation (or whatever metric) per frame (in 2D) instead of collecting values into a vector each time because this will not be efficient.

Stack Player
  • 1,470
  • 2
  • 18
  • 32
0
videoObject = VideoReader('vid1.wmv');
b=[];t=[];
i=1;
while hasFrame(videoObject)
    a = readFrame(videoObject);
    b(i) = std2(a); % standard deviation for each image frame
    t(i) = get(videoObject, 'CurrentTime'); % time stamps of the frames for visual inspection
    i=i+1;
end
plot(t,b) % visual inspection

The above, is my solution using standard deviation to detect frames with the almost all pixels in the same color.

Remy
  • 97
  • 11