1 / 23

Pictures

Pictures. Looping through pixels. Lab Review (1). Objects Instantiated from Class Turtle myTut = new Turtle(myWorld); new operator creates an instance of the class Constructor Constructs (initializes) object Has same name as class May need to supply arguments as parameters.

Download Presentation

Pictures

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. Pictures Looping through pixels.

  2. Lab Review (1) • Objects • Instantiated from Class • Turtle myTut = new Turtle(myWorld); • new operator creates an instance of the class • Constructor • Constructs (initializes) object • Has same name as class • May need to supply arguments as parameters

  3. Lab Review (2) • Methods • Send message to object by calling method • myTut.turnLeft(); • Argument can be supplied as a parameter • myTut.forward(45);

  4. Lab Review (3) /** * Draws a square with side width pixels at the current position * in the world. * * @param width length of the side */ public void drawSquare(int width) { //draw side one this.forward(width); // turn 90 deg counterclockwise this.turnLeft(); // draw side two … } For main program: /** This program draws uses the Turtle to draw nested squares. @author S. Sigman @version 2/4/2007 */

  5. Lab Review (4) • Using the Java packages require that the class or classes be made available to the compiler. • import statement used to do this • import java.awt.Color; • Put at top of the file • import java.awt.*; makes all classes in java.awt available.

  6. Problem • Manipulate a digital image • make a picture brighter/darker • alter the color in a picture • In computer science we must often start by learning about the domain in which we are writing programs. • Often requires us to learn more about our language

  7. Digital Images • How are images encoded? • Basic Idea: Represent image as a 2-dimensional matrix of colored dots. • Human Visual Perception • Humans have low visual acuity • We see color and luminance (black and white) using different neural pathways in our brain • Luminance is perceived with our peripheral vision

  8. Digital Images - Arrays • Arrays are a contiguous collections of cells homogeneous data where each cell is identified by an index. • int [] score = {23, 25, 22, 25}; • score[0] is 23; score[1] is 25; • score[3] is ? • Arrays are objects. The assignment above is a shorthand that hides the new operator. • int [] grades = new int [10]; // declares and // allocates • grades[0] = 35;

  9. Digital Images – Matrices • Matrices are 2-dimensional arrays • In Java a matrix is an array of arrays • An image has type: Pixel [][] pictData; 0 1 2 3 0 1 2 3 0 1 0 Row Major Order pictData [r] [c] 1 Column Major Order pictData [c] [r]

  10. Digital Images - Color • Visible light is continuous • Wavelength: 370 nm – 730 nm • We have red, green, blue receptors • red peak: 560 nm • green peak: 550 nm • blue peak: 425 nm • Color we see is based upon signals from recptors

  11. Digital Images – Color Models • RGB – Red Green Blue • HSV – Hue Saturation and Value • CMYK – Cyan, Magenta, Yellow, Black • Used by printers

  12. Digital Image - RGP • Three Channels • Red, Green, Blue • Value Range: 0 – 255 • 24 bit color • 16 million colors • Does not cover range of colors we perceive • 32 bit color • 16 million colors • alpha channel – transparency

  13. Creating an Image String fileName = FileChooser.pickAFile(); Picture pict = new Picture(fileName); // show the image pict.show(); // explore the image pict.expore();

  14. Looking at Pixels • Pixel pix = pict.getPixel(0,0); • int xCord = pix.getX(); int yCord = pix.getY(); • int redVal = pix.getRed(); • pix.setRed(255); pix.setGreen(0); pix.setBlue(0); • pict.repaint(); // must repaint to show // change

  15. Looking at Pixels (2) • Pixel [] imgData = pict.getPixels(); pict.getPixel(10,100).setColor(Color.black); pict.getPixel(11,100).setColor(Color.black); pict.getPixel(12,100).setColor(Color.black); pict.getPixel(13,100).setColor(Color.black); pict.repaint(); • This process is a little laborious!!

  16. Changing The Green Component For All Pixels • Push the green component to 255 for all pixels. • Algorithm • Get an array of pixels in image • For each pixel in the image • set the green component to 255 • How do we do the for each?

  17. The For Each Loop • Allows us to repeat a set of Java commands for each element in an array. • Syntax • for (type variableName: array) statement; • for multiple statements enclose in { }

  18. Method to Maximize Green public void maxGreen() { // get the pixels Pixel [] pixels = this.getPixels(); // set the pixels to max green for(Pixel curPix : pixels) { curPix.setGreen(255); } }

  19. While Loops • Java provides other ways to construct loops • While loop • loop while some condition is true • syntax • while (test) statement; • Advice: always use braces

  20. Maximize Green With a While Loop public void maxGreen() { // get the pixels Pixel [] pixels = this.getPixels(); // set the pixels to max green int curPos = 0; while (curPos < pixels.length) { pixels[curPos].setGreen(255); curPos++; // same as curPos = curPos + 1; } }

  21. For Loop • Loop with a built-in counter – also called a counting loop • Syntax • for (init; limit test; change) statement; • Advice: always use braces

  22. Maximize Green With a For Loop public void maxGreen() { // get the pixels Pixel [] pixels = this.getPixels(); // set the pixels to max green for (int i=0;i < pixels.length; i++) { pixels[i].setGreen(255); } }

  23. Scope • For loop from previous slide for (int i=0;i < pixels.length; i++) { pixels[i].setGreen(255); } • Where is i’s value know? • Just inside the block the defines the body of the loop. This is i’s scope. • Look at scope in Picture. • Principle: Variables scope should be as small as possible. Called localization.

More Related