Matlab 中的运动历史图像
Motion History Image in Matlab
我正在从事一个关于在 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
fg{1}
很可能是灰度视频的第一帧。根据您的评论,您正在使用 VideoReader
class 来阅读框架。因此,单独读取每个帧,转换为灰度,然后放置在元胞数组中的一个元胞上。完成后,调用脚本。
这是根据您的评论修改的代码以适合此任务:
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);
我正在从事一个关于在 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
fg{1}
很可能是灰度视频的第一帧。根据您的评论,您正在使用 VideoReader
class 来阅读框架。因此,单独读取每个帧,转换为灰度,然后放置在元胞数组中的一个元胞上。完成后,调用脚本。
这是根据您的评论修改的代码以适合此任务:
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);