Presentation is loading. Please wait.

Presentation is loading. Please wait.

Manipulating Pictures, Arrays, and Loops part 6

Similar presentations


Presentation on theme: "Manipulating Pictures, Arrays, and Loops part 6"— Presentation transcript:

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

2 Georgia Institute of Technology
Learning Goals Understand at a conceptual and practical level How to change all the colors in a method How to use a for loop How to negate a picture How to turn a picture into grayscale How to adjust the grayscale based on how we perceive colors Georgia Institute of Technology

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

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

5 Georgia Institute of Technology
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, 255 - greenValue, 255 - blueValue)); } Georgia Institute of Technology

6 Georgia Institute of Technology
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 To average x numbers get the sum of all the numbers and then divide by x. Georgia Institute of Technology

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

8 Georgia Institute of Technology
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)); } Georgia Institute of Technology

9 Georgia Institute of Technology
Testing Grayscale String file = “c:/intro-prog-java/mediasources/caterpillar.jpg”; Picture pictureObj = new Picture(file); pictureObj.explore(); pictureObj.grayscale(); Georgia Institute of Technology

10 Georgia Institute of Technology
Grayscale Result Georgia Institute of Technology

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

12 Grayscale with Luminance Exercise
Create a new method grayscaleWithLuminance Using the new algorithm for calculating intensity intensity = (int) (red * green * blue * 0.114) Georgia Institute of Technology

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

14 Georgia Institute of Technology
Summary You can change all the colors in a method By creating a new color object You can negate a picture By creating a new color with (255 - red, green, blue) You can create a grayscale picture By setting the red, green, and blue to the same value You can improve this by weighting the colors based on how we perceive them Georgia Institute of Technology


Download ppt "Manipulating Pictures, Arrays, and Loops part 6"

Similar presentations


Ads by Google