3 replaceValue.m [15 marks]

In one of your labs you worked with a mask image where the pixels of the mask were equal to 0 or 1. A mask value of 1 indicated the presence of an object of interest.

If you have more than one object of interest you can use more mask values. For example, a pixel with value 2 might indicate the presence of a second object of interest, a pixel with value 3 might indicate the presence of a third object of interest, and so on. The image below shows an example of a mask with values 0 (black = background), 1 (green = bicycle), 2 (pink = person), and 3 (white = border of an object).

Complete the following function which replaces a mask value with a different value:

function J = replaceValue(I, oldValue, newValue)
%REPLACEVALUE Replace a mask value with a new value
%   J = replaceValue(I, oldValue, newValue) returns a new
%   mask image J where the elements of J are equal to 
%   the elements of the input mask I except that
%   elements in I that are equal to oldValue are
%   replaced with newValue


end

3.1 Constraints

  • you must use a loop or loops to iterate over the elements of the matrix to solve this problem
  • the only Matlab functions you are allowed to use are size, numel, and zeros

3.2 Example usage

I = [0  0  0  0;
     0  1  1  1;
     2  2  2  0;
     0  0  0  0;
     0  3  3  1];
J = replaceValue(I, 1, 5);

In the above example J should be equal to the matrix

[0  0  0  0;
 0  5  5  5;
 2  2  2  0;
 0  0  0  0;
 0  3  3  5];