1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Slides:



Advertisements
Similar presentations
Flipping Across the Axis
Advertisements

Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Factoring Quadratic Expressions ax 2 + bx + c. 2x2x 3x3x +3 – 4 Setting the Stage Do you remember how to multiply these together? (Referred to as FOIL.
Sound, Part 2 Using range to manipulate samples by index number.
1 CS 177 Week 6 Recitation Slides Scaling Drawing on images Vector-based Vs. Bitmap graphical representation.
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.
Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
Copying and Transforming Pictures. First, finding the min or max… Next homework asks you to write a function to find the darkest and lightest shade of.
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
UWCSE BRIDGE Workshop Aug. 31 – Sept. 3, 2009 Hal Perkins Computer Science & Engineering University of Washington
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.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
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.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
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.
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.”
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
1 CS 177 Week 11 Recitation Slides Writing out programs, Reading from the Internet and Using Modules.
Copying: How it works Here's the initial setup:. Copying: How it works 2 After incrementing the sourceY and targetY once (whether in the for or via expression):
CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan,
Teaching Programming to Everyone through Media Computation Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
Chapter 4: Modifying Pixels in a Range (partial slide deck)
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.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
ACM SIGCSE 2003: Multimedia Construction Projects Mark Guzdial College of Computing Georgia Institute of Technology
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Chapter 6: Modifying Pixels by Position
CSC 112Introduction to Media Computation 1 Rotating Images.
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.
Media computation as a context for learning computing Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Working with Sounds Barb Ericson College of Computing Georgia Institute of Technology
CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified.
Chapter 5: Picture Techniques with Selection
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Picture Functions ppp =makePicture(pickAFile())
Chapter 8: Making Sounds by Combining Pieces
CS1315: Introduction to Media Computation
Workshop for Programming And Systems Management Teachers
Working with ranges in pictures
Test 2 on Wed, 11/9 On image processing
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Chapter 4: Modifying Pixels in a Range
Two-Dimensional Arrays and Nested Loops – part 2
Two-Dimensional Arrays and Nested Loops – part 6
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
CSC1401 Viewing a picture as a 2D image - 2
February , 2009 CSE 113 B.
Two-Dimensional Arrays and Nested Loops – part 6
CS 177 Week 9 Recitation Slides
CS 101: Introduction to Computing
CS1315: Introduction to Media Computation
Chapter 4: Modifying Pixels in a Range
CS1315: Introduction to Media Computation
Presentation transcript:

1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range

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

3 ANY QUESTIONS?

Horizontal mirror recipe mirroring means intuitively "flipping around" an axis (when you mirror horizontally, you flip your picture around a vertical axis) STEP 1. Since the picture is represented by a matrix, you must determine the coordinates (x and y) of all the "points" of this axis in the matrix STEP 2. Then you have to determine the direction of the flipping (when you mirror horizontally, you may flip the left side to right side or vice versa) STEP3. Now, since pictures are encoded as a matrices, you must figure out where a pixel of the source picture should go in the target picture 4

Step 1- determine the mirror axis Step 2 - determine the flipping direction 5 1 2

Work it out with matrices To find out the mirror axis you need just to determine its x coordinate (the mirrorPoint). It is is halfway across: getWidth(picture)/2 6

Work it out with matrices STEP 2. If the flipping direction is left to right, then the source and target matrices will look like this: 7

Step 3 Figure out where a pixel of the source picture should go in the target picture 8 If source pixel is at (x,y), target pixel is at (width-x-1,y)

Recipe for vertical mirroring def mirrorVertical(source): mirrorPoint = getWidth(source) / 2 width = getWidth(source) for y in range(0,getHeight(source)): for x in range(0,mirrorPoint): leftPixel = getPixel(source,x,y) rightPixel = getPixel(source,width - x - 1,y) color = getColor(leftPixel) setColor(rightPixel,color) 9

Can we do it with a horizontal mirror? def mirrorHorizontal(source): mirrorPoint = getHeight(source) / 2 height = getHeight(source) for x in range(0,getWidth(source)): for y in range(0,mirrorPoint): topPixel = getPixel(source,x,y) bottomPixel = getPixel(source,x,height - y - 1) color = getColor(topPixel) setColor(bottomPixel,color) 10

Of course! 11

What if we wanted to copy bottom to top? Very simple: Swap the order of pixels in the bottom lines def mirrorBotTop(source): mirrorPoint = getHeight(source) / 2 height = getHeight(source) for x in range(0,getWidth(source)): for y in range(0,mirrorPoint): topPixel = getPixel(source,x,y) bottomPixel = getPixel(source,x,height - y - 1) color = getColor(bottomPixel) setColor(topPixel,color) 12

Mirroring bottom to top 13

Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for remembering a place where your media will be coming from (or going to)  setMediaPath() lets you pick a file in your media folder  getMediaPath(basefilename) lets you generate a complete filename out of only the last part 14

Utility functions example >>> setMediaPath() New media folder: C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\ >>> getMediaPath("barbara.jpg") 'C:\\Documents and Settings\\Mark Guzdial\\My Documents\\mediasources\\barbara.jpg' >>> barb=makePicture(getMediaPath("barbara.jpg")) 15

Copying pixels In general, what we have to do is to keep track of the source index variables (sourceX and sourceY), and of the target index variables (targetX and targetY).  We increment (add to them) in pairs sourceX and targetX get incremented together sourceY and targetY get incremented together  The tricky parts are: Setting values inside the body of loops Incrementing at the bottom of loops 16

Copying Barb to a canvas def copyBarb(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 0 for sourceX in range(0,getWidth(barb)): targetY = 0 for sourceY in range(0,getHeight(barb)): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas 17

Copying into the middle of the canvas def copyBarbMidway(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 100 for sourceX in range(0,getWidth(barb)): targetY = 100 for sourceY in range(0,getHeight(barb)): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas 18

Copying: How it works Here’s the initial setup: 19

Copying: How it works 2 After incrementing the sourceY and targetY once (whether in the for or via expression): 20

Copying: How it works 3 After yet another increment of sourceY and targetY: When we finish that column, we increment sourceX and targetX, and start on the next column. 21

Copying: How it looks at the end Eventually, we copy every pixel 22

23 Functions with return values Useful when we need to access the output generated by a function, not just print the value. The following command is used to return the value of a variable abc that is generated inside a function. return abc The return statement lets you assign the output of a function to another variable so that it can be used later. The following slides explain what this means.

24 Functions with return values (contd) The above function prints the value of c on the screen The function below returns the value of c to the place where it was called (i.e. the command window). The effect is the same as having a print statement instead of a return

25 Functions with return values (contd) The following examples explain the difference between printing a value within the function and returning the value from a function

26 Final QUESTIONS???