MATLAB: Motion History Image Problem

gait recognitionmhimotion history image

Hi, my image result for mhi is bcome white and can't get any info from it. Could someone tell me what wrong is it? Thank you.
clear all
v = VideoReader('C:\Users\User\Desktop\FYP\MyMovie.avi');
ii = 1;
while hasFrame(v)
video = readFrame(v);
end
for i=1:134
% keyboard
data(:,:,i)=mean(readFrame(Video));
end
% data = uint8(255 * data);
MHI = data;
% Define MHI parameter T
T = size(data,3)+20; % # of frames being considered; maximal value of MHI
% Load the first frame
frame1 = data(:,:,1);
% Compute H(x,y,1,T) (the first MHI)
fst = MHI(:,:,1);
fst(fst>0) = T;
MHI(:,:,1) = fst;
% start global loop for each frame
for frameIndex = 2 : size(data,3)
%fprintf('Loading frame Index %d\n',frameIndex);
% Load current frame from image array
frame = data(:,:,frameIndex);
frame_pre = MHI(:,:,frameIndex-1);
%%ORIGINAL CODE
% Begin looping through each point
%{
for y = 1 : y_max
for x = 1 : x_max
if (frame(y,x) > 0)
MHI(y,x,frameIndex) = T;
else
if(MHI(y,x,frameIndex-1)>1)
MHI(y,x,frameIndex) = MHI(y,x,frameIndex-1) - 1;
else
MHI(y,x,frameIndex) = 0;
end
end
end
end
%}
%%END OF ORIGNAL CODE
MHI(:,:,frameIndex) = frame_pre - 1;
% Set of all non-zero value to T
frame(frame > 0) = T;
[y,x] = find(frame > 1);
for i = 1 : size(y,1)
MHI(y(i,:), x(i,:), frameIndex) = frame(y(i,:), x(i,:));
end
end
figure, imshow(MHI(:,:,134),[])

Best Answer

while hasFrame(v)
video = readFrame(v);
end
is going to read all of the frames of the video. There are not going to be any left for when you then
for i=1:134
% keyboard
data(:,:,i)=mean(readFrame(Video));
end
Your video is probably color, so 3 dimensional. mean() applied to a rows x cols x 3 array is going to proceed along the columns, giving you a result that is 1 x cols x 3 . Trying to store that 1 x cols x 3 matrix into data(:,:,i) which is ? x ? x 1, would fail. But since your code did not fail there, we have to guess that your image is grayscale instead of RGB; in that case mean applied to rows x cols is going to give you a result that is 1 x cols and storing that into ? x ? x 1 would work with the result being 1 x cols x 1.
Also, by default mean() outputs double, not the original data type, so data is going to be double, but probably the original range was 0 to 255 integer; the mean would be in the same range but double with the fractions between the integers being possible.
Tracing through, because data is double, frame and MHI are going to be double.
You have
frame_pre = MHI(:,:,frameIndex-1);
and then you have a bunch of code that is commented out, followed by
MHI(:,:,frameIndex) = frame_pre - 1;
Because the code in the middle is commented out, the first time it executes the result is going to be to copy the first image to the second, subtracting 1; the second iteration it would copy that second image and subtract 1 -- so getting the first frame minus 2; the third iteration you would end up with the first frame minus 3, and so on to the end, giving you first frame minus 134 or so.
Your data in your MHI array is then going to run from (0-134) to 255 with the final frame running from -134 to 121, I figure.
But then you do
frame(frame > 0) = T
where T is going to be 134+20 and frame has not had the cumulative subtraction of 1's. Probably most of the pixels are going to be non-zero at that time, but the ones that happened to be 0 are going to be left at 0 with the rest set to 154.
When you do
[y,x] = find(frame > 1);
then that is going to match the same positions in frame that were > 0 just a moment earlier, the ones that were just set to T.
And
for i = 1 : size(y,1)
MHI(y(i,:), x(i,:), frameIndex) = frame(y(i,:), x(i,:));
end
is going to set those positions to copies of what was in the frame at those positions, overwriting most of MHI there. But what is not overwritten? The positions where frame was originally 0 -- positions that might have reached as low as (-1 * frameindex) because of the cumulative subtraction. Therefore most pixels are going to be positive, but some are going to be as low as -134 because of the copying of frame to frame subtracting 1 each time.
With a bunch of mostly positive locations but also some locations that might be relatively negative, then the effect with imshow() with [] is going to be to tend to saturate the positive values, giving you white.
Related Question