Time for action – detecting a moving object in a still scene

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.

  1. First, let's load a video that does not contain a lot of motion in its entire duration. Such a video, used in a previous chapter, showed a green ball that entered a scene from the left and exited from the right after passing through a box. The video is called 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
    Time for action – detecting a moving object in a still scene
  2. Our next step is to subtract each frame from the previous and save the result in a new matrix. Since the video has 45 frames, the result will be a 44 frame video (the first frame does not have a previous one to use for the subtraction). For our convenience, we can use an equally-sized matrix and leave the first frame blank:
    >> 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
  3. Now, let's show the montage of the result. Can you predict what it will be?
    >> montage(subtracted,'Size',[5 9]) % Show montage of subtracted
    Time for action – detecting a moving object in a still scene
  4. Why don't we go one step further? Now that we have a matrix of all our frames with the detected ball standing out, we can use them to display the trajectory of the ball. How will we do it? One easy way is to add all the frames together and show them as one single image:
    >> total = sum(subtracted,4); % Addition of all frames (4-th dim.)
    >> total = mat2gray(total); % Normalize the whole matrix to [0,1] 
  5. Now we will display our result. Note that the image will be in color. Applying the mat2gray function just results in each of the channels being normalized to [0,1]:
    >> figure,imshow(total)
    Time for action – detecting a moving object in a still scene

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset