LAB 20 – Conditionally Modifying Pixels (part 4 – Chromakey)
Topics
n To replace the background in a picture: chromakey.
6.1 Conditional Pixel Changes
Type in the following code in the Program window (changing the comments):
import java.awt.Color;
/* Program that transforms pictures using loops and conditionals
* by doing chromakey
*
* Picture and Pixel classes are defined in bookClasses developed
* at Georgia Tech by Mark Guzdial / Barbara Ericson
*
* @author Wayne Summers
* @date Oct. 3, 2007
*/
public class TransformPictures3
{
public static void main(String[] args)
{
String fileName1, fileName2;
System.out.println(“Find the source file (green screen image)”);
fileName1 = FileChooser.pickAFile(); // note that this pops up a window to select a jpg file
Picture sourcePicture;
sourcePicture = new Picture(fileName1);
System.out.println(“Find the destination file (new background image)”);
fileName2 = FileChooser.pickAFile(); // note that this pops up a window to select a jpg file
Picture backgroundPicture;
backgroundPicture = new Picture(fileName2);
// calls to methods that manipulate images go here
sourcePicture.show();
}
}
6.7 Chromakey
i) Type in the following code in the Program window below the main method and before the last }:
/**
* Method to do chromakey by replacing the pixel colors of the green screen picture
* with the new background
* @param pict - picture Object to be modified
* @param newBg - the new background to use as a replacemnt for the green background
*/
public static void chromakeyGreen(Picture sourcePic, Picture newBg)
{
Pixel[] pixelArray = sourcePic.getPixels();
Pixel newPixel = null;
int width = sourcePic.getWidth();
int height = sourcePic.getHeight();
// loop through the pixels
for (Pixel currPixel : pixelArray)
{
if (currPixel.getRed() + currPixel.getBlue() < currPixel.getGreen())
{
newPixel = newBg.getPixel(currPixel.getX(), currPixel.getY());
currPixel.setColor(newPixel.getColor());
} //endif
} //end for-each
}
ii) Type in the following code in the Program window in the main method and before the pic.show();
chromakeyGreen(sourcePicture, backgroundPicture)
iii) Test your program with different pictures and backgrounds.
iv) You may have noticed some residual splotches of green on your chromakeyed picture. If you use explorer(), you will see that there are lots of RGB values on the green background and that sometimes R+B is not greater than G even though the background appears green. Try multiplying both R and B with different percentages to get a “cleaner” picture.