Welcome to Dream.In.Code
Become a Java Expert!

Join 150,414 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,016 people online right now. Registration is fast and FREE... Join Now!




Chromakey Help

 
Reply to this topicStart new topic

Chromakey Help

Lambeau86
18 Mar, 2008 - 04:21 AM
Post #1

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 21



Thanked: 1 times
My Contributions
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);
      }
   }
}





User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Chromakey Help
18 Mar, 2008 - 06:46 PM
Post #2

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Could you attach sample image files?
User is offlineProfile CardPM
+Quote Post

Lambeau86
RE: Chromakey Help
18 Mar, 2008 - 07:56 PM
Post #3

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 21



Thanked: 1 times
My Contributions
yeah sure
I've made a few changes, that I hope are in the correct direction so I'll embed those too

CODE
for( int i = 0, j = p1.getHeight(); i < p1.getWidth()-1; i++){
            for( int k = 0, l= p2.getHeight(); k < p2.getWidth()-1; k++){


                int g1 = p1.getGreen(i, j);
                int b1 = p1.getBlue(i, j);
                int r1 = p1.getRed(i, j);

                int g2 = p2.getGreen(k, l);
                int b2 = p2.getBlue(k, l);
                int r2 = p2.getRed(k, l);

                if (g1 > 180 && g1 < 190){
                    p1.setGreen(i, j, g2);
                    p1.setBlue(i, j, b2);
                    p1.setRed(i, j, r2);
                    
                }

                else{
                    p1.setBlue(i, j, b1);
                    p1.setGreen(i, j, g2);
                    p1.setRed(i,j, r1);
                
                }}}}


it get a domain error, i'm not sure why either

picture isn't me is stock, so is the background


Attached thumbnail(s)
Attached Image Attached Image
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month