Download presentation
Presentation is loading. Please wait.
Published byAnis Annis Mason Modified over 6 years ago
1
Workshop for Programming And Systems Management Teachers
Chapter 7 Conditionals Georgia Institute of Technology
2
Georgia Institute of Technology
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 chromakey or blue screen Georgia Institute of Technology
3
Georgia Institute of Technology
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 See for more information on red-eye. Georgia Institute of Technology
4
Georgia Institute of Technology
Red Eye Algorithm We can find the area around the eyes to limit where we change the colors Using picture.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 Use indentation to make your code easier for other people to read. It doesn’t matter to the compiler. Remember that a block of statements in Java is inside {}. statement Georgia Institute of Technology
7
Georgia Institute of Technology
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; Georgia Institute of Technology
8
Georgia Institute of Technology
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
Georgia Institute of Technology
Remove Red Eye Method /** * Method to remove red eye from the current picture object in the rectange * define by startX, startY, endX, endY. The red will be replaced with the * passed newColor startX the top left corner x value of a rectangle startY the top left corner y value of a rectangle endX the bottom right corner x value of a rectangle endY the bottom right corner y value of a rectangle newColor the new color to use */ public void removeRedEye(int startX, int startY, int endX, int endY, Color newColor) { Pixel pixel = 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 pixel = getPixel(x,y); // if the color is near red then change it if (pixel.colorDistance(Color.red) < 167) pixel.setColor(newColor); } Georgia Institute of Technology
10
Georgia Institute of Technology
Edge Detection Find the areas of high contrast and turn pixels is this area black Turn all other pixels white The image is from foal.jpg in mediasources after doing edge detection with a limit of 10. Georgia Institute of Technology
11
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
12
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 (like x > 128 and x <= 128) int x = 24; if (x > 128) System.out.println(“x is > 128”); else System.out.println(“x <= 128”); Georgia Institute of Technology
13
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
14
Georgia Institute of Technology
Sepia-Toned Pictures Have a yellowish tint, used to make things look old and western Georgia Institute of Technology
15
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
16
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 ^ The conditional operators && and || are ‘short circuit’ operators. They stop evaluation early when the result is obvious from evaluating the first operand. See for Sun’s tutorial on conditional operators. Georgia Institute of Technology
17
Georgia Institute of Technology
Conditional Examples Check that a value is in a range Is some value between 0 and 255 (inclusive) for valid pixel color values 0 <= 13 <= is written as 0 <= 13 && 13 <= 255 // in Java Check if 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
18
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 catapillarClipart.jpg Georgia Institute of Technology
19
Georgia Institute of Technology
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
20
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
21
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
22
Georgia Institute of Technology
Truth Table Conditional Operand 1 Operand 2 Result And true false Or Exclusive Or An and conditional is only true when both operands are true. An or conditional is true as long as either operand is true. An exclusive or conditional is true only if one, but not both, operands are true. Georgia Institute of Technology
23
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 See for information on DeMorgan’s Law Georgia Institute of Technology
24
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) if (10 < x) Georgia Institute of Technology
25
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) else Georgia Institute of Technology
26
Georgia Institute of Technology
Sepia-toned Method public void sepiaTint() { Pixel pixel = null; double redValue = 0; double greenValue = 0; double blueValue = 0; // first change the current picture to grayscale this.grayscale(); // 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 pixel = this.getPixel(x,y); redValue = pixel.getRed(); greenValue = pixel.getGreen(); blueValue = pixel.getBlue(); // tint the shadows darker if (redValue < 60) redValue = redValue * 0.9; greenValue = greenValue * 0.9; blueValue = blueValue * 0.9; } Georgia Institute of Technology
27
Sepia-toned Method - Continued
// tint the midtones a light brown // by reducing the blue else if (redValue < 190) { blueValue = blueValue * 0.8; } // tint the highlights a light yellow else blueValue = blueValue * 0.9; // set the color pixel.setColor (new Color((int) redValue, (int) greenValue, (int) blueValue)); Georgia Institute of Technology
28
Georgia Institute of Technology
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 The image here is part of market.jpg Georgia Institute of Technology
29
Georgia Institute of Technology
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
30
Georgia Institute of Technology
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 Georgia Institute of Technology
31
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
32
Georgia Institute of Technology
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
33
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
34
Replace Background Method
public void swapBackgroundForThreshold(Picture oldBackground, Picture newBackground, double threshold) { Pixel currPixel = null; Pixel oldPixel = null; Pixel newPixel = null; // loop through the columns for (int x=0; x<getWidth(); x++) // loop through the rows for (int y=0; y<getHeight(); y++) // get the current pixel and old background pixel currPixel = this.getPixel(x,y); oldPixel = oldBackground.getPixel(x,y); /* if the color at the current pixel is within 15.0 of the old background pixel * then swap in the new background pixel */ if (currPixel.colorDistance(oldPixel.getColor()) < threshold) newPixel = newBackground.getPixel(x,y); currPixel.setColor(newPixel.getColor()); } Georgia Institute of Technology
35
Replace Background Result
The background color was too close to the shirt color The source picture was kid-in-frame.jpg in mediasources. The old background was in bgframe.jpg. The new background was in moon-surface.jpg. Georgia Institute of Technology
36
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 See for more information Georgia Institute of Technology
37
Georgia Institute of Technology
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) The source picture is blue-mark.jpg. The blue background is blue-empty.jpg. The moon picture is moon-surface.jpg. Georgia Institute of Technology
38
Georgia Institute of Technology
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 Georgia Institute of Technology
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.