Conditionals part 5 Barb Ericson Georgia Institute of
Conditionals – part 5 Barb Ericson Georgia Institute of Technology Dec 2009 Conditionals-part 5 1
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-part 5 2
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. get. Width && y >= 0 && y < this. get. Height() Conditionals-part 5 3
Blur Method public void blur(int num. Pixels) { Pixel pixel = null; Pixel sample. Pixel = null; int red. Value = 0; int green. Value = 0; int blue. Value = 0; int count = 0; // loop through the pixels for (int x=0; x < this. get. Width(); x++) { for (int y=0; y < this. get. Height(); y++) { // get the current pixel = this. get. Pixel(x, y); // reset the count and red, green, and blue values count = 0; red. Value = green. Value = blue. Value = 0; Conditionals-part 5 4
Blur Method - cont /* loop through pixel num. Pixels before x to * num. Pixels after x */ for (int x. Sample = x - num. Pixels; x. Sample <= x + num. Pixels; x. Sample++) { for (int y. Sample = y - num. Pixels; y. Sample <= y + num. Pixels; y. Sample++) { /* check that we are in the range of acceptable * pixels */ if (x. Sample >= 0 && x. Sample < this. get. Width() && y. Sample >= 0 && y. Sample < this. get. Height()) { sample. Pixel = this. get. Pixel(x. Sample, y. Sample); red. Value = red. Value + sample. Pixel. get. Red(); green. Value = green. Value + sample. Pixel. get. Green(); blue. Value = blue. Value + sample. Pixel. get. Blue(); count = count + 1; } } } // use average color of surrounding pixels Color new. Color = new Color(red. Value / count, green. Value / count, blue. Value / count); pixel. set. Color(new. Color); } } Conditionals-part 5 5
Testing the Blur Method > Picture p = new Picture( File. Chooser. get. Media. Path("flower 1. jpg")); > p = p. scale. Up(2); > p. explore(); > p. blur(2); > p. explore(); Conditionals-part 5 6
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-part 5 7
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-part 5 8
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-part 5 9
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-part 5 10
Replace Background Method public void swap. Background(Picture old. Background, Picture new. Background) { Pixel curr. Pixel = null; Pixel old. Pixel = null; Pixel new. Pixel = null; // loop through the columns for (int x=0; x<this. get. Width(); x++) { // loop through the rows for (int y=0; y<this. get. Height(); y++) { Conditionals-part 5 11
Swap Background - Cont // get the current pixel and old background pixel curr. Pixel = this. get. Pixel(x, y); old. Pixel = old. Background. get. Pixel(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 (curr. Pixel. color. Distance(old. Pixel. get. Color()) < 15. 0) { new. Pixel = new. Background. get. Pixel(x, y); curr. Pixel. set. Color(new. Pixel. get. Color()); } } Conditionals-part 5 12
Testing swap. Background Picture p 1 = new Picture(File. Chooser. get. Media. Path( "kid-in-frame. jpg")); Picture old. Back = new Picture(File. Chooser. get. Media. Path("bgframe. jpg")); Picture new. Back = new Picture(File. Chooser. get. Media. Path( "moon-surface. jpg")); p 1. swap. Background(old. Back, new. Back); p 1. show(); Conditionals-part 5 13
Replace Background Result • The background color was too close to the shirt color Conditionals-part 5 14
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 Conditionals-part 5 15
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) Conditionals-part 5 16
Testing chromakey Picture mark. P = new Picture(File. Chooser. get. Media. Path( "blue-mark. jpg")); Picture new. Back = new Picture(File. Chooser. get. Media. Path( "moon-surface. jpg")); mark. P. chromakey(new. Back); mark. P. show(); Conditionals-part 5 17
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 Conditionals-part 5 18
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-part 5 19
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: capture 00. png then capture 01. png, etc – Rename the picture and put it in mediasources – Copy just the non-green pixels to another picture Conditionals-part 5 20
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 Conditionals-part 5 21
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 – Change the pixel color to the new background color • If the blue value is greater than the red + green (for blue screen) Conditionals-part 5 22
- Slides: 22