+ All Categories
Home > Documents > CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a...

CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a...

Date post: 18-Jan-2016
Category:
Upload: jeffrey-perry
View: 220 times
Download: 0 times
Share this document with a friend
24
CSC1401 Using Decisions in Java - 1
Transcript
Page 1: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

CSC1401Using Decisions in Java - 1

Page 2: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Recall from Alice

We only wanted to shoot a lightning bolt at a philosopher

So, we used the If statement

Page 3: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Learning Goals

Understand at a conceptual and practical level

How to conditionally execute a statement or a block of statements

How to remove red-eye from a picture

How to use conditionals with two possibilities

How to do simple edge detection

Page 4: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Remove Red Eye

Red eye is when the flash from the camera is reflected from the subject’s eyes

We want to change the red color in the eyes to another color

But not change the red of her dress

Page 5: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Red Eye Algorithm

How do we know if the color of a pixel is “mostly” red?

We can find the distance between the current color and our definition of red

And change the color of the current pixel only if the current color is within some distance to the desired color

Page 6: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Color Distance

The distance between two points is computed as

Square root of (( x1 – x2)2 + (y1 – y2)2)

The distance between two colors can be computed

Square root of ((red1 – red2)2 + (green1-green2)2 + (blue1 – blue2)2)

There is a method in the Pixel class to do thisdouble dist = pixelObj.colorDistance(color1);

Page 7: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Red Eye algorithm

How do we only remove the red in her eyes, without removing the red from her sweater?

We can find the area around the eyes to limit where we change the colors

Using pictureObj.explore()

Page 8: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Detailed Red Eye Algorithm

Loop with x staring at some passed start value and while it is less than some passed end value

Loop with y starting at some passed start value and while it is less than some passed end value

Get the pixel at this x and y

Get the distance between the pixel color and red

If the distance is less than some value (167) change the color to some passed new color

Page 9: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Conditional Execution

Sometimes we want a statement or block of statements executed only if some expression is true

We can use the “if” statement in Java

if (colorDistance < value) Statement or block to

execute

next statement

statement

if (expression)

true

false

Statement or block

Page 10: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Remove Red Eye Methodpublic void removeRedEye(int startX, int startY, int endX, int endY, Color newColor) { Pixel pixelObj; // loop through the pixels in the rectangle defined by the // startX,

startY, and endX and endY for (int x = startX; x < endX; x++) { for (int y = startY; y < endY; y++) {

// get the current pixel pixelObj = getPixel(x,y);

Page 11: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Remove Red Eye Method

// if the color is near red then change it

if (pixelObj.colorDistance(Color.red) < 167)

{

pixelObj.setColor(newColor);

}

}

}

}

Page 12: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Testing removeRedEye

Use the picture explorer to find the values for start x, start y, end x, and end y

Page 13: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Challenge

Take a picture of a friend And try to change their eye color

Try to change their hair color

Try to change their clothing color

Can you write one method to do this?And call it several times with different parameters?

Page 14: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

A 2nd example: Edge Detection

Loop through all the pixels in the picture

Calculate the average color for the current pixel and the pixel at the same x but y+1.

Get the distance between the two averages

If the absolute value of the distance is greater than some value turn the current pixel black

Otherwise turn the current pixel white

Page 15: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Edge Detection Algorithm

To find areas of high contrast Try to loop from row = 0 to row = height – 1

Loop from x = 0 to x = widthGet the pixel at the x and y (top pixel)

Get the pixel at the x and (y + 1) bottom pixel

Get the average of the top pixel color values

Get the average of the bottom pixel color values

If the absolute value of the difference between the averages is over a passed limit

• Turn the pixel black• Otherwise turn the pixel white

Page 16: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

How do we determine absolute value in Java?

To test if the absolute value of a and b is less than 20, we can write:

if ((a – b < 20) || (b – a) < 20))

Or we can write:if (Math.abs(a-b) < 20)

Page 17: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

And, or, not in Java

And &&

Or ||

Not !

Page 18: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Use if and else for two possibilities

Sometimes you want to do one thing if the expression is true

and a different thing if it is falseint x = 200;

if (x < 128)

{System.out.println(“<128”);}

else

{System.out.println(“>=128”);}

statement

if (expression)

true

false

Statement or block

Statement or block

else

Page 19: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Edge Detection Exercise

Write a method edgeDetection that takes an input limit

And turns all pixels black where the absolute value of the difference between that pixel and the below pixel is greater than the passed limit

And turns all pixels white where the absolute value of the difference between that pixel and the below pixel is less than or equal the passed limit

Pixel has a getAverage() method that returns the average of the three colors at the pixel

Page 20: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Testing Edge Detection

String file = FileChooser.getMediaPath(“butterfly1.jpg”);

Picture p = new Picture(file);

p.explore();

p.edgeDetection(10);

p.explore();

Page 21: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Challenge

Create another method for simple edge detection

This time compare the current pixel with the one to the right (x+1)

How do you need to change the nested loop?

Do you get a different result?

Page 22: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Truth TableConditional Operand 1 Operand 2 Result

And - && true true true

And true false false

And false true false

And false false false

Or – || true true true

Or true false true

Or false true true

Or false false false

Not - ! true false

Not false true

Page 23: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Summary

Use if and else (the else is optional) if you have two possibilities to deal with

if (test){ // statements to execute when the test is true}else { // statements to execute when the test is false}

Complex conditionals Use ‘&&’ to test for more than one thing being trueUse ‘||’ to test if at least one thing is trueUse ‘!’ to change the result from true to false and false to true

Page 24: CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Assignment

Read Media Computation Chapter 6, Sections 1-2


Recommended