ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 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

Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
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.
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.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology August 2005.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
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.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
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.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
1 Ethics of Computing MONT 113G, Spring 2012 Session 10 HTML Tables Graphics on the Web.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Two –Dimensional Arrays Mrs. C. Furman Java Programming November 19, 2008.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Digital Pictures Represented by pixels –With a red, green, and blue value stored for each pixel (each has a range from 0 to 255) Stored in.jpg (JPEG) files.
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.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
CSC1401 Manipulating Pictures. What we have done to date We have modified pictures by writing on top of them Using Turtles and using Graphics Drawing.
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.
Picture Lab. Manipulating Pictures Discussion and activities with java.awt.Color introduce students to megapixels, pixels, the RGB color model, binary.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
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 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.
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 1
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
Digital Pictures Represented by pixels Stored in .jpg (JPEG) files
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 1
Looping through pixels.
Manipulating Pictures, Arrays, and Loops part 4
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Media Computation
Manipulating Pictures, Arrays, and Loops part 1
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Digital Pictures Represented by pixels Stored in .jpg (JPEG) files
Manipulating Pictures, Arrays, and Loops part 6
Digital Pictures Represented by pixels Stored in .jpg (JPEG) files
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

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

ManipulatingPictures-part12 Learning Goals Understand at a conceptual and practical level –How to manipulate digital pictures? –What is an array? –How to get information from a picture object? –How to get and set information in a pixel object? –How to create and work with a color object? –How to import a class in a package?

ManipulatingPictures-part13 Digital Pictures Represented by pixels –With a red, green, and blue value stored for each pixel with 8 bits per color (a range of 0 to 255) Stored in.jpg (JPEG) files –International standard –With lossy compression Lossy means not all data is stored –But what is lost isn’t that important Compression means made smaller Other formats for storing digital pictures are GIFF and BMP

Pixels – Picture Elements Small dots of color that make up digital pictures ManipulatingPictures-part14

5 Color Objects There is a class defined in Java that represents color –The Color class in the package java.awt –To use the class you must either import java.awt.Color; Use the full name java.awt.Color You can create a color object by giving the red, green, and blue values for it –Color colorObj = new Color(255,10,125);

Mixing red, green, and blue You can use a ColorChooser to see how red, green, and blue mix to make all the colors –ColorChooser.pickAColor() –Try to make different colors Yellow Green Pink Black White Brown ManipulatingPictures-part16

7 Predefined Colors The Color class has defined class constants for many colors –Color.RED, Color.GREEN, Color.BLUE, Color.BLACK, Color.WHITE, Color.YELLOW, Color.GRAY, Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA –Or you can use all lowercase names Color.red, Color.blue, Color.black, …

ManipulatingPictures-part18 Pictures have lots of Pixels How can we refer to each pixel? –pixel1, pixel2, pixel3, pixel4, pixel5, … Do we really want to name each one? –There are 640 x 480 = 307,200 pixels How do we deal with lots of data of the same type? –Use an array –Like a list but of a fixed size and with all the data contiguous in memory

ManipulatingPictures-part19 What is an Array? Storage for a sequence of items –Of the same type You can access items by using an index –int x = arrayRef[index]; The index starts at 0 –The first item is at index 0 –The last item is at index (length – 1) Arrays know their length (have a public length field) –arrayObj.length

Declaring and Creating an Array Declare an array using –Type[] name or Type name[]; –Pixel[] pixelArray = null; –double grades[]; Create an array using –new Type[numberOfElements]; –new int[5]; –int numArray = new int[5]; // all values are 0 Initialize array elements using –double[] gradeArray = {80, 90.5, 88, 92, 94.5}; –int grade = gradeArray[2] // grade = 88 ManipulatingPictures-part110

Two-Dimensional Arrays (Matrix) Pictures are actually two-dimensional arrays –Have a horizontal (x or column) and vertical (y or row) value for each pixel ManipulatingPictures-part111

2D Arrays in Java Elements can be accessed with –arrayRef[x][y] or arrayRef[col][row] –arrayRef[y][x] or arrayRef[row][col] It depends how the data was initialized –We use arrayRef[x][y] in our Picture class –GridWorld uses arrayRef[row][col] ManipulatingPictures-part112

ManipulatingPictures-part113 Manipulating a Picture To manipulate a picture we need to manipulate the pixels that make up the picture –Change the red, green, or blue values at the pixel Pixel is a class that we created at Georgia Tech –Each pixel object has a red, green, and blue value that ranges from 0 to 255

ManipulatingPictures-part114 What Data does a Picture Object Have? It knows the picture width pictureObj.getWidth() It knows the picture height pictureObj.getHeight() It knows how to return an array of pixels Pixel[] pixelArray = pictureObj.getPixels() It knows how to return a pixel at a (x,y) location Pixel p = pictureObj.getPixel(0,0);

ManipulatingPictures-part115 Pixel Objects Each pixel has a red, green, and blue value –getRed(), getGreen(), getBlue() –setRed(int v), setGreen(int v), setBlue(int v) Each pixel knows the location it was in the picture object –getX(), get(Y) You can also get and set the color at the pixel –Color currColor = pixelObj.getColor() –pixelObj.setColor(Color theColor)

Modifying the Pixels in a Picture We can get a 1D array of pixel objects from a picture –Pixel[] pixelArray = this.getPixels(); –This gets all the pixels in the first row followed by all the pixels in the second row and so on. We can use a for-each loop to loop through all the pixels in a picture –Like looping through an Alice list ManipulatingPictures-part116

Decrease Red Algorithm How do we decrease the amount of red in a digital picture? –By half? Loop through all the pixels in the picture –Get a 1D array of pixels in the picture –Loop through all the pixels Get the current red at the current pixel Set the red at the current pixel to half the original value –Need the result to be an integer so cast to int ManipulatingPictures-part117

Decrease Red Method public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the current red value value = pixelObj.getRed(); // decrease the red value by 50% (1/2) value = (int) (value * 0.5); // set the red value of the current pixel to the new value pixelObj.setRed(value); } ManipulatingPictures-part118

Testing the Decrease Red Method String fName = "f:/mediasources/caterpillar.jpg"; Picture pict = new Picture(fName); pict.explore(); pict.decreaseRed(); pict.explore(); ManipulatingPictures-part119

How it works When we execute pict.decreaseRed() –The this keyword refers to the same picture object as pict –The first time through the loop pixelObj will refer to the first pixel in the picture pixelArray[0] or getPixel(0,0) –The second time through the loop pixelObj will refer to the second pixel in the picture pixelArray[1] or getPixel(1,0) –The last time through the loop pixelObj will refer to the last pixel in the picture pixelArray.length – 1 or getPixel(328,149) ManipulatingPictures-part120

Challenge Write a method to increase the amount of red in a picture –Call it increaseRed() –Multiply the current red value by 2 for each pixel in the picture –Does the amount of red ever go over 255? Write a method to set all the blue values in a picture to 0 –Call it clearBlue() ManipulatingPictures-part121

ManipulatingPictures-part122 Summary Pictures have pixels –You can change the picture by changing the color of the pixels Arrays let you store and retrieve values of the same type using an index You can ask a picture for it’s width, height, and an array of pixels You can get and set the color of a pixel You will need to import classes that you wish to use that aren’t in java.lang