Homework #2: Introduction to Images Due 4 th Week of Spring 2018 at the start of lab CSE 7, Spring 2018

Similar documents
Lab 4 CSE 7, Spring 2018 This lab is an introduction to using logical and comparison operators in Matlab.

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Introduction to MATLAB

CS1114: Matlab Introduction

Computer Vision. Matlab

Digital Image Processing

CS1114: Matlab Introduction

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Class #1. introduction, functions, variables, conditionals

CS100R: Matlab Introduction

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

Unix Computer To open MATLAB on a Unix computer, click on K-Menu >> Caedm Local Apps >> MATLAB.

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Spring CS Homework 3 p. 1. CS Homework 3

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

CS1114 Assignment 5, Part 1

CSC 101: PreLab Reading for Lab #4 More HTML (some of this reading on Tables and Images are based on previous writings of Prof William Turkett)

Introduction to MATLAB. CS534 Fall 2016

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Lab 1 Intro to MATLAB and FreeMat

Ascii Art. CS 1301 Individual Homework 7 Ascii Art Due: Monday April 4 th, before 11:55pm Out of 100 points

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Dr Richard Greenaway

1 Introduction to Matlab

Day 1: Introduction to MATLAB and Colorizing Images CURIE Academy 2015: Computational Photography Sign-Off Sheet

Finding MATLAB on CAEDM Computers

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

MATLAB TUTORIAL WORKSHEET

AMS 27L LAB #2 Winter 2009

Desktop Command window

How to learn MATLAB? Some predefined variables

ROSE-HULMAN INSTITUTE OF TECHNOLOGY

(Refer Slide Time 01:41 min)

Image Manipulation in MATLAB Due Monday, July 17 at 5:00 PM

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Step by step set of instructions to accomplish a task or solve a problem

APPM 2360 Lab #2: Facial Recognition

Lab 3. CSE 3, Summer 2010 In this lab you will learn and implement some basic html.

Introduction to MATLAB

EOSC 352 MATLAB Review

MATLAB Demo. Preliminaries and Getting Started with Matlab

CS/NEUR125 Brains, Minds, and Machines. Due: Wednesday, March 8

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Array Accessing and Strings ENGR 1181 MATLAB 3

R in Linguistic Analysis. Week 2 Wassink Autumn 2012

MATLAB GUIDE UMD PHYS401 SPRING 2012

CPSC 217 Assignment 3

Practical 4: The Integrate & Fire neuron

MATLAB Project: Getting Started with MATLAB

1 Getting started with Processing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Introduction to MATLAB

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

Getting started with MATLAB

An Introduction to Matlab5

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CSC 101: Lab #2 HTML and the WWW Manual and Report Lab Date: Tuesday, 2/2/2010 Report Due Date: Friday, 2/5/2010 3:00pm

Chapter 2 Fundamentals. Chapter 2 Fundamentals The images used here are provided by the authors.

Array Creation ENGR 1181 MATLAB 2

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Excel Basics Fall 2016

MATLAB Project: Getting Started with MATLAB

APPM 2460 Matlab Basics

ENGR 1181 MATLAB 05: Input and Output

Beginner s Mathematica Tutorial

New and Improved Formula Editor in JMP 13

Teaching Manual Math 2131

Intro To MATLAB. CS Fall 2013 Zach Welch

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB Introduction to MATLAB Programming

APPM 2360 Project 2 Due Nov. 3 at 5:00 PM in D2L

Array Accessing and Strings ENGR 1187 MATLAB 3

Week Two. Arrays, packages, and writing programs

Lab 7: Python Loops and Images 1

1 Overview of the standard Matlab syntax

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2

Introduction to MATLAB

McTutorial: A MATLAB Tutorial

Image Processing Toolbox

MATLAB Introductory Course Computer Exercise Session

EGR 111 Plotting Data

Matrices 4: use of MATLAB

ECE 3793 Matlab Project 1

Lab 3. A Multi-Message Reader

Introduction to Programming for Biology Research

Assignment III: Graphing Calculator

University of Alberta

EE168 Lab/Homework #1 Introduction to Digital Image Processing Handout #3

How are groceries, images, and matrices related?

Matlab Tutorial and Exercises for COMP61021

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Pointers, Arrays and Parameters

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

6.149 Checkoff 2. What to complete. Recall: Creating a file and running a program in IDLE.

Lab 8. Basic Image Processing Algorithms Fall 2017

Transcription:

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.