Please refer to 【http://people.cs.pitt.edu/~kovashka/cs1699/】
OK, this is my first assignment in CS1699. Although CS1699 is an undergraduate course, I find it informative and well designed course. During the class in the first week, I learned how to load an image into memory(imread), how to transform a RGB 3-channels color image into a gray-scale image(rgb2gray), how to transform a matrix in to a vector(reshape), how to find pixels satisfied some conditions(find), how to transform indices into subscripts(ind2sub), and so on so forth.
I have attached the codes written in MATLAB as below. Requirements in this assignment are quote as comments.
% Part I: Matlab Basics (15 points)
% Generate a 1000000x1 (one million by one) vector of random numbers from a Gaussian distribution with mean approximately 0 and standard deviation approximately 5. Use Matlab's randn function.
v = 5 * randn(1000000, 1);
% Add 1 to every value in this list, by using a loop. To get the number of loops you need to run, use Matlab's size function. Time this operation, print the number, and write the total time taken to add 1 to every number in your answer sheet.
[r, c] = size(v);
for i = 1: r
v(i, 1) = v(i, 1) +1;
t_val = toc;
% Now add 1 to every value in the list, without using a loop. Time this operation, print the time and write it down. Use a different function to print the number than you did above.
v = v + 1;
t_val = toc;
% Read in this image into Matlab as a matrix, and write down its dimensions.
im = imread('Downtown_Pittsburgh_from_the_North_Shore.png');
% Convert the image into grayscale.
im_gray = rgb2gray(im);
% Find the darkest pixel in the image, and write its value in your answer sheet. Hint: Convert to a vector first, and use Matlab's ind2sub function.
[r, c] = size(im_gray);
im_vec = reshape(im_gray, 1, r * c);
px_val = min(im_vec)
[i, j] = ind2sub(size(im_gray), find(im_gray == px_val))
% Consider a 31x31 square (a square with size 31 pixels) that is centered on the darkest pixel. Replace all pixels in that square with white pixels.
sq_size = 31
canvas = im;
for a = i - floor(sq_size / 2) : i + floor(sq_size / 2)
for b = j - floor(sq_size / 2) : j + floor(sq_size / 2)
for c = 1 : 3
canvas(a, b, c) = 255;
% Make a new figure, display the modified image and save it as a file. Use saveas(gcf, '[filename].png') to save your image.
'press any keys to continue...'
Results can be find here: