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

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

Slide 1 CS 170 Java Programming 1 Testing Karel

CS1114: Matlab Introduction

Homework #1: SSH. Step 1: From the start button (lower left hand corner) type Secure. Then click on the Secure Shell File Transfer Client.

Digital Image Processing

APPM 2460 Matlab Basics

Computer Vision. Matlab

CS100R: Matlab Introduction

MATLAB Demo. Preliminaries and Getting Started with Matlab

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

function [s p] = sumprod (f, g)

Data Classes & Objects and CSV Processing. Lecture 8 - Spring COMP110

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

Homework 09. Collecting Beepers

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

CS1114: Matlab Introduction

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

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

Introduction to Scientific Computing with Matlab

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

MATLIP: MATLAB-Like Language for Image Processing

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Introduction to MATLAB

EN 001-4: Introduction to Computational Design. Matrices & vectors. Why do we care about vectors? What is a matrix and a vector?

Spring CS Homework 3 p. 1. CS Homework 3

STAT 540 Computing in Statistics

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

MATLAB Introduction to MATLAB Programming

MATLAB - Lecture # 4

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

LAB 7 Writing Assembly Code

EOSC 352 MATLAB Review

implementing the breadth-first search algorithm implementing the depth-first search algorithm

1 Overview of the standard Matlab syntax

EXAM Computer Science 1 Part 1

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

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

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

1 Introduction to Matlab

Boolean Logic & Branching Lab Conditional Tests

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

CSE 115. Introduction to Computer Science I

Matlab- Command Window Operations, Scalars and Arrays

Introduction to MATLAB

Dr Richard Greenaway

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Getting started with MATLAB

Lab - 8 Awk Programming

The Warhol Language Reference Manual

TUTORIAL MATLAB OPTIMIZATION TOOLBOX

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

Semester 2, 2018: Lab 1

1 Getting started with Processing

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

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing:

Loopy stuff: for loops

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Lesson 5C MyClass Methods. By John B. Owen All rights reserved 2011, revised 2014

Teaching Manual Math 2131

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

ENGR 1181 MATLAB 09: For Loops 2

INTRODUCTION TO LABVIEW

In the first class, you'll learn how to create a simple single-view app, following a 3-step process:

Introduction to MATLAB LAB 1

Introduction to Matlab

USER GUIDE. PowerKB for Parature

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Lecture 1. Course Overview Types & Expressions

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

6.001 Notes: Section 15.1

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

McTutorial: A MATLAB Tutorial

Scheme: Expressions & Procedures

ECE Lesson Plan - Class 1 Fall, 2001

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

7 Control Structures, Logical Statements

CSCI 3155: Homework Assignment 3

Project Data: Manipulating Bits

MATLAB TUTORIAL WORKSHEET

Desktop Command window

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

Introduction and MATLAB Basics

Programming in C++ PART 2

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

MATH 2221A Mathematics Laboratory II

Transform data - Compute Variables

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Introduction to MATLAB for Engineers, Third Edition

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

Matlab Tutorial 1: Working with variables, arrays, and plotting

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

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Computer and Programming: Lab 1

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

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

Introduction to MATLAB. CS534 Fall 2016

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

Transcription:

LEARNING OBJECTIVES: Lab 4 CSE 7, Spring 2018 This lab is an introduction to using logical and comparison operators in Matlab 1 Use comparison operators (< > <= >= == ~=) between two scalar values to create a logical value 2 Use comparison operators between a scalar and a matrix to create a matrix of logical values 3 Use matrix of logical values to index a matrix (eg select only certain pixels of an RGB image) 4 Use logical operators to combine logical expressions into compound expressions 5 Use cat() function 6 Review: o Define your own functions o Explore RGB image matrices in MATLAB o Perform advanced indexing and manipulations of RGB image matrices REMINDER: You may ask the course TA and Tutors for assistance You must complete this assignment without looking at other student s code or copying solutions from any source DESCRIPTION: In this assignment, you will both report on outcomes of different experiments, and save your MATLAB code and image as a file for turn in LAB INSTRUCTIONS: Login and set up same environment as Lab#1 (cs7sxx ieng6 home directory, Notepad++, MATLAB) Refer to Lab#1 PART ONE: REVIEW WRITING FUNCTIONS 1 Functions allow you to split code into smaller segments that are each easily understood In previous homework (HW#3), we learned how to write simple functions of our own Let us revisit what we learned 2 General Syntax of a MATLAB Function: function [outvalue1, outvalue2,, outvaluen ] = functionname (invalue1, invalue2,, invaluen) % First comment line - here you should describe what your function does % Additional lines of comments MATLAB commands end

3 Understanding the Syntax: Keyword: MATLAB functions must begin with the keyword "function" and end with the keyword end" o end terminates the function o function shows the outputs, name and input arguments of the function Output: MATLAB supports multiple output values, listed as shown in square brackets o If a function only has a single output, then the square brackets are not required o If a function does not have any output, then neither the square brackets nor the equals sign that follows are used Name: The name of the function must match the name of the m file containing the function Input: Matlab supports multiple input values, in a comma separated list within parentheses as shown o If a function takes no input values, the empty parentheses are still required * Question #1: How would the header of the function look for a function with 2 outputs and 1 input argument? No outputs and 1 input argument? 1 output and no input argument? 4 Let us now write a simple function named calctrianglearea that does the following: takes two scalar input values: b and h, representing the base and height of a triangle has one output value: area calculates the area of triangle using the given base and height store the area in output variable area 5 Once you've written the function, save the file by clicking the Save button in the Editor tab Because the filename must match the function name, you should save the file as calctriangleaream Once the file is saved, we can try calling our new function on the command line just as we would try any other built-in function: >> calctrianglearea(2, 5) Question #2: Copy and paste your entire function, including all comments, into your lab document PART TWO: INTRODUCTION TO CONDITIONALS 1 So far we have seen a number of arithmetic expressions using operators such as +, and * We can also create logical or conditional expressions using comparison and logical operators Such expressions always produce a numerical result that is either 1 for true expressions, or 0 for false expressions 2 The comparison operators are: < (less than) <= (less than or equal to) == (equal to) The Boolean operators are: >= (greater than or equal to) > (greater than) ~= (not equal to) & (and) && (and with short-circuiting) (or) ~ (not) Just like with arithmetic operators, you can use parentheses to bracket expressions to force an evaluation order For example, try typing these following example expressions on the command line: * Note: in Matlab version 2017b, empty parenthesis is not required for no input arguments

>> x = 120; >> y = 20; >> disp ( x < y); >> disp ( x < 110); >> disp ( (x > 29) & (y < 30)); >> disp((x ~= 0 ) (y > 80)); >> disp( ~(x < 24)); >> z = (x ~= y); Question #3: Copy and paste the results of these expression into your lab document Question #4: What is the value of z after you typed in z=(x==y)? Explain why you get this value Question #5: Explain what is going on in the line disp((x ~= 0) (y >80)); Explain what are the conditionals doing as well as the what the disp function does (Hint: use the help disp command) PART THREE: IMAGE MASHING USING CONDITIONALS 1 Now onto some fun stuff! Let's start by playing around with some green screen code To start with, save the following three images ( mariojpg, librarywalkjpg, originaljpg ) into your MATLAB directory 2

3 Load these 3 images into different MATLAB RGB matrices: (replace??? with correct code) mario = imread(???); original = imread(???); librarywalk = imread(???); %read in the second file and make sure it matches variable name %read in the third file 4 Now we need to put Mario in the original game Begin by creating a filter that masks out the green areas of the image using conditionals on the layers of the image filter = mario (:,:,1)<??? & mario (:,:,2)>??? & mario (:,:,3)<???; We've given you most of the code above but you will have to replace the remaining??? with appropriate values (it's ok to look at the lecture slides for help, but to test your learning you might try to puzzle it out yourself first) Remember we want this filter to select the background areas which are close to green Hint: First try replacing all??? with some numbers between 0 and 255 and see what happens It may take a few tries to get it right, so just keep playing with the values and remember what they represent Question #6: Explain what happens if you replace??? with 0 or 255? 5 So we have gathered all the pixel values that are close to green and stored them in a twodimensional matrix called filter This 2-D matrix will only have the logical values of either 0 or 1 6 Type imshow(filter) in the command window to see your filter We selected the green background around Mario to replace it with our 'original' background (the white space in the filter picture below) But Mario himself is in black because he is not green 7 You are not done yet! Let's use the filter to make Mario run in the game background 8 Next we want to create a new image based on this filter, however, notice that the filter we made has just one "layer" while the images have 3: Question #7: Describe in your words what does each layer in the image represent?

9 Since we want to use this on RGB images, we need fix this by duplicating the filter 3 times This can be done by the command: 10 The cat function takes matrices and combines them into one matrix The "3" in input arguments means that these should be combined (layered) in the third dimension (ie the layer dimension--- the 1st dimension is rows, the 2nd is columns) You don't need to deeply understand the cat function for this class, but you should get used to using this line of code, which we will use often to create RGB filters 11 Finally, we are ready to create our new image We create a mashup image by first copying all the information from mario Then, we copy in values from the original background indexed by our filter: mashup = mario; mashup(filter) = original(filter); imshow(mashup) 12 Previously we have done matrix indexing that looked like this: mashup(1:end,1:2:end, : ) That code selects only certain rows and columns of the image (in this example: every row, every other column, and all layers) The line above that includes mashup(filter) is also doing matrix indexing! But it copies only certain individual pixels instead of certain rows/columns The pixels it selects are the ones where filter has a 1 value, not a 0 Question #8: State the final values you picked for the??? parts in the filter and why 13 If you had chosen your values for the??? parts correctly above, this should result in an image resembling: Mario in original game looks like this! It s ok if you have some green on the edge of the mario If you have too much green, feel free to go back and change your values Just remember to redo all the steps again (ie using cat on filter, redoing the filter on mashup, etc)

14 Save the image above as mashupjpg Show this image during check-off 15 Now, let s make Mario run on Library Walk!! Remember to replace??? with the correct code mashup = mario; mashup(filter) = (???)(filter); imshow(mashup); Hint: Refer back to how we did the first mashup Which variable name was in the place of the???? So which variable name should we use this time? What is its role and what is this line of code doing? 16 Question #9: Copy and paste your code Explain what you replaced??? with and why 17 Save the image as moremashupjpg Show this image during check-off 18 If you've done it correctly, Mario should be running in UCSD: Mario on library walk looks like this! 19 PART FOUR: ERROR CHECKING WITH CONDITIONALS Changing gears now, we'll examine using conditionals to check for things that could cause MATLAB errors so we can avoid those errors in our code In particular, we'll look at checking matrix index boundaries (to make sure we don't get an error by indexing our matrix out-of-bounds 1 Start by creating a variable x and matrix A with The function magic(7) creates a special matrix with the property that all the rows and columns sum to the same thing (a little bit like Sudoku, but not exactly) If you typed the above command WITHOUT the semicolon, as shown above, you should see the values of the matrix and you can take a moment to check that the sum property is true Remember you can also double-click on the matrix name (A) in the Workspace pane at any time to see all the values in A

2 Let's use MATLAB to check that the sum property is true Try executing these two lines of code: The first line checks if row x sums to 175 and the second checks if column x sums to 175 Both should evaluate to true (1) You could also just type "sum(a(2,: )) == 175" of course! But we're using x for a reason you'll see in a minute Try this with a few different values of x to check other rows and other columns: This all works well assuming x is a valid value What happens if we set x = 0 and then try to evaluate the above sums? What about x = 8? Question #10: Describe the errors you get and explain what is happening We would like to add some conditional code to make sure that x is in a valid range A possibility might be An interesting thing you may not be aware of yet is that you can also use && to mean "and" test, just like & Even though they both mean "and" test, they work slightly differently Try this: One of these two versions of this conditional code results in an error Question #11: State which one results in an error and explain why What is the difference between & and && [HINT: It has to do with short-circuiting ]?

PART THREE: LAB #4 CHECKOFF CHECKLIST To receive credit for this lab you need to: Write your name on the whiteboard, when you are finished Show your TA/Tutor your Notepad++ document in your ieng6 cs7sxx home directory folder Be prepared to show the TA/Tutor all questions sufficiently answered in this document Be prepared to show the TA/Tutor the 2 images you were asked to save mashupjpg moremashupjpg Be able to answer questions about the MATLAB environment, functions, documentation and more Do not leave until you received an email autograder confirming that you have received credit It is your responsibility to make sure you get credit for each lab!