Barb Ericson Georgia Institute of Technology Dec 2009

Slides:



Advertisements
Similar presentations
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Advertisements

Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
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.
Alice and Media Computation: Goals for this session Show interesting things you can do combining Alice and Media Computation. Provides a story-telling.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Image Processing & Perception Sec 9-11 Web Design.
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.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
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.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Georgia Institute of Technology Pictures and Alice Barb Ericson Georgia Institute of Technology September 2006.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
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.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
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.
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,
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Create a Halloween Computer Game in Scratch
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Topic 10 The if statement Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Chapter 5 Working with Images
Barb Ericson Georgia Institute of Technology August 2005
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
Workshop for Programming And Systems Management Teachers
Arrays versus ArrayList
Barb Ericson Georgia Institute of Technology April 2006
Barb Ericson Georgia Institute of Technology June 2006
CSE 113 A February , 2009.
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
February , 2009 CSE 113 B.
Manipulating Pictures, Arrays, and Loops part 6
Chapter 6 Conditionals - part 4
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Barb Ericson Georgia Institute of Technology Dec 2009 Conditionals – part 5 Barb Ericson Georgia Institute of Technology Dec 2009 Conditionals-part5

Learning Goals Understand at a conceptual and practical level How to blur a picture to reduce pixelation? How to change the background on a picture? How to do chromakey? How to merge Alice characters with a real background picture? Conditionals-part5

Blurring a Picture When we scale a picture up the picture can become pixelated Jagged edges instead of smooth edges We can smooth out the picture by setting a pixel's color values to the average of the surrounding pixels All pixels surrounding the current pixel We need to be careful that we don't end up outside the array Guard using x >= 0 && x < this.getWidth && y >= 0 && y < this.getHeight() Conditionals-part5

Blur Method public void blur(int numPixels) { Pixel pixel = null; Pixel samplePixel = null; int redValue = 0; int greenValue = 0; int blueValue = 0; int count = 0; // loop through the pixels for (int x=0; x < this.getWidth(); x++) for (int y=0; y < this.getHeight(); y++) // get the current pixel pixel = this.getPixel(x,y); // reset the count and red, green, and blue values count = 0; redValue = greenValue = blueValue = 0; Conditionals-part5

Blur Method - cont Conditionals-part5 /* loop through pixel numPixels before x to * numPixels after x */ for (int xSample = x - numPixels; xSample <= x + numPixels; xSample++) { for (int ySample = y - numPixels; ySample <= y + numPixels; ySample++) { /* check that we are in the range of acceptable * pixels if (xSample >= 0 && xSample < this.getWidth() && ySample >= 0 && ySample < this.getHeight()) { samplePixel = this.getPixel(xSample,ySample); redValue = redValue + samplePixel.getRed(); greenValue = greenValue + samplePixel.getGreen(); blueValue = blueValue + samplePixel.getBlue(); count = count + 1; } // use average color of surrounding pixels Color newColor = new Color(redValue / count, greenValue / count, blueValue / count); pixel.setColor(newColor); Conditionals-part5

Testing the Blur Method > Picture p = new Picture( FileChooser.getMediaPath("flower1.jpg")); > p = p.scaleUp(2); > p.explore(); > p.blur(2); Conditionals-part5

Challenge The blur method modifies the pixels that are used in later calculations Affecting the result Instead create a new picture the same width and height of the original picture and set the color in that picture based on the colors in the original picture Remember to return the resulting picture Remember to save a reference to the resulting picture Conditionals-part5

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? Conditionals-part5

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 Conditionals-part5

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 Conditionals-part5

Replace Background Method public void swapBackground(Picture oldBackground, Picture newBackground) { Pixel currPixel = null; Pixel oldPixel = null; Pixel newPixel = null; // loop through the columns for (int x=0; x<this.getWidth(); x++) // loop through the rows for (int y=0; y<this.getHeight(); y++) Conditionals-part5

Swap Background - Cont // get the current pixel and old background pixel currPixel = this.getPixel(x,y); oldPixel = oldBackground.getPixel(x,y); /* if the distance between the current pixel color * and the old background pixel color is less than the 15 * then swap in the new background pixel */ if (currPixel.colorDistance(oldPixel.getColor()) < 15.0) { newPixel = newBackground.getPixel(x,y); currPixel.setColor(newPixel.getColor()); } Conditionals-part5

Testing swapBackground Picture p1 = new Picture(FileChooser.getMediaPath( "kid-in-frame.jpg")); Picture oldBack = new Picture(FileChooser.getMediaPath("bgframe.jpg")); Picture newBack = new Picture(FileChooser.getMediaPath( "moon-surface.jpg")); p1.swapBackground(oldBack,newBack); p1.show(); Conditionals-part5

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. Conditionals-part5

Chroma Key – 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 http://entertainment.howstuffworks.com/blue-screen.htm for more information Conditionals-part5

Chroma Key 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. Conditionals-part5

Testing chromakey Picture markP = new Picture(FileChooser.getMediaPath( "blue-mark.jpg")); Picture newBack = "moon-surface.jpg")); markP.chromakey(newBack); markP.show(); Conditionals-part5

Put an Alice Character on a Picture Position an Alice character in a world in Alice Change the screen grab to use png format instead of jpg png is not a lossy compression format and jpg is Edit – Preferences – Screen Grab – image format See the instructions on how to do this in section 12.8 of the book Exploring Wonderland. Instructions are also on the Tea Party website. Conditionals-part5

Setting up the Picture (cont) Delete the ground in the Alice world Right click on it and select delete Click on World in the Object tree and then click on the Property tab Change the atmosphere color to green (or blue if the Alice character has green in it) Conditionals-part5

Save a Picture from Alice Click the Play button Drag to make the window smaller or you can also set the window size using Edit – Preferences – Rendering – Width or Height Click the "Take Picture" button This writes a picture to the desktop: capture00.png then capture01.png, etc Rename the picture and put it in mediasources Copy just the non-green pixels to another picture Conditionals-part5

Challenge Create a method that copies just the pixels in a picture that aren't close to a particular color to another picture Use this to copy an Alice character to a normal picture You can specify where to copy it to on the resulting picture You can specify the upper left corner in the source to start the copy from You can specify the bottom right corner in the source to stop at Look at the general copy method for an example of specifying the upper left and bottom right corners in the source. Conditionals-part5

Summary Use logical && to check that you don't exceed the bounds of a picture To swap the background of a picture Take a picture with a person in the scene And a picture without the person in the scene Change the pixel color to the new background color If the distance from the current pixel is within a distance to the old background color To chromakey Take a picture of a person in front of a blue screen If the blue value is greater than the red + green (for blue screen) Conditionals-part5