NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.

Slides:



Advertisements
Similar presentations
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
Advertisements

Conditionals-part31 Conditionals – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
How to use the Java class libraries Brief documentation of how to do this all with Java.
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.
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.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
UsingSoundRanges-part21 Processing Sound Ranges part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
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 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,
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC 112Introduction to Media Computation 1 Rotating Images.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
CS1315: Introduction to Media Computation Transforming pictures by index number.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
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.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Barb Ericson Georgia Institute of Technology September 2005
Test 2 on Wed, 11/9 On image processing
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Barb Ericson Georgia Institute of Technology Dec 2009
Processing Sound Ranges part 1
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Barb Ericson Georgia Institute of Technology August 2005
Working with ranges in pictures
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 2
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Two-Dimensional Arrays and Nested Loops – part 4
Two-Dimensional Arrays and Nested Loops – part 3
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 2
Manipulating Pictures, Arrays, and Loops part 6
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops part 6
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part42 Learning Goals Understand at a conceptual and practical level –How to copy one picture to another so that the first picture is rotated 90 degrees left or right –How to simplify a problem –How to come up with an algorithm to solve a problem –How to test the algorithm –How can you make a picture smaller or larger

NestedLoops-part43 Left Rotation How can you copy one picture onto another so that the first picture is rotated to the left 90 degrees?

NestedLoops-part44 Left Rotation First simplify the problem by thinking about how to copy when each pixel has just a number at it Can you come up with an algorithm for this?

NestedLoops-part45 Left Rotation Try out your algorithm on another example –Does it work? Can you translate it into code?

NestedLoops-part46 Left Rotation To rotate an image left 90 degrees still copy all the pixels –But they go to different locations in the target Column values become row values target x = source y target y = source width - 1 – source x

NestedLoops-part47 Left Rotation Algorithm Create the target picture object Invoke the method on the target picture –Create the source picture object –Loop through the source x Loop through the source y –Get the source pixel at the x and y values –Get the target pixel with the x equal the source y value and the y equal the source picture width – 1 minus the source x –Copy the color from the source pixel to the target pixel

NestedLoops-part48 Left Rotation Method public void copyKatieLeftRotation() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX, targetY = 0; // loop through the columns for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++) {

NestedLoops-part49 Copy Katie Left Rotation // loop through the rows for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetX = sourceY; targetY = sourcePicture.getWidth() – 1 – sourceX; targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); }

NestedLoops-part410 Testing Left Rotation String file = FileChooser.getMediaPath( “ 7inX95in.jpg”); Picture p = new Picture(file); p.show(); p.copyKatieLeftRotation(); p.repaint();

NestedLoops-part411 Challenge Create a general left rotation method that –Works on any picture Call the method on the picture to be rotated left –Returns a new picture that is just the right size to hold the rotated picture Make the new picture's width the same as the old picture's height and the new picture's height the same as the old picture's width

NestedLoops-part412 Scaling You can make a picture smaller –Faster to download on the web Increment the source x and y by a number larger than 1 –Don’t use all the source pixels in the target picture You can make a picture larger –Show more detail Copy the same source x and y to more than one target x and y –Use source pixels more than once in target

NestedLoops-part413 Scaling Down a Picture passionFlower.jpg is 640pixels wide and 480 pixels high If we copy every other pixel we will have a new picture with width (640 / 2 = 320) and height (480 / 2 = 240)

NestedLoops-part414 Scaling Down Algorithm Create the target picture Invoke the method on the target picture –Create the source picture –Loop with source x starting at 0 and target x starting at 0 as long as < source width Increment the source x by 2 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height –Increment the source y by 2 each time through the loop, increment the target y by 1 »Copy the color from the source to target pixel

NestedLoops-part415 Scaling Down Method public void copyFlowerSmaller() { Picture flowerPicture = new Picture( FileChooser.getMediaPath(“passionFlower.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX=0; sourceX < flowerPicture.getWidth(); sourceX+=2, targetX++) {

NestedLoops-part416 Scaling Down Method - Continued // loop through the rows for (int sourceY=0, targetY=0; sourceY < flowerPicture.getHeight(); sourceY+=2, targetY++) { sourcePixel = flowerPicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); }

NestedLoops-part417 Trying Copy Flower Smaller Create a new picture half the size of the original picture (+ 1 if odd size) –Picture p1 = new Picture(320,240); Copy the flower to the new picture –p1.copyFlowerSmaller(); Show the result –p1.show();

NestedLoops-part418 Thinking Through Scaling Up Copy each pixel in the source multiple times to the target –Source (0,0) Target (0,0) –Source (0,0) Target(1,0) –Source (1,0) Target(2,0) –Source (1,0) Target(3,0) –Source (2,0) Target(4,0) –Source (2,0) Target(5,0) –Source (0,0) Target(0,1) –Source (0,0) Target(1,1)

NestedLoops-part419 Scaling Up Algorithm Create the target picture Invoke the method on the target picture –Create the source picture –Loop with source x starting at 0 and target x starting at 0 as long as < source width Increment the source x by 0.5 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height –Increment the source y by 0.5 each time through the loop, increment the target y by 1 »Copy the color from the source to target pixel

NestedLoops-part420 Scaling Up Exercise Write a method copyFlowerBigger to scale up the picture flower1.jpg when you copy it to 640x480.jpg Save the result to a file using –pictureObj.write(“file”);

NestedLoops-part421 Summary To copy one picture to another with the first picture rotated –You need to change the targetX and targetY You should simplify a problem –And try to solve it by hand –Then come up with an algorithm for solving it –And then try it on another example To scale a picture down use every other pixel –To scale a picture up use every pixel more than once