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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
% 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); tic; for i = 1: r v(i, 1) = v(i, 1) +1; end t_val = toc; r, t_val % 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. tic; v = v + 1; t_val = toc; display(r); display(t_val); % Read in this image into Matlab as a matrix, and write down its dimensions. im = imread('Downtown_Pittsburgh_from_the_North_Shore.png'); size(im) % Convert the image into grayscale. im_gray = rgb2gray(im); size(im_gray) % 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; end end end % Make a new figure, display the modified image and save it as a file. Use saveas(gcf, '[filename].png') to save your image. figure, imshow(canvas); saveas(gcf, 'result.png'); 'press any keys to continue...' pause(); exit(0); |

Results can be find here: