How to process large video in Matlab with for loop and without memory error

前端 未结 2 2024
甜味超标
甜味超标 2021-01-16 08:55

I\'m new to Matlab processing, and I would like to read and process a large video (more than 200k frames) inside a \"for loop\" (or without it). In particular, i would like

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 09:30

    Here is my version:

    mov = VideoReader('movie.avi');
    nFrames = mov.NumberOfFrames;
    
    len = 1000;     %# epoch length
    step = 3;       %# step size
    
    %# indices of each epoch
    indices = bsxfun(@plus, 1:step:len, (0:ceil(nFrames/len-1))'*len);   %#'
    indices = num2cell(indices,2);
    indices{end}(indices{end}>nFrames) = [];
    
    %# loop over each epoch
    corr_coef = cell(size(indices));
    for e=1:numel(indices)
        %# read first image in epoch
        img1 = read(mov, indices{e}(1));
        img1 = rgb2gray(img1);            %# instead of im2bw(img1, graythresh(img1))
    
        %# read rest of images in epoch
        corr_coef{e} = zeros(1,numel(indices{e})-1);
        for f=2:numel(indices{e})
            img2 = read(mov, indices{e}(f));
            img2 = rgb2gray(img2);
    
            %# compute corr2 between the two images
            corr_coef{e}(f-1) = corr2(img1,img2);
        end
    end
    

    The cell array corr_coef contains the correlation coefficients in each epoch, where each cell contains a vector corr_coef{e}(i) of corr2 between the first frame and the (i+1)-th frame.

    Note that if one of the frames is constant (all black for example), the 2D correlation coefficient is simply NaN (zero divided by zero in the formula‌​)

提交回复
热议问题