HW049
Math 2984 - Fall 2017
Intro to Mathematical Problem Solving


TASK: Read a JPG file which is a photograph of a person. Make a new JPG file which only displays pixels that closely match a particular shade of the person's face.


COMMENT: In RGB real number coordinates, we think of the color red as [1.0, 0.0, 0.0]. But [0.5, 0.0, 0.0] is a related color, it's simply a lighter red, and any color of the form [s,0,0] will similarly represent some shade of red, for 0 < s <= 1.

If we think that one color [r1,g1,b1] is simply a shade of another color [r2,g2,b2], we can compare them by computing their normalized values:

        shade1 = [ r1, g1, b1 ] / ( r1 + g1 + b1 );
        shade2 = [ r2, g2, b2 ] / ( r2 + g2 + b2 };
      
If shade1 exactly equals shade2, then [r1,g1,b1] and [r2,g2,b2] are simply shades of the same color, differing in brightness.

Since we are dealing with numbers on a computer, we might prefer to allow some margin of tolerance "TOL", and say [r1,g1,b1] and [r2,g2,b2] are "essentially" shades of the same color if

        norm ( shade1 - shade2 ) < TOL
      
where "norm()" is the MATLAB function that computes the Euclidean length of a vector.

In a picture, your face may be roughly one color, but because of the effects of lighting and shadow, your face may be displayed as different shades of one color. However, if we knew the shade of just one pixel of your face, we might be able to identify all or at least many of the image pixels that represent your face.

In the graphics file "visitor.jpg", there is a person, and the dominant shade of the person's face is shade1 =[ 0.4061, 0.3105, 0.2834 ]; Suppose we have an image pixel with [r2,g2,b2] = [77, 58, 52]. Is this color close to shade1, for a tolerance of 0.01?

        rgbsum = (77+58+52)
        shade2 = [77,58,52]/rgbsum = [ 0.4118, 0.3102, 0.2781 ];
        norm ( shade1 - shade2 ) = 0.0078
      
Yes, color2 is a close enough match.

Suppose we let MATLAB read "visitor.jpg" into an MxNx3 graphics matrix A. For each pixel A(I,J), we have [r2,g2,b2] = [A(i,j,1),A(i,j,2),A(i,j,3)], and rgbsum=r2+g2+b2. We can compute shade2 and compare it to shade1. If the shades are close, we can declare a match, something like this:

        if TOL < norm ( shade1 - shade2 )
          B(I,J,1:3) = 255;  (white out) 
        else
          B(I,J,1:3) = A(I,J,1:3);  (a close match!)
      

However, it turns out that the shade matching idea doesn't work for dark colors, and so we have to add one more test:

        if TOL < norm ( shade1 - shade2 ) OR rgbsum is small
          B(I,J,1:3) = 255;  (white out) 
        else
          B(I,J,1:3) = A(I,J,1:3);  (a close, bright match!)
      


INSTRUCTIONS: Copy the file 'visitor.jpg' from the Canvas homework site. Fill in the question marks; In particular, find a value of tol between 0.00 and 0.20 that does a good job of picking out the visitor's face, and not too much else.

        A = ?            % read the file into MATLAB)
        [ m, n, k ] = ?  % get size of A.
        B = A;           % copy A.
        tol = ?;         % choose a value of tol.  You'll have to experiment.

        shade1 = [ 0.4061, 0.3105, 0.2834 ];

        for j = 1 : n
          for i = 1 : m

            rgb = double ( [ A(i,j,1), A(i,j,2), A(i,j,3) ] );
            rgb_sum = ?
            shade2 = ?

            if ( tol < norm ( shade1 - shade2 ) | rgb_sum < 550 )
              B(i,j,1:3) = 255;
            end
 
          end
        end

        imshow ( B );    % display matching pixels

        imwrite ( B, 'hw049.jpg' );  % You can save a copy of your image.
      


CHECK: You might get something like this:


SUBMIT: Your script file should be named "hw049.m", and begin with:

        % hw049.m
        % YOUR NAME
        % This script (describe what it does)
      
I do not need a copy of your plot.