Presentation is loading. Please wait.

Presentation is loading. Please wait.

Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.

Similar presentations


Presentation on theme: "Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1."— Presentation transcript:

1 Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1

2 Digital Pictures Georgia Institute of Technology Represented by pixels With a red, green, and blue value stored for each pixel 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

3 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 was created at Georgia Tech Each pixel object has a red, green, and blue value

4 What Data does a Picture Object Have? Georgia Institute of Technology A picture object has an array of pixel objects That it read from the JPEG file 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()

5 Picture Exercise Georgia Institute of Technology Create a picture in DrJava get the pictures width, height, and number of pixels String fileName = FileChooser.pickAFile(); Picture pictureObj = new Picture(fileName); int width = pictureObj.getWidth(); System.out.println(“The picture width is “ + width); int height = pictureObj.getHeight(); System.out.println(“The picture height is “ + height); Pixel[] pixelArray = pictureObj.getPixels(); System.out.println(pixelArray.length + “ pixels”);

6 Pixel Objects Georgia Institute of Technology Each pixel has a red, green, and blue value getRed(), getGreen(), getBlue() setRed(v), setGreen(v), setBlue(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 getColor(), setColor(color)

7 Color Objects Georgia Institute of Technology 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);

8 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 uppercase names Color.RED, Color.BLUE, Color.BLACK, … Georgia Institute of Technology

9 Getting and Setting Pixel Colors Georgia Institute of Technology To get a pixel’s color as a color object Color color1 = pixelObj.getColor(); int red = color1.getRed(); int green = color1.getGreen(); int blue = color1.getBlue(); To set a pixel’s color using a new color object red = 20; green = 30; blue = 100; Color color2 = new Color(red,green,blue); pixelObj.setColor(color2);

10 Pixel Exercise Georgia Institute of Technology In DrJava Pick a file and create a picture object Get the array of pixels from the picture object Get the 1 st pixel from the array of pixels Pixel pixelObj = pixelArray[0]; // 0 is first one Get the red, green, and blue value for this pixel Get the x and y location of this pixel Get the color of this pixel Get the red, green, and blue values of the color

11 Changing Pixel Colors Georgia Institute of Technology There are two ways to change the color of a pixel in a picture Set the red, green, and blue values individually pixelObj.setRed(value), pixelObj.setGreen(value), pixelObj.setBlue(value), Or set the color pixelObj.setColor(colorObj) But, you won’t see any change in the picture Until you ask it to repaint: pictureObj.repaint();

12 Pictures are 2-D Arrays They have columns and rows (x and y) You can get a pixel at a particular x and y location Pixel pixelObj = picture.getPixel(x,y); The columns and rows start with index 0 end with num -1 Georgia Institute of Technology X Y

13 Changing a Picture Exercise Georgia Institute of Technology > import java.awt.Color; > String fileName = "C:/intro-prog-java/mediasources/caterpillar.jpg"; > Picture pictureObj = new Picture(fileName); > pictureObj.show(); > pictureObj.getPixel(10,100).setColor(Color.black); > pictureObj.getPixel(11,100).setColor(Color.black); > pictureObj.getPixel(12,100).setColor(Color.black); > pictureObj.getPixel(13,100).setColor(Color.black); > pictureObj.getPixel(14,100).setColor(Color.black); > pictureObj.getPixel(15,100).setColor(Color.black); > pictureObj.getPixel(16,100).setColor(Color.black); > pictureObj.getPixel(17,100).setColor(Color.black); > pictureObj.getPixel(18,100).setColor(Color.black); > pictureObj.getPixel(19,100).setColor(Color.black); > pictureObj.repaint();

14 Method to Decrease Red Georgia Institute of Technology Loop through all the pixels in an array of Pixel objects and change the red in each public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the red value value = pixelObj.getRed(); // decrease the red value by 50% (1/2) value = value / 2; // set the red value of the current pixel to the new value pixelObj.setRed(value); } The body of the loop is in {}

15 Decrease Red Method Georgia Institute of Technology public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; Pixel pixelObj = null; int index = 0; // loop through all the pixels while(index < pixelArray.length) { // get the current pixel pixelObj = pixelArray[index]; // get the red value value = pixelObj.getRed(); // decrease the red value value = value / 2; // set the pixel red pixelObj.setRed(value); // increment the index index = index + 1; }

16 Decrease Red Exercise Georgia Institute of Technology In DrJava Add the method decreaseRed() to Picture.java Before the last } which ends the class definition Compile the method Click the Compile All button Test it by doing the following in the interactions pane > String fileName = "C:/intro-prog-java/mediasources/caterpillar.jpg"; > Picture picture1 = new Picture(fileName); > picture1.explore(); > picture1.decreaseRed(); > picture1.explore();

17 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% Georgia Institute of Technology

18 Faking a Sunset Algorithm Georgia Institute of Technology 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

19 Faking a Sunset Method Georgia Institute of Technology /** * 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++; }

20 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 Georgia Institute of Technology

21 Negate Algorithm Georgia Institute of Technology 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

22 Negate Method Georgia Institute of Technology /** * 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, 255 - greenValue, 255 - blueValue)); }

23 Changing to Grayscale Georgia Institute of Technology 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 (15 + 25 + 230) / 3 = 90

24 Other ideas… Create a mirror image Create quadrants Create lines Change the size Reverse the image ….


Download ppt "Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1."

Similar presentations


Ads by Google