Search code examples
imagematlabvideoframes

how to select specific frames from a video in MATLAB?


I am working on a project about lip recognition and I have to read a recorded a video at a frame rate of 30 fps so, if I have 70 frames I need just to acquire or select a representative frame every 8 frames as the shortest video which has number of frames in the data set is of 16 frames but my problem is to adjust the for loop every 8 frames,and it can't read any frame is the problem about the video reader??so,please if you have any idea I would be grateful thanks,,

v = VideoReader('1 - 1.avi');
s = floor((size(v,4))/8);
for t =1:s:size(v,4)
img = read(s,i);
y = imresize(img,[100,120];

Solution

  • I would take the example for VideoReader and modify the code to explain -

    %%// Paramters
    sampling_factor = 8;
    resizing_params = [100 120];
    
    %%// Input video
    xyloObj = VideoReader('xylophone.mpg');
    
    %%// Setup other parameters
    nFrames = floor(xyloObj.NumberOfFrame/sampling_factor); %%// xyloObj.NumberOfFrames;
    vidHeight = resizing_params(1); %// xyloObj.Height;
    vidWidth = resizing_params(1); %// xyloObj.Width;
    
    %// Preallocate movie structure.
    mov(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap',[]);
    
    %// Read one frame at a time.
    for k = 1 :nFrames
        IMG = read(xyloObj, (k-1)*sampling_factor+1);
        %// IMG = some_operation(IMG);
        mov(k).cdata = imresize(IMG,[vidHeight vidWidth]);
    end
    
    %// Size a figure based on the video's width and height.
    hf = figure;
    set(hf, 'position', [150 150 vidWidth vidHeight])
    
    %// Play back the movie once at the video's frame rate.
    movie(hf, mov, 1, xyloObj.FrameRate);
    

    Basically the only change I have made are for 'nFrames' and the other factors revolving around it. Try changing the 'sampling_factor' and see if that makes sense . Also, I have added the image resizing that you are performing at the end of your code.