Manipulating Pictures, Arrays, and Loops

Slides:



Advertisements
Similar presentations
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.
Advertisements

ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
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.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
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.
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.
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.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
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.
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.
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.
CSE8A Lecture 5 TODO: –FINISH PSA2 WITH YOUR PARTNER! Read next class: Section 5.1. PLAY WITH CODE! –Get stuck, then figure out how to get unstuck – it’s.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
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.
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
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 5
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
CSE 8A Lecture 6 Reading for next class:
Manipulating Pictures, Arrays, and Loops part 4
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Processing Digital Sounds part 3
Two-Dimensional Arrays and Nested Loops – part 2
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 2
Manipulating Pictures, Arrays, and Loops part 6
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
CSC1401 Manipulating Pictures 2
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to trace code How to create a new method by copying another one and changing the method name And other parts of the method How to increase the red in a picture How to remove a color from a picture Georgia Institute of Technology

Georgia Institute of Technology Tracing Code An important skill to develop is the ability to trace code Also called walking through or stepping through your code Look at each line and predict what will happen Show the variables and their values Georgia Institute of Technology

Step Through decreaseRed() A picture object was created from the file “caterpillar.jpg” and then was sent the message decreaseRed() The picture object was implicitly passed to the method decreaseRed() and can be referred to by this The array of pixel objects was returned from sending getPixels() to the picture object Pixel[] pixelArray = this.getPixels(); Some variables were declared for later use in the loop Pixel pixelObj = null; int index = 0; int value = 0; Georgia Institute of Technology

Step Through decreaseRed() - cont The while loop tests if the index is less than the length of the array while (index < pixelArray.length) { And if so it executes the statements in the body of the loop {} It sets the variable pixelObj to the pixel at the current index in the array of pixels pixelObj = pixelArray[index]; It gets the red value of that pixel value = pixelObj.getRed(); it sets the value to the integer part of (red value * 0.5) value = (int) (value * 0.5); It sets the pixel’s red to the new value pixelObj.setRed(value); It increments the index value index++; After each execution of the loop the test will be executed again. If the test is false execution will jump to the first statement after the body of the loop. If the test is true the statements in the loop will be executed again. Georgia Institute of Technology

Memory Map of decreaseRed() width=329 height=150 What does memory look like the first time through the loop? How about the 2nd time through? How about the 3rd time through? How about the last time through? Picture: Class getPixels() … this pixels R=252, G=254, B=251, X=0, Y=0 R=253, G=255, B=254, X=1, Y=0 R=254, G=254, B=254, X=2, Y=0 … pixel The this refers to the object the method was invoked on. This is a picture object so it has a reference to the object that defines the Picture class. The pixels variable refers to the array of Pixel objects. The pixel variable refers to the first Pixel in the array of Pixel objects. Each Pixel object has a reference to the object that defines the class Pixel. The value variable holds the red value for the current pixel. The index starts at 0 and changes each time through the loop. Pixel: Class getRed() setRed()… value = 252 index = 0 Georgia Institute of Technology

Georgia Institute of Technology Increase Red What if you want to increase red by 30% How would you do that? Multiplying by 0.5 reduces the red by 50% Multiplying by 1.0 would keep the same red value Multiplying by 1.3 would increase the red by 30% Multiplying by 1.7 would increase the red by 70% Georgia Institute of Technology

Increase Red Algorithm To increase the red value in a picture by 30% Get the array of pixels from the picture Set up an index to start at 0 Loop while the index is less than the length of the array Get the pixel at the current index from the array of pixels Get the red value at the pixel Multiply the red value by 1.3 Set the red value at the pixel to the reduced red value Increment the index and go back to step 3 Georgia Institute of Technology

Increase Red: Algorithm to Code The algorithm for this method is very similar to the algorithm for decrease red Start with the code for decreaseRed Change the line of code that multiplied the current red value by 0.5 to 1.3 Change the name of the method Change any comments that need changing Georgia Institute of Technology

Georgia Institute of Technology increaseRed Method /** * Method to increase the red by 30% */ public void increaseRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int value = 0; int index = 0; // loop through all the pixels while (index < pixelArray.length) // get the current pixel pixelObj = pixelArray[index]; // get the value value = pixelObj.getRed(); // increase value value = (int) (value * 1.3); // set the red value pixelObj.setRed(value); // increment the index index++; } Georgia Institute of Technology

Georgia Institute of Technology Testing increaseRed Underwater pictures look too blue The water filters out most of the red color To test increase red: String file = “c:/intro-prog-java/mediasources/water.jpg”; Picture p = new Picture(file); p.explore(); p.increaseRed(); You may want to increase red several times to correct the too blue water picture. Use the explorer to check the red values in the original picture and the one with the red increased. Georgia Institute of Technology

Georgia Institute of Technology Clear the Blue Values What if you want to clear the blue from a picture? Set all the blue values to 0 for all the pixels The algorithm is similar to decreaseRed() and increaseRed() except You don’t need to get the value out for blue and multiply it by some value and then set the blue value Just set the blue value to 0 Georgia Institute of Technology

Georgia Institute of Technology Clear Blue Exercise In Picture.java Write the method public void clearBlue() That sets the blue value to 0 for all pixels in a picture Test with: String fileName = “c:/intro-prog-java/mediasources/caterpillar.jpg”; Picture p = new Picture(fileName); p.explore(); p.clearBlue(); When you clear the blue the white in the picture will look yellow. Georgia Institute of Technology

Georgia Institute of Technology Summary Tracing code is walking through code and saying what each line is doing It can also help to show the values of the variables after each line executes You can increase the red in a picture by multiplying the red value by a number > 1.0 You can clear a color from a picture by setting the value to 0 Georgia Institute of Technology