2

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.

Amey Kelkar
  • 155
  • 2
  • 12
  • Do you recommend me to use OpenCV? – Amey Kelkar May 06 '15 at 18:28
  • @AmeyKelkar This can definitely be achieved in real-time with OpenCV, and I would indeed recommend it. – buzjwa May 06 '15 at 19:25
  • @Dima - Never, but if that's written by you, then I will definitely believe that it's fast! – rayryeng May 06 '15 at 19:45
  • @Dima - That statement was made **before** I saw your work :p I was drawing from my experience.... hence my statement "From experience in dealing with videos....", so yes, it's nice to be corrected every now and then :) However, the OP isn't using the Computer Vision Toolbox, and so I don't know how applicable that toolbox is going to be with their above code. – rayryeng May 06 '15 at 19:54
  • @rayreng, I think with vectorized code the OP should get a decent frame rate. – Dima May 06 '15 at 19:59
  • @Dima - OK. Let's see what happens. Until then, I'll remove my previous comments. – rayryeng May 06 '15 at 20:03

1 Answers1

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
Dima
  • 38,860
  • 14
  • 75
  • 115