1 / 24

CSC1401 Using Decisions in Java - 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. Learning Goals. Understand at a conceptual and practical level How to conditionally execute a statement or a block of statements

patia
Download Presentation

CSC1401 Using Decisions in Java - 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. CSC1401Using Decisions in Java - 1

  2. Recall from Alice • We only wanted to shoot a lightning bolt at a philosopher • So, we used the If statement

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

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

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

  6. 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 this double dist = pixelObj.colorDistance(color1);

  7. 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()

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

  9. 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 false if (expression) true Statement or block statement

  10. Remove Red Eye Method public 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);

  11. Remove Red Eye Method // if the color is near red then change it if (pixelObj.colorDistance(Color.red) < 167) { pixelObj.setColor(newColor); } } } }

  12. Testing removeRedEye • Use the picture explorer to find the values for start x, start y, end x, and end y

  13. 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?

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

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

  16. 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)

  17. And, or, not in Java • And && • Or || • Not !

  18. 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 or block statement

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

  20. Testing Edge Detection • String file = FileChooser.getMediaPath(“butterfly1.jpg”); • Picture p = new Picture(file); • p.explore(); • p.edgeDetection(10); • p.explore();

  21. 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?

  22. Truth Table

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

  24. Assignment • Read Media Computation Chapter 6, Sections 1-2

More Related