CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified.

Slides:



Advertisements
Similar presentations
CS2984: Introduction to Media Computation Drawing directly on images.
Advertisements

Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
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.
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.
+ Introduction to Programming My first red-eye removal.
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 0.5: A Better Approach to Introductory Computer Science for Majors Bob Sloan, Pat Troy University of Illinois at Chicago SIGCSE 2008.
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach 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.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
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.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CS 100 Introduction to Computing Introduction to JES Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan.
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.
CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan,
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):
CS 0.5 A New Approach to Introductory Computer Science for Majors Supported by NSF Grant DUE
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
CS1315: Introduction to Media Computation Transforming pictures by index number.
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.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
1 CS 177 Week 4 Recitation Slides for Loop if statement and range.
Barbara Ericson Georgia Tech Sept 2005
Chapter 5: Picture Techniques with Selection
I/O Streams File I/O 2-D array review
Adapted from slides by Marty Stepp and Stuart Reges
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Week 2.
CS1315: Introduction to Media Computation
CS150 Introduction to Computer Science 1
Topics Introduction to Repetition Structures
Manipulating Pictures, Arrays, and Loops part 2
Functions BIS1523 – Lecture 17.
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:

CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use.

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 tell us that, although we could use getX and getY to get this information as you will see later.

Tuning our color replacement If you want to get Maury’s hair, the threshold method from last time doesn’t work  TV cart, pants within the threshold value How could we do it better?  Lower our threshold, but then miss most of the hair  Work only within a range… (only look at certain parts of the picture)

Color replacement: last time Made everything within threshold of brown more red

Introducing the function range Range returns a sequence “between” its first two inputs, possibly using a third input as the increment (includes the first; excludes second) >>> 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 for a picture 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 (x and y)

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 Be sure to watch your blocks carefully! 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) x y picture (1,1)

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, the variable x takes on the value 1. We then execute the block under the outer for loop. (We’ll be processing the first column of pixels in the picture.)

Now, the inner loop 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, we set y to 1. We’re now going to process each of the pixels in column 1.

Process a pixel 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) With x = 1 and y = 1, we get the leftmost pixel and increase its red by 10%

Next pixel 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 we set y to 2 (next value in the sequence range(1,getHeight(picture))

Process pixel (1,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) 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.

Finally, next column 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) Now that we’re done with the loop for y, we get back to the FOR loop for x. x takes on the value 2, and we go back to the y loop to process all the pixels in the column x=2.

Replacing colors in a range def turnRedInRange(): brown = makeColor(45,32,17) file = "/Users/sloan/Mediasources/MauryInfant.jpg" picture=makePicture(file) for x in range(129,185): for y in range(1,81): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown) < 32.0: redness=getRed(px)*1.85 setRed(px,redness) show(picture) return(picture) Get the range using MediaTools

Before and after: new hairdo Still not perfect....

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(45,32,17) file = "/Users/sloan/Mediasources/MauryInfant.jpg" picture=makePicture(file) for x in range(129,185): for y in range(1,81): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown) < 32.0: redness=getRed(px)*1.85 setRed(px,redness) show(picture) return(picture)

The nested loop I used MediaTools to find the rectangle where most of the hair is that I want to change def turnRedInRange(): brown = makeColor(45,32,17) file=r"/Users/sloan/Mediasources/MauryInfant.jpg" picture=makePicture(file) for x in range(129,185): for y in range(1,81): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown) < 32.0: redness=getRed(px)*1.85 setRed(px,redness) show(picture) return(picture)

Same thing as last time (but raised threshold now) Then we’re looking for a close match on hair color, and increasing the redness def turnRedInRange(): brown = makeColor(45,32,17) file=r"/Users/sloan/Mediasources/MauryInfant.jpg" picture=makePicture(file) for x in range(129,185): for y in range(1,81): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown) < 32.0: redness=getRed(px)*1.85 setRed(px,redness) show(picture) return(picture)

Could we do this without nested loops? Yes, but complicated IF and actually uses more computation since we go through all the pixels instead of just a smaller range. def turnRed2(): brown = makeColor(45,32,17) file=r"/Users/sloan/Mediasources/MauryInfant.jpg" picture=makePicture(file) for px in getPixels(picture): x = getX(px) y = getY(px) if x >= 129 and x = 1 and y <= 81: color = getColor(px) if distance(color,brown) < 32.0: redness=getRed(px)*1.85 setRed(px,redness) show(picture) return(picture)