Main Page

CS170A -- HW#0 -- Solution form -- Matlab

Your name: Jerry Liu

Your UID: 404474229

Please upload only this notebook to CCLE by the deadline.

Policy for late submission of solutions: We will use Paul Eggert's Late Policy: $N$ days late $\Leftrightarrow$ $2^N$ points deducted} The number of days late is $N=0$ for the first 24 hrs, $N=1$ for the next 24 hrs, etc., and if you submit an assignment $H$ hours late, $2^{\lfloor H/24\rfloor}$ points are deducted.

Problem 1: Images (30 points)

(a) color-to-grayscale transformation:
include both your function grayscale(A) and its result where A the RGB Mandrill image.

In [3]:
grayscale  =  @(A)  uint8(256 * cat(3, mean(A,3), mean(A,3), mean(A,3)));
imshow(grayscale(Mandrill))

(b) image saturation and oversaturation:
include both your function saturate(A,t) and its result where A the RGB Mandrill image, when t=0.25.

In [6]:
saturate  =  @(A, t)  t * A + (1 - t) * double(grayscale(A)) / 256;
imshow(saturate(Mandrill, 0.25))

(c) image brightening:
include both your function brighten(A,t) and its result where A the RGB Mandrill image, when t=0.25.

In [7]:
brighten  =  @(A, t)  t * A;
imshow(brighten(Mandrill, 0.25))

Problem 2: Color Models (30 points)

(a) RGB to YCbCr(R,G,B):
Prove that the result of RGB to YCbCr(R,G,B) are all in the range 0 t 255, provided R, G, and B are.

In [18]:
% The resulting Matrix looks like
% 0.29900R + 0.58700G + 0.11400B
% −0.16874R + −0.33126G + 0.50000B + 128
% 0.50000R + −0.41869G + −0.08131B + 128

% Worst case scenario, for maximum:
% line 1: R = G = B = 255
% line 2: R = G = 0, B = 255
% line 3: R = 255, G = B = 0
R = 255; G = 255; B = 255;
0.29900 * R + 0.58700 * G + 0.11400 * B
R = 0; G = 0; B = 255;
-0.16874 * R - 0.33126 * G + 0.50000 * B + 128
R = 255; G = 0; B = 0;
0.50000 * R - 0.41869 * G - 0.08131 * B + 128

% Minimum:
R = 0; G = 0; B = 0;
0.29900 * R + 0.58700 * G + 0.11400 * B
R = 255; G = 255; B = 0;
-0.16874 * R - 0.33126 * G + 0.50000 * B + 128
R = 0; G = 255; B = 255;
0.50000 * R - 0.41869 * G - 0.08131 * B + 128

% Since we use floor function, there is no problem. The range is between 0 and 255
ans =

   255


ans =

  255.5000


ans =

  255.5000


ans =

     0


ans =

    0.5000


ans =

    0.5000

(b) RGB to CMY(R,G,B):
Develop a similar kind of function RGB_to_CMY(R,G,B) for converting RGB to CMY values.

In [22]:
RGB_to_CMY = @(R, G, B) cat(3, uint8(255 - R), uint8(255 - G), uint8(255 - B));

(c) CMY Mandrill:
Show your result of RGB_to_CMY() for the Mandrill image by rendering it in RGB. (Please display the image in RGB -- with Cyan as Red, Magenta as Green, Yellow as Blue.)

In [23]:
CMY = RGB_to_CMY(uint8(Mandrill(:,:,1) * 255), uint8(Mandrill(:,:,2) * 255), uint8(Mandrill(:,:,3) * 255));
imshow(CMY)

Problem 3: Rotations (20 points)

The file rotations_and_reflections.m produces some 2x2 matrices, and shows how to define symbolic variables like $\theta$. Using symbolic values, find a 3x3 matrix for $R_{123}(\psi,\theta,\phi)$. (Hint: blkdiag might help.)

In [12]:
phi = sym('phi');
theta = sym('theta');
si = sym('si');
Rotation = @(t) [ cos(t) -sin(t) ; sin(t) cos(t) ];
% On the pdf spec, it asks for (phi, theta, si); I am using the one from the pdf spec
R_123 = blkdiag(Rotation(si), 1) * blkdiag(1, Rotation(theta)) * blkdiag(Rotation(phi), 1)
R_123 =
 
[ cos(phi)*cos(si) - cos(theta)*sin(phi)*sin(si), - cos(si)*sin(phi) - cos(phi)*cos(theta)*sin(si),  sin(si)*sin(theta)]
[ cos(phi)*sin(si) + cos(si)*cos(theta)*sin(phi),   cos(phi)*cos(si)*cos(theta) - sin(phi)*sin(si), -cos(si)*sin(theta)]
[                            sin(phi)*sin(theta),                              cos(phi)*sin(theta),          cos(theta)]

Problem 4: Slices (20 points)

(a) Global Average Temperature Anomaly:
plot the average (non-missing-value) temperature anomaly over the entire grid, for every year from 1916 to 2015.

In [6]:
GHCN = csvread('ghcn.csv');

%   The data was artificially shifted to [0, 4500];
%     its range should be [-2500, +2000]/100 = [-25,+20], in degrees Centigrade.
%     Since our focus here is on warming, we ignore temperatures below -5.
%   We omit the year and month in columns 1:2 before scaling:

GHCN_in_centigrade  = (GHCN(:,3:74) - 2500) / 100;

temperature_anomaly = reshape( GHCN_in_centigrade, [36, 12, 137, 72] );   % convert to a 4D matrix, so we can use slices

missing_values = (temperature_anomaly == -25);

my_years = 1916:2015;
temparature_nonmissing_anomaly = temperature_anomaly .*  (~ missing_values);
my_slice = temparature_nonmissing_anomaly( :, :, my_years - 1880 + 1, : );

total_number_of_grid_squares = 36 * 72 * 12;
N = total_number_of_grid_squares;

average_anomaly_by_year = reshape( sum(sum(sum( my_slice, 4),2),1), [length(my_years) 1] ) / N;


plot( my_years, average_anomaly_by_year )
xlabel('year')
ylabel('temperature anomaly -- Celsius')
title('average global temperature anomaly by year')

(b) Global Warming:
Based on your plot, give your opinion on this question: is `global warming' real?

In [ ]:
Yes