Search code examples
matlabimage-processingvideovideo-processingpixel

How to efficiently find video frames with all pixels in a same color in MATLAB?


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


Solution

  • 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.