问题
My project was to detect human activity through stored video clips. I have used following code in order to get the Motion History Image (MHI) of the video clip.
function 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
However, now I want to extend my project and display the Motion History Image (MHI) in real time. That is, the frames will be captured from the webcam, and as they are captured, a Motion History Image (MHI) will be displayed. How can I achieve this?
Any help will be appreciated. Thank you.
回答1:
You should definitely vectorize your loops. You can also use a 3D array for MHI
instead of a cell array. I have not tested it, but the code should look something like this:
MHI = zeros(y_max, x_max, numel(fg));
MHI(:,:,1) = fg{1} .* T;
for frameIndex = 2:length(fg)
mhi = MHI(:,:,frameIndex);
mhi(fg{frameIndex} == 255) = T;
prevMHI = MHI(:,:,frameIndex-1);
idx = prevMHI > 1;
mhi(idx) = prevMHI(idx) - 1;
end
来源:https://stackoverflow.com/questions/30083812/real-time-motion-history-image-mhi-in-matlab