Matlab中的运动历史图像

时间:2015-12-28 18:18:44

标签: matlab image-processing computer-vision video-processing motion

我正在研究一个关于在matlab中使用运动历史图像进行动作识别的项目。我是这个领域的新手。我使用帧差分方法进行背景减法,以获得只有移动的人的图像。现在我想计算MHI。我找到了MHI的以下代码。我不明白什么是fg {1}以及如何使用它。任何帮助将不胜感激。谢谢。

vid= VideoReader('PS7A1P1T1.avi');
n = vid.NumberOfFrames; 

fg = cell(1, n);

 for i = 1:n
      frame = read(vid,i); 
      frame = rgb2gray(frame);
      fg{i} = frame;
 end
%---------------------------------------------------------------
%background subtraction using frame differencing method 
thresh = 25;            
bg = fg{1};           % read in 1st frame as background frame 
% ----------------------- set frame size variables ----------------------- 
fr_size = size(bg);              
width = fr_size(2); 
height = fr_size(1); 
% --------------------- process frames ----------------------------------- 
for i = 2:n 

    fr = fg{i};       % read in frame 

    fr_diff = abs(double(fr) - double(bg));  % cast operands as double to avoid negative overflow 

    for j=1:width                 % if fr_diff > thresh pixel in foreground 
        for k=1:height 
            if ((fr_diff(k,j) > thresh)) 
                fg {i}(k,j) = fr(k,j); 
            else 
                fg {i}(k,j) = 0; 
            end 
        end 
    end 

    bg = fr; 

    imshow(fg{i}) 

end 

out = MHI(fg);

// ----------------------------------------     函数MHI = MHI(fg)

% Initialize the output, MHI a.k.a. H(x,y,t,T)
MHI = fg;

% Define MHI parameter T
T = 15; % # of frames being considered; maximal value of MHI.

% Load the first frame
frame1 = fg{1};

% Get dimensions of the frames
[y_max x_max] = size(frame1);

% Compute H(x,y,1,T) (the first MHI)
MHI{1} = fg{1} .* T;

% Start global loop for each frame
for frameIndex = 2:length(fg)

    %Load current frame from image cell
    frame = fg{frameIndex};

    % Begin looping through each point
    for y = 1:y_max
        for x = 1:x_max
            if (frame(y,x) == 255)
                MHI{frameIndex}(y,x) = T;
            else
                if (MHI{frameIndex-1}(y,x) > 1)
                    MHI{frameIndex}(y,x) = MHI{frameIndex-1}(y,x) - 1;
                else
                    MHI{frameIndex}(y,x) = 0;
                end
            end
        end
    end
end

1 个答案:

答案 0 :(得分:1)

fg{1}很可能是灰度视频的第一帧。鉴于您的评论,您使用VideoReader类来读取框架。因此,单独读取每个帧,转换为灰度,然后放置在单元阵列中的单元格上。完成后,调用脚本。

以下是根据您的评论修改的代码以适应此任务:

vid = VideoReader('PS7A1P2T1.avi');
n = vid.NumberOfFrames; 

fg = cell(1, n);

for i = 1:n
   frame = read(vid,i); 
   frame = rgb2gray(frame);
   fg{i} = frame;
end

然后您可以调用MHI脚本:

out = MHI(fg);