1 CS 177 Week 4 Recitation Slides for Loop if statement and range.

Slides:



Advertisements
Similar presentations
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Advertisements

01-IntroToMediaComp1 Barb Ericson Georgia Institute of Technology Oct 2010 Introduction to Computer Science and Media Computation.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
+ Introduction to Programming My first red-eye removal.
Picture Color Manipulation. Using a Loop Our first picture recipe def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
Replacing colors using if We don’t have to do one-to-one changes or replacements of color We can use if to decide if we want to make a change.  We could.
CS1315: Introduction to Media Computation Referencing pixels directly by index number.
Manipulating Pixels by Range and More on Functions.
CS2984: Introduction to Media Computation Using Loops for Pictures Conditionals Copying images.
CSC Computing with Images
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
Chapter 5: Advanced Picture Techniques (partial deck)
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
01-IntroToMediaComp1 Barb Ericson Georgia Institute of Technology Feb 2010 Introduction to Computer Science and Media Computation.
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To.
Yet Another Version. More Careful Edge Detection def lineDetect(filename): orig = makePicture(filename) makeBw = makePicture(filename) for x in range(0,getWidth(orig)-1):
CS1315: Introduction to Media Computation Using Loops for Pictures.
Chapter 3: Modifying Pictures using Loops. Chapter Learning Objectives.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Changing colors in an area Part 3: Chromakey.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
1 CS 177 Week 8 Recitation Slides JES Sound functions and Modifying Sounds Increasing/Decreasing Volume Maximizing (Normalizing) Splicing Reversing Mirroring.
CS1315: Introduction to Media Computation Transforming pictures by index number.
Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select.
1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Intro CS and Media Computation1 Barb Ericson Georgia Institute of Technology Feb 2010 Introduction to Computer Science and Media Computation.
CS1315: Introduction to Media Computation Making sense of functions.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified.
Multimedia Summer Camp
Chapter 4: Modifying Pictures using Loops
Image Processing CS177.
Introduction to Python
Week 2.
Python: Control Structures
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Functions BIS1523 – Lecture 17.
Chapter 4: Modifying Pictures using Loops
Chapter 3: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
CS1315 Introduction to Media Computation
Chapter 3: Modifying Pictures using Loops
Java Programming Loops
Agenda – 1/31/18 Questions? Group practice problems
Practice with loops! What is the output of each function below?
A Media Computation Cookbook
Fundamentals of Programming I Introduction to Digital Image Processing
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CS 177 Week 3 Recitation Slides
Chapter 3: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
Multimedia Summer Camp
CS1315: Introduction to Media Computation
CSC1401 Manipulating Pictures 2
EE 194/BIO 196: Modeling biological systems
REPETITION Why Repetition?
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

1 CS 177 Week 4 Recitation Slides for Loop if statement and range

2 Announcements EXAM 1  Wednesday 09/29  6:30p - 7:30p  EE 129

3 ANY QUESTIONS?

4 Let’s remember for Loop def decreaseRed(picture): for p in getPixels(picture): value = getRed(p) setRed(p,value*0.5)

5 What is wrong here? def decreaseRed(picture): for p in getPixels(picture): value = getRed(p) setRed(p,value*0.5) Indentation is wrong! This statement is not inside the for loop. Only the last pixel is changed.

Clearing Blue def clearBlue(picture): for p in getPixels(picture): setBlue(p,0) Again, this will work for any picture. 6

Lightening and darkening an image def darken(picture): for px in getPixels(picture): color = getColor(px) color = makeDarker(color) setColor(px,color) 7 def lighten(picture): for px in getPixels(picture): color = getColor(px) color = makeLighter(color) setColor(px,color)

Creating a negative Let’s think it through  R,G,B go from 0 to 255  Let’s say Red is 10. That’s very light red. What’s the opposite? LOTS of Red!  The negative of that would be 255 – 10 = 245 So, for each pixel, if we negate each color component in creating a new color, we negate the whole picture. 8

Creating a negative def negative(picture): for px in getPixels(picture): red = getRed(px) green = getGreen(px) blue = getBlue(px) negColor=makeColor( 255-red, 255-green, 255-blue) setColor(px,negColor) 9 negative of negative is the original picture

Converting to greyscale We know that if red=green=blue, we get grey  But what value do we set all three to? What we need is a value representing the darkness of the color, the luminance There are lots of ways of getting it, but one way that works reasonably well is really simple—simply take the average: 10

Converting to greyscale def greyScale(picture): for p in getPixels(picture): intensity = (getRed(p)+getGreen(p)+getBlue(p))/3 setColor(p,makeColor(intensity,intensity,intensity)) 11

Building a better greyscale def greyScaleNew(picture): for px in getPixels(picture): newRed = getRed(px) * newGreen = getGreen(px) * newBlue = getBlue(px) * luminance = newRed + newGreen + newBlue setColor(px,makeColor(luminance,luminance,luminance)) 12 We’ll weight red, green, and blue based on how light we perceive them to be, based on laboratory experiments.

Comparing the two greyscales: Average on left, weighted on right 13

How to save the changes? writePictureTo(picture,”filename”) Windows:  writePictureTo(picture,"E:/temp/output.jpg") MacOS  writePictureTo(picture,"/home/users/guzdial/med iasources/output.jpg") Writes the picture out as a JPEG Be sure to end your filename as “.jpg”! If you don’t specify a full path, will be saved in the same directory as JES. 14

if statement An if statement takes a logical expression and evaluates it. If it is true,the statements in if block are executed, otherwise, they are not executed. if a < 100: print "a is small“ 15 If a is 45, prints “a is small” If a is 153, does nothing

if - else statement Similarly, the logical expression is evaluated. If it is true,the statements in if block are executed, otherwise, the statements in else block are executed. if a < 100: print "a is small" else: print "a is large" 16 If a is 45, prints “a is small” If a is 153, prints “a is large”

Let’s count the red pixels in a picture def countRedPixels(picture): redCount = 0 for p in getPixels(picture): color = getColor(p) if(color == red): redCount = redCount + 1 print redCount 17

Let’s count the the non-red pixels too def countPixels(picture): redCount = 0 nonRedCount = 0 for p in getPixels(picture): color = getColor(p) if(color == red): redCount = redCount + 1 else: nonRedCount = nonRedCount + 1 print redCount print nonRedCount 18

function range Range is a function that returns a sequence If range has only one input parameter: (i.e range(input))  It generates the sequence of all the non-negative integers that are less than the input parameter value  the generated sequence starts with 0  increment is 1  the last element of the sequence is the value of input parameter – 1 >>> range(3) >>> range(1)>>> range(-1) [0,1,2] [0] [] >>> range(9) >>> range(0) >>> range(-5) [0, 1, 2, 3, 4, 5, 6, 7, 8][] [] 19

function range If two inputs (i.e range(first_input, second_input)):  It generated the sequence of all the integers that are greater than or equal to the first_input value and less than the second_input value  the first element of the sequence is the value of first_input  increment is 1  the last element of the sequence is the value of second_input – 1 >>> range(0, 3) >>> range(4, 7)>>> range(-2, 2) [0, 1, 2] [4, 5, 6] [-2, -1, 0, 1] >>> range(0, 10) >>> range(7, 4) >>> range(-2, -5) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][] [] 20

function range If three inputs (i.e. range(first_input, second_input, third_input)):  the sequence starts with the first_input value  increment is third-input  If increment is positive the sequence ends with the largest value less than second_input  If increment is negative the sequence ends with the smallest value greater than second_input >>> range(0, 3, 1) >>> range(1, 7, 2) >>> range(-5, 5, 3) [0, 1, 2] [1, 3, 5] [-5, -2, 1, 4] >>> range(0, 6, 3) >>> range(-7, -1, 2) >>> range(7, 1, -2) [0, 3] [-7, -5, -3] [7, 5, 3] 21

22 Final QUESTIONS???