For this first example, let's try to pinpoint the moving pixels in a video by a simple subtraction of consecutive frames. By subtracting two consecutive frames of a color video, the result we will get is a three-dimensional matrix of values near zero in pixels, which remain constant, and higher values in pixels that have a big alteration in their values.
singleball.avi
. Let's load it and display its montage:>> vObj = VideoReader('singleball.avi'), >> video = read(vObj); % read in all frames from video object >> montage(video,'Size',[5 9]) % Using a 5x9 grid for 45 frames
>> subtracted = zeros(size(video)); >> for i = 2:size(video,4), % For all frames but the first one % Subtract each frame from its next one subtracted(:,:,:,i) = video(:,:,:,i) - video(:,:,:,i-1); end
>> montage(subtracted,'Size',[5 9]) % Show montage of subtracted
>> total = sum(subtracted,4); % Addition of all frames (4-th dim.) >> total = mat2gray(total); % Normalize the whole matrix to [0,1]
mat2gray
function just results in each of the channels being normalized to [0,1]:>> figure,imshow(total)
So, in this simple example, you made your own motion detector for a very controlled scene. As you can see from the resulting montage, the only pixels that are not dark are the pixels that belong to the moving green ball. The first step of the process consisted of a drill that you must have been very comfortable with; we read a video, loaded all its frames into the workspace, and displayed a montage of all of them (the grid we use in montage depends on the total number of frames). Next, we initialized a zero-valued matrix to store the result of the detection and proceeded to write a
for
loop that subtracted each frame from its next one. Afterwards, we displayed a montage of all the frames of the detection result. Our final step was to display the trajectory of the ball in the frames sequence. This was accomplished by adding all the frames of the detection video together, confining the result to the range [0,1] to be treated as an image by MATLAB and then displayed it using imshow
.