9 Masking the target
We want to isolate the part of the image that contains only the white smiley face. To do this we will first create an image mask where the elements of the mask are equal to 1
where the image is bright and equal to 0
everywhere else.
Our approach will use simple thresholding: the elements of the mask will be equal to 1
where the image gray value is greater than some threshold value and equal to 0
everywhere else.
9.1 Write a function to convert a colour to a grayscale value
In MATLAB, create and complete the following function:
function mask = makeMask(I, threshold)
%MAKEMASK Makes a mask for the LAB G target
% mask = makeMask(I, threshold) makes a mask for a grayscale image I
% of the LAB G target.
%
% The value of mask is 1 wherever I is greater than threshold and
% 0 wherever I is less than or equal to threshold.
Don’t use a loop for this function; all you need is a simple conditional statement.
9.2 Test your function
Test your function using the images you captured in the previous step. For example:
mask1 = makeMask(test1, 0.65);
figure
imshow(mask1);
figure
imshow(mask1 .* test1)
The code above computes a mask that selects all of the pixels in the image test1
that have a gray value greater than 0.65
. My masked test1
image looks like the following:
Notice that the threshold
value of 0.65
causes some pixels in the target to be missed. If we lower the threshold value to 0.5
we get a different result:
mask1 = makeMask(test1, 0.5);
figure
imshow(mask1);
My masked test1
image using a threshold
value of 0.5
looks like the following:
Notice that that the threshold
value of 0.5
causes some pixels outside of the target to be incorrectly included.
Experiment with several different values for the threshold gray level to find a threshold value that works well (creates a mask that isolates the white target). Remember what threshold value you find because you will need this value when you attempt to track the target.
9.2.1 Apply the mask to the image
You can apply the mask to the image by multiplying the elements of the mask to the image element by element. For example, using a threshold value of 0.65
:
mask1 = makeMask(test1, 0.65);
imshow(mask1 .* test1)
Applying the mask to my test1
image looks like the following: