Homework #2: Introduction to Images Due 4 th Week of Spring 2018 at the start of lab CSE 7, Spring 2018 Before beginning this homework, create a new Notepad++ file in your cs7sxx home directory on ieng6 to record your answers to the Questions. Save as H2_LastName.txt just like in Homework#1. STEP 0: 1. In this homework assignment, we will be working with images in MATLAB. In the most recent lab, we used the imread() function to get a three-dimensional matrix representing the image. Here are some examples illustrating how we can work with images in MATLAB (i.e., you should try to execute these commands with the appropriate setup but it is optional). The variable img will be a 3D matrix of size [height of kitten.jpg ] x [width of kitten.jpg ] x 3 (in that order). Figure 1 shows what the original image looks like. >> imshow(img); To display the matrix img as a picture, we will use the function imshow(). >> blueval = img(20, 55, 3); To access the individual elements of the matrix, we will state the variable name and three indices (or index ranges) of the matrix. In the code above, it is looking at the matrix variable img, specifically the element at the 20 th row, 55 th column, and 3 rd floor. Recall that for image matrices, the floors correspond to the red, green, or blue color values. So, to get the red or green values, we will just specify 1 or 2 (respectively) rather than 3 for the blue value (remember RGB!). Question #1: What is the code to obtain the red value of the pixel at row 20, column 30 of an image matrix called img and assign to a variable called redval? Question #2: What is the code to obtain all of the values pertaining the green values? (Hint: this means to obtain all the values in the 2nd dimension) Question #3: What is the code to obtain the blue values of the last row? >> new_img2 = img(1:end, 1:2:end, :); MATLAB allows the syntax where if we put a value in between the range, it will change the step size. For example, in the code above, we are accessing the columns from 1 to the end in steps of 2, i.e., every other column. So, we look at the variable img and look at all the rows (1:end), every other column, and all the colors. Then, we save these elements of img as a new variable called new_img2. Refer to Figure 3 for the resulting image
EXAMPLE: a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] >> a(1:4:16) (try it yourself) >> new_img3 = [img, img]; MATLAB will also allow creating a matrix of matrices as well. The code above, places the two matrices right next to each other and saves the resulting matrix new_img3 as new_img3. img img Question #4: Instead of placing a comma between the two img variables, try it with a semicolon what happens, and why? STEP 1: 1. Choose an image that you would like to work for this assignment. Preferably a mediumsized one, rather than one that takes up the entire screen or one that is too tiny. It would be fun to use something that shows your own interests, but any image is fine as long as it is appropriate for a school/workplace environment (i.e. not offensive) a. Save your image into the folder where you keep your MATLAB scripts. You should see the name of your image file appear in the file list. Also, save a copy of the original image in your cs7sxx home directory 2. Create a new script called Crop.m. a. The first line of your script should be: img = imread(filename); b. This line imports filename as a matrix of numbers which MATLAB can then interpret into an image. The matrix representing the image is called img. Notice that the script uses the variable filename, so you will need to set filename to something in the Command Window before you run the script (i.e., c. Save your script (just one line of code so far). 3. In the Command Window, run your script by typing these lines: a. Make a variable named filename and assign it a string that is the name of your file. Note that the image file that you assigned to the variable filename now appears in the Workspace. b. Now call the script, and use the imshow function to show your image. Remember that img is the variable created in Crop that is a matrix of numbers representing the picture.
>> Crop >> imshow(img); c. You should see a window open within MATLAB that shows your image. Check to see that you see your image and everything seems to be working. d. If you examine the contents of the image matrix by double-clicking it in the Workspace, you will see that it is a matrix of numbers between 0 and 255. Important! For EVERY script you write, your code must work for whatever image file is input with filename. So you should not "hard-code" (write explicitly) any values such as the size of the matrix (instead, use "end"). 4. Now that we've checked that you can open and view images, let's finish our Crop script. Open the Crop script. a. Create a variable named cropped_img that will hold a cropped image made from the bottom two thirds of the original image. b. This is done using matrix indexingcropping img will be similar to what is being done below. However, we show you how we cropped a picture in half columnwise. >> new_img1= img(1:end/2, 1:end, :); This line of code saves a portion of the matrix called img as a new matrix called new_img1. The specific portion of img that is copied is specified by the indices inside the parentheses. 1:end/2 accesses the rows starting at 1 and going to end/2, which is the top half rows of the image. 1:end accesses the columns from 1 to the end. : accesses all the floors, so all of the RGB color values. If we didn t take all 3 color values, the coloring in our image would change. How can this be adapted to crop only the last two thirds of the image instead of half? i. First, create a new variable called num_of_rows that will hold the number of rows in your image img. ii. Now that you have a variable that holds the number of rows in your image, how can you calculate the index of starting row needed for the cropped image? Store the new number of rows into a variable called new_rows. (Hint: the new image starts from 1/3 of the total rows) iii. Apply what you learned in Step 0 to crop img and store it into your variable cropped_img. (Hint: access the rows from new_rows to end)
c. Here is what the image looks like before and after the crop:
Original Cropped Image STEP 2: Note: You may get a warning that looks like: Warning: Integer operands are required for colon operator when used as index > In Crop (line 2) In this case, you need to convert a double (real) number to an integer. For example, if the height of your image is 215 rows, you won't be able to select the 107.5 rows on the right! You need an integer instead of a fractional number of rows. To do this, you can use the floor function. It takes in a number as an argument, and returns that number rounded down to an integer. You can try floor in the Command Window on simple examples to get the feeling for it: >> floor(215/2) ans =??? >> floor(2.7) ans =??? >> floor(2) ans =??? Question #5: When you are finished testing your Crop script, copy and paste all the commands into your Notepad++ document. Save the cropped_img image as cropped_img1.jpg. 1. Make another script, Squish.m that uses the original image. In the script, create a shortpic as an output variable, where shortpic contains every other row of original img. 2. Now within the same script, using the variable shortpic, create a variable squished_img that will hold a new image made up of two shortpic images placed next to each other (horizontal). Question #6: When you are finished editing your Squish script, copy all commands into
your Notepad++ document. Save the squished_img image as squished_img1.jpg. Your squished_img should look similar to this: STEP 3: 1. Make a script called Stretch.m that uses the original image. In the script, create a tallpic as an output variable, where tallpic contains every other column of original img. 2. Now within the same script, using the variable tallpic, create a variable fourgrids_img that will hold a new image made up of four tallpic images. Two stacked on top of the other two images. (Hint: How can you modify what you did in Step 2 to place the images together vertically and horizontally?) Question #7: When you are done testing your Stretch script, copy all commands into your Notepad++ document. Save the fourgrids_img image as fourgrids_img1.jpg. Your fourgrids_img1 will look something like this:
Important! In Lab #2, you learned how to comment a script using the percent sign %. Starting with Homework #2, you are expected to comment at least one of your scripts (although it is good practice to comment every script you write!) for EVERY HOMEWORK ASSIGNMENT. At least one of your scripts in every homework must include (1) a header comment including your name, the name of the script, and a description of what the script does. (2) a brief description of every function you call in this script, this includes the input and output arguments. (3) any additional comments necessary to clarify the logic you used in your code. You do NOT have to copy your comments into your Notepad++ document. Example: **Don t forget to stay within the 80 column limit! ** STEP 4: 1. Run all your scripts on the image you selected in Step #1 above, as well as kitten.jpg which is provided on website. Testing your code with two images is a way to see if your code is input-flexible. 2. Images Needed for Checkoff document: In order to get checked off, you will need 8 images in YOUR cs7sxx folder. 1) Unmodified image 1 2) Result of Crop script on image 1 (cropped_img1.jpg) 3) Result of Squish script on image 1 (squished_img1.jpg) 4) Result of Stretch on image 1 (fourgrids_img1.jpg) 5) Unmodified image 2 6) Result of Crop script on image 2 (cropped_img2.jpg) 7) Result of Squish script on image 2 (squished_img2.jpg) 8) Result of Stretch on image 2 (fourgrids_img2.jpg) You can save your images by going to File->Save As on the window that opens when you run imshow(cropped_img). PLEASE CHOOSE TO SAVE YOUR IMAGES AS JPG/jpg format. Paste the script code for all three scripts into your Notepad++ document. Clearly label the scripts as indicated in the instructions.