11 Localizing the target

The final goal of this lab is to use the Phidget to control the pan-tilt platform to move the camera so that it follows the stripe on the paper target. To accomplish this task we need to first find the location of the stripe in the video captured by the camera.

To this end we will attempt to find the left and right edges of the stripe in an image. Our approach will use the following steps:

  1. convert the middle row of the colour image captured by the camera to grayscale using the toGray function
  2. smooth the grayscale row of pixels using the the movingAverage function
  3. look for the first pixel in the the smoothed row of pixels that has a gray value less than 40; this is (hopefully) the left edge of the stripe
  4. look for the last pixel in the the smoothed row of pixels that has a gray value less than 40; this is (hopefully) the right edge of the stripe

11.1 Write a function to find the left edge of the stripe

In MATLAB, create and complete the following function:

function index = findLeftEdge(smoothGray)
%FINDLEFTEDGE Find the left edge of the target in LAB F.
%   index = findLeftEdge(smoothGray) attempts to find the left edge of the
%   target in LAB F.
%
%   The function works by finding the first index of gray where
%   the value in smoothGray falls below the value 40.
%
%   If a value of less than 40 is not found then findLeftEdge
%   returns the middle index of smoothGray (the number of elements
%   in smoothGray divided by 2).

You should use a loop and a break statement to find the index of the first value in smoothGray that is less than 40. Note that the function here is very similar to the question that you answered in part (c) when writing the script labF.

Test your function by searching for the left edge in avg:

left = findLeftEdge(avg(540, :))

In my test image, the value of left is 995. The width of the image is 1920 pixels and in my test image the left edge of the stripe is to the right of the center of the image so the value of 995 seems to make sense.

11.2 Write a function to find the right edge of the stripe

In MATLAB, create and complete the following function:

function index = findRightEdge(smoothGray)
%FINDRIGHTEDGE Find the right edge of the target in LAB F.
%   index = findRightEdge(smoothGray) attempts to find the right edge of the
%   target in LAB F.
%
%   The function works by finding the last index of gray where
%   the value in smoothGray falls below the value 40.
%
%   If a value of less than 40 is not found then findRightEdge
%   returns the middle index of smoothGray (the number of elements
%   in smoothGray divided by 2).

You should use a loop and a break statement to find the index of the last value in smoothGray that is less than 40. Note that the function here is very similar to the question that you answered in part (c) when writing the script labF.

Test your function by searching for the right edge in avg:

right = findRightEdge(avg(540, :))

In my test image, the value of right is 1204. The right edge of the stripe is less than the width of the image and it is greater than the detected left edge of the stripe so the value of 1204 seems to make sense.