Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.

Similar presentations


Presentation on theme: "Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007."— Presentation transcript:

1 Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007

2 Conditionals-Mod8-part22 Learning Goals Understand at a conceptual and practical level –How to use conditionals with two possibilities –How to do simple edge detection –How to use ‘and’, ‘or’, ‘exclusive or’ and ‘not’ in a conditional –What is De Morgan’s Law? –Use complex conditionals for edge detection

3 Conditionals-Mod8-part23 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? 1. AND = everything is true

4 Conditionals-Mod8-part24 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? 2. OR = at least 1 is true

5 Conditionals-Mod8-part25 3. Exclusive OR Tonight at 7pm I’m going to the movies or to visit my grandmother –How many places can you go at 7pm? I’m getting an A or B in my history class. –How many grades will you get for history? Exclusive OR means one or the other, but not both. It’s written A B

6 Conditionals-Mod8-part26 4. 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 ^ in Java

7 Conditionals-Mod8-part27 Conditional Exercise What is the result from the following code? int x = 3; int y = 4; if (x 5) System.out.println(“both are true”); else System.out.println(“one operand is false”);

8 Conditionals-Mod8-part28 Conditional Exercise What is the result from the following code? int x = 3; int y = 6; if (x 5) System.out.println(“both true”); else System.out.println(“one operand is false”);

9 Conditionals-Mod8-part29 Conditional Exercise What is the result from the following code? int x = 4; int y = 4; if (x 5) System.out.println(“or is true”); else System.out.println(“or is false”);

10 Conditionals-Mod8-part210 Conditional Exercise What is the result from the following code? int x = 4; int y = 4; if (x 5) System.out.println(“or is true”); else System.out.println(“or is false”);

11 Conditionals-Mod8-part211 5. 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 or –x >= 0 && x <= 255 // is the same 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

12 Conditionals-Mod8-part212 6. 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 LawDe Morgan’s Law

13 Conditionals-Mod8-part213 7. De Morgan’s Law Exercise What is equivalent to the following? !(x > 4 && x < 8) !(y > 2 || y < 10) !(x == 2 && y == 4) !(y != 2 && x != 3) !(x == 3 || x == 5) !(y == 2 || y < 5) Answers (!(x>4)||!(x =8) (!(y>2)&&!(y<10)) x!=2 || y!=4 y==2 || x==3 x!=3 && x!=5 y!=2 && y>=5 Does this make sense?

14 Conditionals-Mod8-part214 Edge Detection Where there is a sharp change in color, a black line will be drawn, the rest of the picture is white

15 Conditionals-Mod8-part215 To detect an edge Say the limit is 20 R=187, G=224, B=222 Average = 211 R=205, G=233, B=235 Average = 224 The difference between the averages is 224-211 = 13 Not much of a difference So it’s NOT an edge. The pixel will be white

16 Conditionals-Mod8-part216 To detect an edge Say the limit is 20 R=187, G=224, B=222 Average = 211 R=247 G=252 B=24 Average = 174 The difference between the averages is 211-174 = 37 A significant difference So IS an edge. The pixel will be black 211-174 = 37, 174-211 = -37 8. To find the difference: a-b>limit or b-a> limit

17 Conditionals-Mod8-part217 9. Edge Detection Exercise Write a method edgeDetection that takes an input limit public void edgeDetection(int limit){ –It turns all pixels black where the absolute value of the difference between that pixel and the below pixel is greater than the limit –It 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 –It uses the getAverage() method in the Pixel class that returns the average of the red, green and blue components of the colors at the pixel, p –int avg =(int) p.getAverage();

18 Conditionals-Mod8-part218 10. Edge Detection Algorithm Loop through all the pixels in the picture –Calculate the average color of the current pixel and also the pixel in the next row down (at the same x but y+1). –Get the distance between the two averages –If the distance is greater than some value (the limit) turn the current pixel black –Otherwise turn the current pixel white

19 Conditionals-Mod8-part219 11. Translate this algorithm into Java (use some of your old programs to help) To find areas of high contrast –Loop from row(y) = 0 to y < height – 1 Loop from col(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 difference between the averages is over a passed limit »Turn the pixel black »Otherwise turn the pixel white

20 Conditionals-Mod8-part220 Testing Edge Detection String file = ‘ butterfly1.jpg”; Picture p = new Picture(file); p.explore(); p.edgeDetection(10); p.explore(); The edgeDetection method goes in the Picture class

21 Conditionals-Mod8-part221 Challenge (Top Grade) 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 Conditionals-Mod8-part222 Summary Use if and else 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 ‘and’ to test for more than one thing being true –Use ‘or’ to test if at least one thing is true –Use ‘exclusive or’ to test that one and only one thing is true –Use ‘not’ to change the result from true to false and false to true


Download ppt "Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007."

Similar presentations


Ads by Google