Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | jeffrey-perry |
View: | 220 times |
Download: | 0 times |
CSC1401Using 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
How to remove red-eye from a picture
How to use conditionals with two possibilities
How to do simple edge detection
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
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
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 thisdouble dist = pixelObj.colorDistance(color1);
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()
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 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
statement
if (expression)
true
false
Statement or block
Remove Red Eye Methodpublic 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);
Remove Red Eye Method
// if the color is near red then change it
if (pixelObj.colorDistance(Color.red) < 167)
{
pixelObj.setColor(newColor);
}
}
}
}
Testing removeRedEye
Use the picture explorer to find the values for start x, start y, end x, and end y
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?
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
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 = widthGet 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
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)
And, or, not in Java
And &&
Or ||
Not !
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 falseint x = 200;
if (x < 128)
{System.out.println(“<128”);}
else
{System.out.println(“>=128”);}
statement
if (expression)
true
false
Statement or block
Statement or block
else
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
Testing Edge Detection
String file = FileChooser.getMediaPath(“butterfly1.jpg”);
Picture p = new Picture(file);
p.explore();
p.edgeDetection(10);
p.explore();
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?
Truth TableConditional Operand 1 Operand 2 Result
And - && true true true
And true false false
And false true false
And false false false
Or – || true true true
Or true false true
Or false true true
Or false false false
Not - ! true false
Not false true
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 trueUse ‘||’ to test if at least one thing is trueUse ‘!’ to change the result from true to false and false to true
Assignment
Read Media Computation Chapter 6, Sections 1-2