I'm trying to chroma key a picture, so that the green screen in the back of my picture is replaced by the second picture i choose. But when I run my code all i get is a slightly altered green screen. Heres my code along with the picture file used along with it. Any suggestions would be very helpful.
CODE
public class DIP {
public static void chromakey(Picture p1, Picture p2){
for( int i = 0; i < p1.getWidth(); i++){
for (int j = 0; j< p1.getHeight()-1; j++){
for( int k = 0; k < p2.getWidth(); k++){
for (int l = 0; l< p2.getHeight()-1; l++){
int g1 = p1.getGreen(i, j);
if (g1 >70 && g1 < 75)
i=k;
j=l;
}}}}}
public static void main(String[] args) {
// Choose a file from the filesystem
ImageFileChooser ifc = new ImageFileChooser();
String filename = ifc.getFile();
// Create a Picture object
Picture original = new Picture(filename, "Original Image");
Picture chrom = new Picture(filename, "Chromakey");
// Show the Picture
original.displayImage();
// Convert Picture to chromakey
System.out.println("Converting to chromakey.");
String filename2 = ifc.getFile();
Picture background = new Picture(filename2, "");
chromakey(chrom,background);
chrom.displayImage();
}}
CODE
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class Picture
{
private File file;
private BufferedImage bufferedImage;
private String title;
/**
Builds a PixelImage object based off of the file name
given as a parameter.
@param fileName The image file to open and read.
*/
public Picture(String fileName, String title)
{
try
{
file = new File(fileName);
bufferedImage = ImageIO.read(file);
this.title = title;
}
catch (IOException ex)
{
System.out.println("Error reading the file " + fileName + ".");
System.exit(1);
}
}
public Picture(int width, int height)
{
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
setPixel(x,y,0);
}
/**
Returns the width of the image.
@return Returns the width of the image.
*/
public int getWidth()
{
return bufferedImage.getWidth();
}
/**
Returns the height of the image.
@return Returns the height of the image.
*/
public int getHeight()
{
return bufferedImage.getHeight();
}
/**
Returns the Alpha-Red-Green-Blue (ARGB) value for
the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@return Returns the ARGB value for the pixel at (x,y).
*/
public int getPixel(int x, int y)
{
return bufferedImage.getRGB(x,y);
}
/**
Returns the Alpha value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@return Returns the alpha value for the pixel at (x,y).
*/
public int getAlpha(int x, int y)
{
int rgb = bufferedImage.getRGB(x,y);
return (rgb >> 24) & 0xFF;
}
/**
Returns the Red value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@return Returns the red value for the pixel at (x,y).
*/
public int getRed(int x, int y)
{
int rgb = bufferedImage.getRGB(x,y);
return (rgb >> 16) & 0xFF;
}
/**
Returns the Green value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@return Returns the green value for the pixel at (x,y).
*/
public int getGreen(int x, int y)
{
int rgb = bufferedImage.getRGB(x,y);
return (rgb >> 8) & 0xFF;
}
/**
Returns the Blue value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@return Returns the blue value for the pixel at (x,y).
*/
public int getBlue(int x, int y)
{
int rgb = bufferedImage.getRGB(x,y);
return rgb & 0xFF;
}
/**
Sets the Alpha-Red-Green-Blue value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@param value The ARGB value for the pixel at (x,y).
*/
public void setPixel(int x, int y, int value)
{
bufferedImage.setRGB(x,y,value);
}
/**
Sets the Alpha value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@param value The alpha value for the pixel at (x,y).
*/
public void setAlpha(int x, int y, int value)
{
int rgb = bufferedImage.getRGB(x,y);
rgb = (rgb & 0xFFFFFF) | (value << 24);
bufferedImage.setRGB(x,y,rgb);
}
/**
Sets the Red value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@param value The red value for the pixel at (x,y).
*/
public void setRed(int x, int y, int value)
{
int rgb = bufferedImage.getRGB(x,y);
rgb = (rgb & 0xFF00FFFF) | (value << 16);
bufferedImage.setRGB(x,y,rgb);
}
/**
Sets the Green value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@param value The green value for the pixel at (x,y).
*/
public void setGreen(int x, int y, int value)
{
int rgb = bufferedImage.getRGB(x,y);
rgb = (rgb & 0xFFFF00FF) | (value << 8);
bufferedImage.setRGB(x,y,rgb);
}
/**
Sets the Blue value for the pixel at (x,y).
@param x An x coordinate in the image.
@param y An y coordinate in the image.
@param value The blue value for the pixel at (x,y).
*/
public void setBlue(int x, int y, int value)
{
int rgb = bufferedImage.getRGB(x,y);
rgb = (rgb & 0xFFFFFF00) | value;
bufferedImage.setRGB(x,y,rgb);
}
/**
Displays the image in a separate window.
*/
public void displayImage()
{
JFrame frame = new JFrame();
ImageIcon imageIcon = new ImageIcon();
JLabel label = new JLabel(imageIcon);
imageIcon.setImage(bufferedImage);
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.setTitle(this.title);
frame.setVisible(true);
}
public void saveImage(String fileName)
{
try
{
File file = new File(fileName);
ImageIO.write(bufferedImage,"png",file);
}
catch (Exception ex)
{
System.out.println("Save error");
System.exit(1);
}
}
}