1 / 24

Manipulating Pictures, Arrays, and Loops part 1

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

delu
Download Presentation

Manipulating Pictures, Arrays, and Loops part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Manipulating Pictures, Arrays, and Loopspart 1 Georgia Institute of Technology

  2. Digital Pictures • 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 Georgia Institute of Technology

  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? • 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() Georgia Institute of Technology

  5. Picture Exercise • 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”); Georgia Institute of Technology

  6. Pixel Objects • 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) Georgia Institute of Technology

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

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

  10. Pixel Exercise • In DrJava • Pick a file and create a picture object • Get the array of pixels from the picture object • Get the 1st 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 Georgia Institute of Technology

  11. Changing Pixel Colors • 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(); Georgia Institute of Technology

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

  13. Changing a Picture Exercise > 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(); Georgia Institute of Technology

  14. Method to Decrease Red • 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 {} Georgia Institute of Technology

  15. Decrease Red Method 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; } Georgia Institute of Technology

  16. Decrease Red Exercise • 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(); Georgia Institute of Technology

  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 • Reduce the blue and green 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 • Set the blue value at the pixel to 0.7 times the original value • Set the green value at the pixel to 0.7 times the original value • Increment the index and go back to step 3 Georgia Institute of Technology

  19. 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++; } } Georgia Institute of Technology

  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 • 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

  22. 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

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

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

More Related