背景帧循环matlab

Background frame loop matlab

我正在尝试创建一个循环,以便对 matlab 中的许多 .mov 文件执行相同的操作。我现在拥有的代码如下所示:

close all
clear all 
clc

movFiles = dir('*.mov');
numFiles = length(movFiles);
mydata = cell(1,numFiles);

% mydata = zeros(numFiles);
for k = 1:numFiles
    mydata{1,k} = VideoReader(movFiles(k).name);
end


for k=1:numFiles
bk_downsample = 5; %The downsample factor for frame averaging 
%disp('Opening video...') %lower number =longer computation time
vob = mydata; 
frame = vob.read(inf); %Reads to end = vob knows the number of frames
vidHeight = vob.Height;
vidWidth = vob.Width;
nFrames = vob.NumberOfFrames;

%% First-iteration background frame
background_frame = double(frame*0);
disp('Calculating background...')
for k = 1:bk_downsample:nFrames
    background_frame = background_frame + double(read(vob, k));
    disp(k/(nFrames)*100)
end

%background_frame = uint8(bk_downsample*background_frame/(nFrames));
background_frame = bk_downsample*background_frame/(nFrames);

%imshow(background_frame)

%% Second-iteration background frame
%This section re-calculates the background frame while attempting to
%minimize the effect of moving objects in the calculation

background_frame2 = double(frame*0);
pixel_sample_density = im2bw(double(frame*0));
diff_frame = double(frame*0);
stream_frame = diff_frame(:,:,1);
bk_downsample = 10;

figure
hold on
for k = 1:bk_downsample:nFrames
    diff_frame = imabsdiff(double(read(vob, k)), background_frame);
    diff_frame = 1-im2bw(uint8(diff_frame),.25);
    pixel_sample_density = pixel_sample_density + diff_frame;
    stream_frame = stream_frame + (1-diff_frame)/(nFrames/bk_downsample);
    nonmoving = double(read(vob, k));
    nonmoving(:,:,1) = nonmoving(:,:,1).*diff_frame;
    nonmoving(:,:,2) = nonmoving(:,:,2).*diff_frame;
    nonmoving(:,:,3) = nonmoving(:,:,3).*diff_frame;
    background_frame2 = background_frame2 + nonmoving;
    %pause
    disp(k/(nFrames)*100)
end

background_frame2(:,:,1) = background_frame2(:,:,1)./pixel_sample_density;
background_frame2(:,:,2) = background_frame2(:,:,2)./pixel_sample_density;
background_frame2(:,:,3) = background_frame2(:,:,3)./pixel_sample_density;


imshow(uint8(background_frame2))
%imshow(stream_frame)
   filename = ['Ring_' num2str(k) '_background_' num2str(img) '.jpg'];
imwrite((uint8(background_frame2)),filename)
end

我知道是vob=mydata开头的错误;但我不确定如何纠正它,希望有人能够帮助我,因为它会在我的数据分析中节省我很多时间。 祝你有美好的一天! :)

您的代码没有多大意义...您正在创建元胞数组:

mydata = cell(1,numFiles);
%// . . .
        mydata{1,k} = . . .

但是您尝试像结构一样访问它:

vob = mydata; 
frame = vob.read(inf);

如果我猜的话,那么你的错误源于你忘记在元胞数组中建立索引,即:

vob = mydata{k};

我在您的代码中注意到的其他编程怪事是您在两个嵌套的 for 循环中使用相同的循环变量 k,外部循环在 k=1:numFiles内部的在 k=1:bk_downsample:nFrames 上;不要这样做,除非你在弄清楚为什么你的 for 循环只执行一次时试图把自己逼疯。将它们命名为 k1 用于外循环,将它们命名为 k2 用于内循环,你会更快乐。

我不是视频处理方面的专家,但对我来说你的台词应该是:

vob=mydata{1,k};

这就是出现该错误的原因,因为您将结构的单元格视为单个结构。