10 Calculating the center of mass of the mask
As you may have noticed in the previous lab, simple tracking algorithms are easily fooled if the background of the image contains objects that are similar to the tracked object. For example, the tracking algorithm from the previous lab behaves very erratically if the paper target does not fill the entire image. Even very sophisticated tracking algorithms can fail when the tracked object is difficult to isolate from the background.
One assumption that many tracking algorithms make is that the target does not move very much from one image to the next. If we also assume that the tracked object is approximately in the center of the image then we can restrict our search for the object to a central region of the image.
10.1 Write a function to compute the center of mass of a mask
In MATLAB, create and complete the following function:
function [cm_row, cm_col] = centerOfMass(mask)
%CENTEROFMASK Center of mass of a mask image
% [cm_row, cm_col] = centerOfMass(mask) computes the center of mass
% [cm_row, cm_col] of a mask image where only mask values in the
% central part of the mask are considered.
%
% If no mask pixels have the value 1 inside of the central part of
% the mask then the center of mass is considered to be the middle row
% and middle column of the mask (rounded up if not an integer value).
In the function, you should consider mask pixels that are in the middle of the mask as illustrated below. Suppose that you have a mask that has a certain height and width; then when you compute the center of mass of the mask, you only want to consider the mask pixels in the central part of the mask shown in red.
Your function should be similar to your answer to part (d) in the script labG
except that the function should only consider mask pixels in the central region of the mask and the function needs to correctly return the center of mass if no mask pixels are equal to 1
inside the central region of the mask.
10.2 Test your function
Test your function by finding the centers of mass of the masks you computed using your test images. The centers of mass for my three test masks are shown below:
Notice that in the third image the center of mass of the mass appears in the center of the target even though there are many white mask pixels on the left edge of the mask; this is made possible by focussing only on the central region of the mask when computing the center of mass.