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,

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Conditionals-part31 Conditionals – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Using Decisions in Java - 2. Learning Goals Understand at a conceptual and practical level How to use conditionals with > 2 possibilities How.
Objectives Understand grayscale Investigate a grayscale image
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Replacing colors using if We don’t have to do one-to-one changes or replacements of color We can use if to decide if we want to make a change.  We could.
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.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
Introduction to Image processing using processing.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
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.
Chapter 5: Advanced Picture Techniques (partial deck)
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
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.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
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,
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Barbara Ericson Georgia Tech Sept 2005
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Barb Ericson Georgia Institute of Technology Dec 2009
Topic 10 The if statement Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Multimedia Summer Camp
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Barb Ericson Georgia Institute of Technology August 2005
Conditional Statements
Manipulating Pictures, Arrays, and Loops part 5
Georgia Institute of Technology
Workshop for Programming And Systems Management Teachers
The most important decision statement
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Chapter 6 Conditionals - part 4
Barb Ericson Georgia Institute of Technology May 2006
CS1315: Introduction to Media Computation
Manipulating Pictures, Arrays, and Loops
CSC1401 Manipulating Pictures 2
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

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, and instructor materials prepared by B. Ericson.

Outline 2  How to use conditionals  Picture manipulation using conditionals: Edge detection Sepia toning Chromakey (Blue-screening)

Making Decisions 3  Computer programs often have to make decisions  Taking different actions depending on some condition  Examples:  If the amount you want to withdraw is less than your bank balance, you are allowed to make the withdrawal, otherwise you are not  If a number is negative, you cannot take its square root

Making Decisions 4  More examples:  If a turtle is too close to the edge of its world, it cannot move forward the specified distance  If a red value for a pixel is already 255, you can’t increase it  Making decisions in high-level programming languages is called conditional execution and is done using the if statement and if-else statement

If Statement 5  A statement or block of statements is executed only if some condition is true  If the condition is false, execution falls through to the next statement following the if statement Next statement if (condition) true false Statement or block

If Statement Syntax 6  We may want a single statement to be executed if the condition is true: if (condition) statement  We may want a block of statements to be executed if the condition is true: if (condition) { statement … statement }  Safer to always use braces, even if a single statement  For readability, make sure you line up the braces

If - else Statement 7  Using an if and else, we can do one thing if the condition is true or a different thing if the condition is false Next statement if (condition) true false Statement or block else

If - else Statement Syntax 8  If the condition is true, statement1 (or block of statements) is executed, otherwise statement2 (or block of statements) is executed if (condition) statement1 else statement2  Example: if (x < y) System.out.println("y is larger"); else System.out.println("x is larger");

Conditional Statement Example 9  Bank withdrawal example: // amount: the amount you want to withdraw // balance: your account balance if (amount <= balance) { balance = balance – amount; } System.out.println("Balance is " + balance);  What happens if amount > balance?

Conditional Statement Example 10  To let the user know that the withdrawal was not allowed if (amount <= balance) { balance = balance – amount; System.out.println("New balance is " + balance); } else { System.out.println("Sorry, you are trying to withdraw $" + amount + " and your balance is only $" + balance); }

Using Conditions in Pictures 11  Choosing which pixels to manipulate in a picture  To remove red-eye in an image  To create sepia-toned images  To posterize images (reduce the number of color)  Do blue-screen effect  etc.

Simple Color Replacement 12  Suppose we wanted to change all of the white in an image to a “bluer” white  Algorithm:  For each pixel in the picture If the pixel’s color is white Decrease the red and green in this color

Simple Color Replacement Method 13 public void makeWhiteMoreBlue() { for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel pixelObj = this.getPixel(x,y); int red = pixelObj.getRed(); int green = pixelObj.getGreen(); int blue = pixelObj.getBlue(); if (red == 255 && green == 255 && blue == 255) { pixelObj.setRed(200); pixelObj.setGreen(200); }

A better method 14  Allow more flexibility:  Replace the color when all red, green and blue are above a threshold  In this case, reduce red and green, and increase blue

Simple Color Replacement Method 15 public void makeWhiteMoreBlue() { for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel pixelObj = this.getPixel(x,y); int red = pixelObj.getRed(); int green = pixelObj.getGreen(); int blue = pixelObj.getBlue(); if (red >= 200 && green >= 200 && blue >= 200) { pixelObj.setRed(200); pixelObj.setGreen(200); pixelObj.setBlue(255); }

Multiple if statements 16  Suppose we are doing different things based on a set of ranges, for example: 0 <= x <= 5 5 < x <= < 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 17  Or we can use several if- else statements  You don’t need to check if x > 5, since the first if block would have been executed if it was <= 5 if (0 <= x && x <= 5) statement or block else if (x <= 10) statement or block else // what must x be? statement or block

Sepia-Toned Pictures 18  They have a yellowish tint, used to make things look old

Sepia-Toned Algorithm 19  First make the picture grayscale  Change the darkest grays (shadows) to be even darker  Make the middle grays a brown color  By reducing the blue  Make the lightest grays (highlights) a bit yellow  By increasing red and green  Or decreasing blue (less than before though)  We now have more than 2 possibilities, so we need to use multiple if statements

Sepia-Toned Method 20 public void sepiaTint() { // 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 its color values Pixel pixelObj = this.getPixel(x,y); int redValue = pixelObj.getRed(); int greenValue = pixelObj.getGreen(); int blueValue = pixelObj.getBlue();

Sepia-Toned Method (continued) 21 // tint the shadows darker if (redValue < 60) { redValue = (int) (redValue * 0.9); greenValue = (int) (greenValue * 0.9); blueValue = (int) (blueValue * 0.9); } // tint the midtones a light brown by reducing the blue else if (redValue < 190) { blueValue = (int) (blueValue * 0.8); } // tint the highlights a light yellow by reducing the blue else { blueValue = (int) (blueValue * 0.9); }

Sepia-Toned Method (continued) 22 // set the colors pixelObj.setRed(redValue); pixelObj.setGreen(greenValue); pixelObj.setBlue(blueValue); }

Dangling Else Problem 23  Consider this example: if (x > 0) if (x < 100) System.out.println(“one” ); else System.out.println(“two”);  Suppose x has the value –2. What do you think will be printed?

Dangling Else Problem 24  The rule is that an else goes with the closest if  indentation makes no difference!  Now what if we had wanted to group the else with the first if ? Use braces: if (x > 0) { if (x < 100) System.out.println(“one” ); } else System.out.println(“two”);

Blue Screen 25  The weather announcers on TV are not really standing in front of a changing weather map  They are in front of a solid colour background blue or green (but usually blue – it’s better for skintones)  How do the weather map images appear behind them? If a pixel’s color is blue, replace it with color of corresponding pixel from new background image  Also used for special effects in TV and movies

Blue Screen 26  Here just a blue sheet was used  Professionally, you need an evenly lit, bright, pure blue background  With nothing blue in the part of the image you want to keep

New Background 27

Algorithm 28  Our method will be invoked on an image with a blue screen  The blue will be replaced as a background by another image passed in as a parameter  Algorithm:  Loop through all the pixels  If the pixel color is “blue” Replace the pixel color with the color from the pixel at the same location in the new background picture

Method 29 public void blueScreen(Picture newBackground) { // loop through the columns for (int x=0; x<this.getWidth(); x++) { // loop through the rows for (int y=0; y<this.getHeight(); y++) { // get the current pixel Pixel currPixel = this.getPixel(x,y);

Method (continued) 30 /* if the color of the pixel at this location is mostly blue (blue value greater than red and green combined), then use new background color at this pixel */ if (currPixel.getRed() + currPixel.getGreen() < currPixel.getBlue()) { Pixel newPixel = newBackground.getPixel(x,y); currPixel.setColor(newPixel.getColor()); }

Summary 31  Conditionals  Nested Conditionals  Dangling Else  Picture Algorithms that use conditionals