ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.

Slides:



Advertisements
Similar presentations
Conditionals-part31 Conditionals – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Advertisements

ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology August 2005.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 10 THE IF STATEMENT 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 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.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Barbara Ericson Georgia Tech Sept 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Manipulating Pictures, Arrays, and Loops part 3
Topic 10 The if statement Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
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
Workshop for Programming And Systems Management Teachers
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology June 2006
Introduction to Processing Digital Sounds part 3
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 3
Barb Ericson Georgia Institute of Technology Oct 2005
CSC1401 Manipulating Pictures 2
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009

ManipulatingPictures-Mod6-part52 Learning Goals Understand at a conceptual and practical level –How to use a for loop –How to print out values using System.out.println –How to average values –How to convert a while loop into a for loop

ManipulatingPictures-part33 Faking a Sunset If you want to make an outdoor scene look like it happened during sunset –You might want to increase the red But you can’t increase past 255 –Another idea is to reduce the blue and green To emphasize the red Try to reduce the blue and green by 30%

ManipulatingPictures-part34 Faking a Sunset Algorithm Reduce the blue and green by 30% 1.Get the array of pixels from the picture 2.Set up an index to start at 0 3.Loop while the index is less than the length of the array 1.Get the pixel at the current index from the array of pixels 2.Set the blue value at the pixel to 0.7 times the original value 3.Set the green value at the pixel to 0.7 times the original value 4.Increment the index and go back to step 3

ManipulatingPictures-part35 Faking a Sunset Method /** * Method to simulate a sunset by * decreasing the green * and blue */ public void makeSunset() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int value = 0; int i = 0; // loop through all the pixels while (i < pixelArray.length) { // get the current pixel pixelObj = pixelArray[i]; // change the blue value value = pixelObj.getBlue(); pixelObj.setBlue((int) (value * 0.7)); // change the green value value = pixelObj.getGreen(); pixelObj.setGreen((int) (value * 0.7)); // increment the index i++; }

ManipulatingPictures-part36 Testing makeSunset String file = “c:/intro-prog-java/mediasources/beach.jpg”; Picture pictureObj = new Picture(file); pictureObj.explore(); pictureObj.makeSunset(); pictureObj.explore();

Variables In makeSunset we used i for the index –Programmers often use short names like i to stand for an index Scope – where a variable name is understood and can be used –Variables declared inside of a method have scope only inside that method The same name can be used by different methods –Variables declared in the interactions pane are only known in the interactions pane ManipulatingPictures-part37

8 For Loops Programmers like shortcuts –Especially those that reduce errors –And mean less typing We have been using a while loop with an index –We had to declare the index variable and initialize it before the loop If you forget this there will be a compiler error –We had to increment the index in the loop If you forget this it will be an infinite loop The shortcut for this is a for loop

ManipulatingPictures-part39 For Loop Syntax for (initialization area; continuation test; change area) –Initialization area Declare variables and initialize them –Continuation test If true do body of loop If false jump to next statement after the loop –Change area Change the loop variables –Increment or decrement them

ManipulatingPictures-part310 Comparison of While and For Loops int index = 0; while (index < pixelArray.length) { statements. index++; } for (int i=0; i < pixelArray.length; i++) { statements. }

ManipulatingPictures-part311 Change clearBlue() to use a For Loop /** * Method to clear the blue (set * the blue to 0 for all pixels) */ public void clearBlue() { Pixel pixelObj = null; // get the array of pixels Pixel[] pixelArray = this.getPixels(); // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // set the blue on the pixel to 0 pixelObj.setBlue(0); } }

ManipulatingPictures-part312 Using System.out.println() in a Loop One way to check what is happening in your program is to add –System.out.println(expression); You might add this to the loop to check the value of 'i' during it. –And to verify that the increment happens after the last statement in the loop You could add a print statements after the loop to see the last value of 'i'

ManipulatingPictures-part313 Change to For Loop Exercise Edit makeSunset() and change it from using a while loop to using a for loop –Move the declaration of the index to the for loop initialization area –Move the index increment to the for loop change area –Execute the code to make sure it still works

ManipulatingPictures-part314 Negating an Image How would you turn a picture into a negative? –White should become black 255,255,255 becomes 0,0,0 –Black should become white 0,0,0 becomes 255,255,255

ManipulatingPictures-part315 Negate Algorithm Subtract current value from 255 for red, green, and blue 1.Get the array of pixels from the picture 2.Declare variables to hold the current pixel and the red, green, and blue values 3.Loop starting an index at 0 and incrementing by 1 and loop while the index is less than the length of the array 1.Get the pixel at the current index from the array of pixels 2.Set the red value to 255 – current red value 3.Set the blue value to 255 – current blue value 4.Set the green value to 255 – current green value 5.Increment the index and go back to step 3

ManipulatingPictures-part316 Negate Method /** * Method to negate the picture */ public void negate() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int redValue, blueValue, greenValue = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // get the values redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); // set the pixel's color pixelObj.setColor( new Color(255 - redValue, greenValue, blueValue)); }

ManipulatingPictures-part317 Changing to Grayscale Grayscale ranges from black to white –The red, green, and blue values are the same How can we change any color to gray? –What number can we use for all three values? The intensity of the color –We can average the colors (red + green + blue) / 3 –Example ( ) / 3 = 90

ManipulatingPictures-part318 Grayscale Algorithm Set color values to the average of the original values 1.Get the array of pixels from the picture 2.Declare variables to hold the current pixel and the red, green, and blue values 3.Loop starting an index at 0 and incrementing by 1 and loop while the index is less than the length of the array 1.Get the pixel at the current index from the array of pixels 2.Calculate the average of the current values 1.(redValue + greenValue + blueValue )/ 3 3.Set the red value to the average 4.Set the blue value to the average 5.Set the green value to the average 6.Increment the index and go to step 3

ManipulatingPictures-part319 Grayscale Method /** * Method to change the picture to gray scale */ public void grayscale() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int intensity = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // compute the average intensity intensity = (pixelObj.getRed() + pixelObj.getGreen() + pixelObj.getBlue()) / 3; // set the pixel color pixelObj.setColor(new Color(intensity,intensity,intensity)); }

ManipulatingPictures-part320 Testing Grayscale String file = “c:/intro-prog-java/mediasources/caterpillar.jpg”; Picture pictureObj = new Picture(file); pictureObj.explore(); pictureObj.grayscale(); pictureObj.explore();

ManipulatingPictures-part321 Grayscale Result

ManipulatingPictures-part322 Luminance We perceive blue to be darker than red, and green –Even when the same amount of light is reflected A better grayscale model should take this into account –Weight green the highest (* 0.587) –red less (* 0.299) and –blue the very least (* 0.114)

ManipulatingPictures-part323 Grayscale with Luminance Exercise Create a new method grayscaleWithLuminance Using the new algorithm for calculating intensity intensity = (int) (red * green * blue * 0.114)

ManipulatingPictures-part324 Testing Grayscale with Luminance String file = “c:/intro-prog-java/mediasources/caterpillar.jpg”; Picture pictureObj = new Picture(file); pictureObj.explore(); pictureObj. grayscaleWithLuminance (); pictureObj.explore();

ManipulatingPictures-part325 Summary You can change more than one color in a method –Like in make sunset You can use a for loop to repeat a series of Java statements until a test is false for (initialization area; continuation test; change area) –Executes the same way a while loop does But less error prone when executing a loop at set number of times –You can convert code that uses a while loop into using a for loop –You can print values in a loop using System.out.println(expression);