CSC 1401 Using Decisions in Java 1 Recall

  • Slides: 24
Download presentation
CSC 1401 Using Decisions in Java - 1

CSC 1401 Using Decisions in Java - 1

Recall from Alice We only wanted to shoot a lightning bolt at a philosopher

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

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

Remove Red Eye Red eye is when the flash from the camera is reflected

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

Red Eye Algorithm How do we know if the color of a pixel is

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

Color Distance The distance between two points is computed as Square root of ((

Color Distance The distance between two points is computed as Square root of (( x 1 – x 2)2 + (y 1 – y 2)2) The distance between two colors can be computed Square root of ((red 1 – red 2)2 + (green 1 green 2)2 + (blue 1 – blue 2)2) There is a method in the Pixel class to do this double dist = pixel. Obj. color. Distance(color 1);

Red Eye algorithm How do we only remove the red in her eyes, without

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 picture. Obj. explore()

Detailed Red Eye Algorithm Loop with x staring at some passed start value and

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

Conditional Execution Sometimes we want a statement or block of statements executed only if

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 (color. Distance < value) Statement or block to execute next statement false if (expression) true Statement or block statement

Remove Red Eye Method public void remove. Red. Eye(int start. X, int start. Y,

Remove Red Eye Method public void remove. Red. Eye(int start. X, int start. Y, int end. X, int end. Y, Color new. Color) { Pixel pixel. Obj; // loop through the pixels in the rectangle defined by the // start. X, start. Y, and end. X and end. Y for (int x = start. X; x < end. X; x++) { for (int y = start. Y; y < end. Y; y++) { // get the current pixel. Obj = get. Pixel(x, y);

Remove Red Eye Method // if the color is near red then change it

Remove Red Eye Method // if the color is near red then change it if (pixel. Obj. color. Distance(Color. red) < 167) { pixel. Obj. set. Color(new. Color); } }

Testing remove. Red. Eye Use the picture explorer to find the values for start

Testing remove. Red. Eye Use the picture explorer to find the values for start x, start y, end x, and end y

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

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?

A 2 nd example: Edge Detection Loop through all the pixels in the picture

A 2 nd 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

Edge Detection Algorithm To find areas of high contrast Try to loop from row

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 = width Get 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

How do we determine absolute value in Java? To test if the absolute value

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)

And, or, not in Java And Or Not && || !

And, or, not in Java And Or Not && || !

Use if and else for two possibilities Sometimes you want to do one thing

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 false int x = 200; if (x < 128) {System. out. println(“<128”); } else {System. out. println(“>=128”); } false if (expression) else true Statement or block statement Statement or block

Edge Detection Exercise Write a method edge. Detection that takes an input limit And

Edge Detection Exercise Write a method edge. Detection 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 get. Average() method that returns the average of the three colors at the pixel

Testing Edge Detection String file = File. Chooser. get. Media. Path(“butterfly 1. jpg”); Picture

Testing Edge Detection String file = File. Chooser. get. Media. Path(“butterfly 1. jpg”); Picture p = new Picture(file); p. explore(); p. edge. Detection(10); p. explore();

Challenge Create another method for simple edge detection This time compare the current pixel

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?

Truth Table Conditional Operand 1 Operand 2 Result And - && true And true

Truth Table Conditional Operand 1 Operand 2 Result And - && true And true false And false false Or – || true Or true false true Or false Not - ! true false Not false true

Summary Use if and else (the else is optional) if you have two possibilities

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 true Use ‘||’ to test if at least one thing is true Use ‘!’ to change the result from true to false and false to true

Assignment Read Media Computation Chapter 6, Sections 1 -2

Assignment Read Media Computation Chapter 6, Sections 1 -2