Pictures Looping through pixels.. Lab Review (1) Objects Instantiated from Class Turtle myTut =...

23
Pictures Looping through pixels.

Transcript of Pictures Looping through pixels.. Lab Review (1) Objects Instantiated from Class Turtle myTut =...

Page 1: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

Pictures

Looping through pixels.

Page 2: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 3: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

Lab Review (2)

Methods Send message to object by calling

method myTut.turnLeft();

Argument can be supplied as a parameter myTut.forward(45);

Page 4: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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 */

Page 5: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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.

Page 6: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 7: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 8: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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;

Page 9: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

0 1 2 3

Row Major Order

pictData [r] [c]

0 1 2 3

0

1

Column Major Order

pictData [c] [r]

Page 10: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 11: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

Digital Images – Color Models

RGB – Red Green Blue HSV – Hue Saturation and Value CMYK – Cyan, Magenta, Yellow, Black

Used by printers

Page 12: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 13: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

Creating an Image

String fileName = FileChooser.pickAFile();

Picture pict = new Picture(fileName);

// show the image

pict.show();

// explore the image

pict.expore();

Page 14: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 15: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 16: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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?

Page 17: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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 { }

Page 18: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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); }}

Page 19: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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

Page 20: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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; }}

Page 21: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

For Loop

Loop with a built-in counter – also called a counting loop

Syntax for (init; limit test; change)

statement; Advice: always use braces

Page 22: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

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);

}

}

Page 23: Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.

Scope

For loop from previous slidefor (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.