Conditionals Barb Ericson Georgia Institute of Technology June
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
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 • We can find the area around the eyes to limit where we change the colors – Using picture. Obj. 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
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 executed only if some expression is true – We can use the “if” statement in Java false if (expression) true Statement or block if (color. Distance < value) Statement or block to execute next statement
Using if Exercise • Open Dr. Java 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);
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 Picture class to do this • public double get. Color. Distance(color 1, color 2)
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 = null; // 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 if (pixel. Obj. color. Distance(Color. red) < 167) pixel. Obj. set. Color(new. Color); } } }
Testing remove. Red. Eye • String file = File. Chooser. get. Media. Path(“jenny-red. jpg”); • • Picture p = new Picture(file); p. explore(); p. remove. Red. Eye(); p. explore();
Edge Detection • Find the areas of high contrast and turn pixels is this area black – Turn all other pixels white
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
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 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
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();
Sepia-Toned Pictures • Have a yellowish tint, used to make things look old and western
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
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 ^
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
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
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
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?
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?
Truth Table Conditional Operand 1 Operand 2 Result And true And true false And false false Or true Or true false true Or false Exclusive Or true false true Exclusive Or false false
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 – !(op 1 && op 2) = !op 1 || !op 2 – !(op 1 || op 2) = !op 1 && !op 2 • This is known as De Morgan’s Law
Using Multiple If Statements • 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 “else if” for > 2 Options • 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
Conditionals with > 2 Choices if (0 <= x && x <= 5) { } else if (x <= 10) { } else // what is x? { }
Sepia-toned Method public void sepia. Tint() { Pixel pixel. Obj = null; double red. Value = 0; double green. Value = 0; double blue. Value = 0; // first change the current picture to grayscale this. grayscale();
Sepia-toned Method - Cont // loop through the pixels for (int x = 0; x < this. get. Width(); x++) { for (int y = 0; y < this. get. Height(); y++) { // get the current pixel and color values pixel. Obj = this. get. Pixel(x, y); red. Value = pixel. Obj. get. Red(); green. Value = pixel. Obj. get. Green(); blue. Value = pixel. Obj. get. Blue();
Sepia-toned Method - Cont // tint the shadows darker if (red. Value < 60) { red. Value = red. Value * 0. 9; green. Value = green. Value * 0. 9; blue. Value = blue. Value * 0. 9; } // tint the midtones a light brown by reducing the blue else if (red. Value < 190) { blue. Value = blue. Value * 0. 8; }
Sepia-toned Method - Cont // tint the highlights a light yellow // by reducing the blue else { blue. Value = blue. Value * 0. 9; } // set the colors pixel. Obj. set. Red((int) red. Value); pixel. Obj. set. Green((int) green. Value); pixel. Obj. set. Blue((int) blue. Value); } } }
Testing sepia. Tint • String file = File. Chooser. get. Media. Path(“gorge. jpg”); • Picture p = new Picture(file); • p. explore(); • p. sepia. Tint(); • p. explore();
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
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
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
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?
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
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
Replace Background Method public void swap. Background(Picture old. Background, Picture new. Background) { Pixel curr. Pixel = null; Pixel old. Pixel = null; Pixel new. Pixel = null; // loop through the columns for (int x=0; x<this. get. Width(); x++) { // loop through the rows for (int y=0; y<this. get. Height(); y++) {
Swap Background - Cont // get the current pixel and old background pixel curr. Pixel = this. get. Pixel(x, y); old. Pixel = old. Background. get. Pixel(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 (curr. Pixel. color. Distance(old. Pixel. get. Color()) < 15. 0) { new. Pixel = new. Background. get. Pixel(x, y); curr. Pixel. set. Color(new. Pixel. get. Color()); } }
Replace Background Result • The background color was too close to the shirt color
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
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)
Summary • Use if, if-else, or 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
- Slides: 46