CS1315: Introduction to Media Computation Referencing pixels directly by index number.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

CS2984: Introduction to Media Computation Drawing directly on images.
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 102 Computers In Context (Multimedia)‏ 02 / 25 / 2009 Instructor: Michael Eckmann.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
CS 1 with Robots Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
Computer Science 101 Introduction to Programming with Pictures.
+ Introduction to Programming My first red-eye removal.
 1 Loop and Set operations. Creating a loop Loops are used to repeat a computation. Let’s see the example of factorial computation. Let’s compute 10!
COMPSCI 101 Principles of Programming Lecture 27 - Using the Canvas widget to draw rows and columns of shapes.
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.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
Manipulating Pixels by Range and More on Functions.
CS2984: Introduction to Media Computation Using Loops for Pictures Conditionals Copying 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.
Program Design and Debugging. How do programmers start? How do you get started with a program? “Programming is all about debugging a blank piece of paper.”
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
CS1315: Introduction to Media Computation How to design and debug a program: Top-down, bottom-up, and debugging. Using background subtraction and chromakey.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
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: Advanced Image Manipulations, e.g., changing.
Chapter 6: Modifying Pixels by Position
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
Copyright © Curt Hill Further Picture Manipulation Considering position.
CSC 112Introduction to Media Computation 1 Rotating Images.
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.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
1 CS 177 Week 4 Recitation Slides for Loop if statement and range.
CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified.
I/O Streams File I/O 2-D array review
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Multimedia Summer Camp
Week 2.
Lab 9 Intro to Robots.
CS1315: Introduction to Media Computation
CS150 Introduction to Computer Science 1
Working with ranges in pictures
CS1315: Introduction to Media Computation
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Chapter 4: Modifying Pixels in a Range
Agenda – 1/31/18 Questions? Group practice problems
Practice with loops! What is the output of each function below?
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
CS150 Introduction to Computer Science 1
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Chapter 15: Topics in Computer Science: Functional Programming
CS 177 Week 3 Recitation Slides
CSC1401 Viewing a picture as a 2D image - 2
CS 101: Introduction to Computing
CS1315: Introduction to Media Computation
Chapter 4: Modifying Pixels in a Range
CS1315: Introduction to Media Computation
CSC1401 Manipulating Pictures 2
Presentation transcript:

CS1315: Introduction to Media Computation Referencing pixels directly by index number

Remember that pixels are in a matrix Matrices have two dimensions: A height and a width We can reference any element in the matrix with (x,y) or (horizontal, vertical)  We refer to those coordinates as index numbers or indices We sometimes want to know where a pixel is, and getPixels doesn’t let us know that.

Tuning our color replacement If you want to get more of Barb’s hair, just increasing the threshold doesn’t work  Wood behind becomes within the threshold value How could we do it better?  Lower our threshold, but then miss some of the hair  Work only within a range…

Introducing the function range Range returns a sequence between its first two inputs, possibly using a third input as the increment >>> print range(1,4) [1, 2, 3] >>> print range(-1,3) [-1, 0, 1, 2] >>> print range(1,10,2) [1, 3, 5, 7, 9]

That thing in [] is a sequence >>> a=[1,2,3] >>> print a [1, 2, 3] >>> a = a + 4 An attempt was made to call a function with a parameter of an invalid type >>> a = a + [4] >>> print a [1, 2, 3, 4] >>> a[0] 1 We can assign names to sequences, print them, add sequences, and access individual pieces of them. We can also use for loops to process each element of a sequence.

We can use range to generate index numbers We’ll do this by working the range from 1 to the height, and 1 to the width But we’ll need more than one loop.  Each for loop can only change one variable, and we need two for a matrix

Working the pixels by number To use range, we’ll have to use nested loops  One to walk the width, the other to walk the height def increaseRed2(picture): for x in range(1, getWidth(picture)): for y in range(1, getHeight(picture)): px = getPixel(picture,x,y) value = getRed(px) setRed(px, value*1.1) Bug Alert: Be sure to watch your blocks carefully!

What’s going on here? def increaseRed2(picture): for x in range(1,getWidth(picture)): for y in range(1,getHeight(picture)): px = getPixel(picture,x,y) value = getRed(px) setRed(px,value*1.1) The first time through the first loop, x is the name for 1. We’ll be processing the first column of pixels in the picture.

Now, the inner loop Next, we set y to 1. We’re now going to process each of the pixels in column 1. def increaseRed2(picture): for x in range(1,getWidth(picture)): for y in range(1,getHeight(picture)): px = getPixel(picture,x,y) value = getRed(px) setRed(px,value*1.1)

Process a pixel With x = 1 and y = 1, we get the leftmost pixel and increase its red by 10% def increaseRed2(picture): for x in range(1,getWidth(picture)): for y in range(1,getHeight(picture)): px = getPixel(picture,x,y) value = getRed(px) setRed(px,value*1.1)

Next pixel Next we set y to 2 (next value in the sequence range(1,getHeight(picture)) def increaseRed2(picture): for x in range(1, getWidth(picture)): for y in range(1, getHeight(picture)): px = getPixel(picture,x,y) value = getRed(px) setRed(px,value*1.1)

Process pixel (1,2) x is still 1, and now y is 2, so increase the red for pixel (1,2) We continue along this way, with y taking on every value from 1 to the height of the picture. def increaseRed2(picture): for x in range(1, getWidth(picture)): for y in range(1, getHeight(picture)): px = getPixel(picture, x, y) value = getRed(px) setRed(px,value*1.1)

Finally, next column Now that we’re done with the loop for y, we get back to the for loop for x. x now takes on the value 2, and we go back to the y loop to process all the pixels in the column x=2. def increaseRed2(picture): for x in range(1, getWidth(picture)): for y in range(1, getHeight(picture)): px = getPixel(picture, x, y) value = getRed(px) setRed(px,value*1.1)

Replacing colors in a range def turnRedInRange(): brown = makeColor(57,16,8) file=r"C\Documents\mediasources\barbara.jpg" picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture) Get the range using MediaTools

Walking this code Like last time:  Don’t need input  same color we want to change  same file make a picture def turnRedInRange(): brown = makeColor(57,16,8) file=r"C\Documents\mediasources\barbara.jpg" picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture)

The nested loop Used MediaTools to find the rectangle where most of the hair is that we want to change def turnRedInRange(): brown = makeColor(57,16,8) file=r"C\Documents\mediasources\barbara.jpg" picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture)

Scanning for brown hair def turnRedInRange(): brown = makeColor(57,16,8) file=r"C\Documents\mediasources\barbara.jpg" picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color, brown) < 50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture) We’re looking for a close-match on hair color, and increasing the redness Similar to scanning whole picture We could raise threshold now. (Why?…)

Could we do this without nested loops? Yes, but only with a complicated if statement Moral: Nested loops are common for 2D data def turnRedInRange2(): brown = makeColor(57,16,8) file=r"C:\Documents \mediasources\barbara.jpg" picture=makePicture(file) for p in getPixels(picture): x = getX(p) y = getY(p) if x >= 70 and x < 168: if y >=56 and y < 190: color = getColor(p) if distance(color,brown)<100.0: redness=getRed(p)*2.0 setRed(p,redness) show(picture) return picture