CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
New Mexico Computer Science For All Booleans and Logic Maureen Psaila-Dombrowski.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
CSC1401. Learning Goals Understand at a conceptual level What is media computation? How does color vision work? How can you make colors with red, green,
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
Mathematical Expressions, Conditional Statements, Control Structures
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
Day 8: Fruit Loops: Color. Loop Review What does the following loop print? for (int i= 0; i
Algorithms Writing instructions in the order they should execute.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Lesson Two: Everything You Need to Know
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 10 THE IF STATEMENT 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Control statements Mostafa Abdallah
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
CSC1401 Drawing in Java - 1. Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Georgia Institute of Technology
Topic 10 The if statement Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
Workshop for Programming And Systems Management Teachers
CSE 8A Lecture 6 Reading for next class:
The most important decision statement
Practice with loops! What is the output of each function below?
Two-Dimensional Arrays and Nested Loops – part 4
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
CSC1401 Viewing a picture as a 2D image - 2
Program Flow.
Barb Ericson Georgia Institute of Technology May 2006
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CSC1401 Manipulating Pictures 2
Manipulating Pictures, Arrays, and Loops part 6
Control Structures.
Day 11 The Last Week!.
Presentation transcript:

CSC1401 Using 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 this double 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 Method public 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 2 nd 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 = 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

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 false int x = 200; if (x < 128) {System.out.println(“<128”);} else {System.out.println(“>=128”);} statement if (expression) true false 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 Table ConditionalOperand 1Operand 2Result And - && true Andtruefalse Andfalsetruefalse Andfalse Or – || true Ortruefalsetrue Orfalsetrue Orfalse Not - ! truefalse Notfalsetrue

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 true Use ‘||’ to test if at least one thing is true Use ‘!’ to change the result from true to false and false to true

Assignment Read Media Computation Chapter 6, Sections 1-2