1 / 46

Conditionals

Conditionals. Barb Ericson Georgia Institute of Technology June 2005. Learning Goals. Understand at a conceptual and practical level How to use simple conditionals How to use complex conditionals How to remove red-eye How to posterize a picture How to do chroma-key or blue screen.

jody
Download Presentation

Conditionals

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. Conditionals Barb Ericson Georgia Institute of Technology June 2005 Georgia Institute of Technology

  2. Learning Goals • Understand at a conceptual and practical level • How to use simple conditionals • How to use complex conditionals • How to remove red-eye • How to posterize a picture • How to do chroma-key or blue screen Georgia Institute of Technology

  3. 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 Georgia Institute of Technology

  4. Red Eye Algorithm • We can find the area around the eyes to limit where we change the colors • Using pictureObj.explore() • But we still just want to change the pixels that are “close to” 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 Georgia Institute of Technology

  5. 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 Georgia Institute of Technology

  6. Conditional Execution • Sometimes we want a statement 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 Georgia Institute of Technology

  7. Using if Exercise • Open DrJava and try this in the interactions pane int x = 2; if (x > 1) System.out.println(“X is > 1”); System.out.println(“X is “ + x); x = 0; if (x > 1) System.out.println(“X is > 1”); System.out.println(“X is “ + x); Georgia Institute of Technology

  8. 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 Picture class to do this • public double getColorDistance(color1,color2) Georgia Institute of Technology

  9. Remove Red Eye Method public void removeRedEye(int startX, int startY, int endX, int endY, Color newColor) { Pixel pixelObj = null; // 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); Georgia Institute of Technology

  10. Remove Red Eye Method // if the color is near red then change it if (pixelObj.colorDistance(Color.red) < 167) pixelObj.setColor(newColor); } } } Georgia Institute of Technology

  11. Testing removeRedEye • String file = FileChooser.getMediaPath(“jenny-red.jpg”); • Picture p = new Picture(file); • p.explore(); • p.removeRedEye(); • p.explore(); Georgia Institute of Technology

  12. Edge Detection • Find the areas of high contrast and turn pixels is this area black • Turn all other pixels white Georgia Institute of Technology

  13. 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 Georgia Institute of Technology

  14. 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 Georgia Institute of Technology

  15. 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 Georgia Institute of Technology

  16. Testing Edge Detection • String file = FileChooser.getMediaPath(“butterfly1.jpg”); • Picture p = new Picture(file); • p.explore(); • p.edgeDetection(10); • p.explore(); Georgia Institute of Technology

  17. Sepia-Toned Pictures • Have a yellowish tint, used to make things look old and western Georgia Institute of Technology

  18. Sepia-toned Algorithm • First make the picture grayscale. • Change the shadows (darkest grays) to be even darker (0 <= red < 60) • Make the middle grays a brown color (60 <= red < 190) • Make the highlights (lightest grays) a bit yellow (190 <= red) • Increase red and green • Or decrease blue Georgia Institute of Technology

  19. Conditional Operators • We can check if several things are true - And • Using && (evaluation stops if the first item is false) • Using & (to always evaluate both operands) • We can check if at least one of several things are true - Or • Using || (evaluation stops if the first item is true) • Using | (to always evaluate both operands) • We can check if only one and only one of the things is true – Exclusive Or • Using ^ Georgia Institute of Technology

  20. Using && (And) and || (Or) • Check that a value is in a range • Is some value between 0 and 255 (inclusive) • for valid pixel color values • 0 <= x <= 255 is written as • 0 <= x && x <= 255 // in Java • Check if at least one of several things is true • Is this black or white? • True if either it is black or it is white Georgia Institute of Technology

  21. Count White Pixels Exercise • Write a method that counts the number of white pixels (red = 255, blue = 255, green = 255) in a picture • The method should return the number of white pixels • Use it to count the number of white pixels in caterpillar.jpg Georgia Institute of Technology

  22. Conditional Exercise • When are the following true? When are they false? • You can go out if your room is clean and you did your homework • You can go out if your room is clean or you did your homework • You can go out if either your room is clean or you did your homework but not if both of these is true Georgia Institute of Technology

  23. How many when there is an “And”? • I want you to get soup, milk, bread, and yogurt at the store. • How many items will you come home with? • I want you to clean your room and mop the floor in the kitchen and wash the dishes. • How many tasks do you need to do? • I want a scoop of chocolate scoop and a scoop of vanilla. • How many scoops of ice cream is this? Georgia Institute of Technology

  24. How many when there is an “Or” • You need to help clean the house • You can clean the bathroom or the kitchen or the living room • How many jobs do you have to do? • You want to get an ice cream • The flavors you can pick from are chocolate, vanilla, strawberry, or orange sherbet • How many flavors do you need to pick for a single scoop? Georgia Institute of Technology

  25. Truth Table Georgia Institute of Technology

  26. Not Conditional Operator • Use ! To change the value to the opposite • !true = false • !false = true • A not conditional operator applied to a complex conditional changes it • !(op1 && op2) = !op1 || !op2 • !(op1 || op2) = !op1 && !op2 • This is known as De Morgan’s Law Georgia Institute of Technology

  27. If we are doing different things based on a set of ranges 0 <= x <= 5 5 < x <= 10 10 < x if (0 <= x && x <= 5) Statement or block if (5 < x && x <= 10) Statement or block if (10 < x) Statement or block Using Multiple If Statements Georgia Institute of Technology

  28. If we are doing different things based on a set of ranges 0 <= x <= 5 5 < x <= 10 10 < x if (0 <= x && x <= 5) Statement or block else if (x <= 10) Statement or block else Statement or block Using “else if” for > 2 Options Georgia Institute of Technology

  29. Conditionals with > 2 Choices if (0 <= x && x <= 5) { } else if (x <= 10) { } else // what is x? { } Georgia Institute of Technology

  30. Sepia-toned Method public void sepiaTint() { Pixel pixelObj = null; double redValue = 0; double greenValue = 0; double blueValue = 0; // first change the current picture to grayscale this.grayscale(); Georgia Institute of Technology

  31. Sepia-toned Method - Cont // loop through the pixels for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { // get the current pixel and color values pixelObj = this.getPixel(x,y); redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); Georgia Institute of Technology

  32. Sepia-toned Method - Cont // tint the shadows darker if (redValue < 60) { redValue = redValue * 0.9; greenValue = greenValue * 0.9; blueValue = blueValue * 0.9; } // tint the midtones a light brown by reducing the blue else if (redValue < 190) { blueValue = blueValue * 0.8; } Georgia Institute of Technology

  33. Sepia-toned Method - Cont // tint the highlights a light yellow // by reducing the blue else { blueValue = blueValue * 0.9; } // set the colors pixelObj.setRed((int) redValue); pixelObj.setGreen((int) greenValue); pixelObj.setBlue((int) blueValue); } } } Georgia Institute of Technology

  34. Testing sepiaTint • String file = FileChooser.getMediaPath(“gorge.jpg”); • Picture p = new Picture(file); • p.explore(); • p.sepiaTint(); • p.explore(); Georgia Institute of Technology

  35. Posterize • Reducing the number of different colors in an image • Set all values in a range to one value (the midpoint of the range) • Set all 0 to 63 to 31 • Set all 64 to 127 to 95 • Set all 128 to 191 to 159 • Set all 192 to 255 to 223 Georgia Institute of Technology

  36. Posterize Algorithm • Loop through all the pixels in an image • Get the red value for the pixel • Find the right range and set the new red value • Get the green value for the pixel • Find the right range and set the new green value • Get the blue value for the pixel • Find the right range and set the new blue value Georgia Institute of Technology

  37. Posterize Exercise • Write the method posterize() in the Picture.java class • that will reduce the number of colors by changing color values to 1 of 4 values • Set all 0 to 63 to 31 • Set all 64 to 127 to 95 • Set all 128 to 191 to 159 • Set all 192 to 255 to 223 • Test with tammy.jpg Georgia Institute of Technology

  38. Background Replacement • If you have a picture of a person in front of some background • And a picture of the background itself • Can you replace the pixel colors for the background to be from another image? Georgia Institute of Technology

  39. Replace Background • Replace the colors at all the pixels in the source image • That are within some range of the background color • Use pixels from another background image Georgia Institute of Technology

  40. Replace Background Algorithm • Works on the source picture • Pass in the original background and the new background pictures • Loop through all the pixels in the source image • Check if the distance from the source pixel color is within 15.0 of the background picture pixel color • If so replace it with the color at the new background pixel Georgia Institute of Technology

  41. Replace Background Method public void swapBackground(Picture oldBackground, Picture newBackground) { Pixel currPixel = null; Pixel oldPixel = null; Pixel newPixel = null; // loop through the columns for (int x=0; x<this.getWidth(); x++) { // loop through the rows for (int y=0; y<this.getHeight(); y++) { Georgia Institute of Technology

  42. Swap Background - Cont // get the current pixel and old background pixel currPixel = this.getPixel(x,y); oldPixel = oldBackground.getPixel(x,y); /* if the distance between the current pixel color * and the old background pixel color is less than the 15 * then swap in the new background pixel */ if (currPixel.colorDistance(oldPixel.getColor()) < 15.0) { newPixel = newBackground.getPixel(x,y); currPixel.setColor(newPixel.getColor()); } } } } Georgia Institute of Technology

  43. Replace Background Result • The background color was too close to the shirt color Georgia Institute of Technology

  44. Chromakey – Blue Screen • For TV and movie special effects they use a blue or green screen • Here just a blue sheet was used • Professionally you need an evenly lit, bright, pure blue background • With nothing blue in the scene Georgia Institute of Technology

  45. Chromakey Exercise • Write the method chromakey that takes a new background picture as an input parameter • It will loop through all the pixels • If the pixel color is blue (red + green < blue) • Replace the pixel color with the color from the new background pixel (at the same location) Georgia Institute of Technology

  46. Summary • Use if, if-else, or if, else if, else for conditional execution • Conditional Operators • Use && to require two expressions to both be true for the result to be true • Use || to allow either (or both) to be true • Use ^ to allow one and only one to be true • Use ! to change the expression from true to false and false to true Georgia Institute of Technology

More Related