【问题标题】:Motion History Image in MatlabMatlab中的运动历史图像
【发布时间】:2016-04-02 14:30:51
【问题描述】:

我正在做一个关于在 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

【问题讨论】:

  • 如果我提供了帮助,请考虑接受我的回答,让社区知道您不再需要帮助。祝你好运

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


【解决方案1】:

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

这是从您的 cmets 修改的代码以适应此任务:

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);

【讨论】:

  • 非常感谢您的回复。我正在使用以下代码阅读视频。我没有使用任何矩阵。看来我做错了? vid= VideoReader('PS7A1P2T1.avi'); OutVideoDir = '图像序列'; mkdir(OutVideoDir); numFrames = vid.NumberOfFrames; n=numFrames;对于 i = 1:n 帧 = 读取(vid,i);帧 = rgb2gray(帧); baseFileName = sprintf('%d.jpg', i); % 例如"1.jpg" fullFileName = fullfile(OutVideoDir, baseFileName); imwrite(帧,fullFileName);结束
  • 是的,您正在使用 VideoReader。我不知道你使用了那个界面。我稍后会修改我的答案来解决这个问题。我目前不在电脑前。
  • 好的,非常感谢您的帮助。欣赏它。
  • 我在后台减法代码中收到此错误:无法从 double 转换为单元格。 Action_Recognition 中的错误(第 85 行)fg (k,j) = 0;
  • 我正在使用此链接中的代码进行背景减法:read.pudn.com/downloads166/sourcecode/math/765783/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
相关资源
最近更新 更多