How to Read/Write image files? compute differences in 2 pictures...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccarter45
    New Member
    • Mar 2008
    • 10

    How to Read/Write image files? compute differences in 2 pictures...

    I need to compute the differences in color between 2 pictures. Also, I need to write a method that computes a useful difference color of two colors (where the colors and their difference will be represented as single int's). The formula you should use for colordiff, the difference color between color1 and color2, with color1 = (R1, G1, B1) and color2 = (R2, G2, B2) is as follows:
    colordiff = (Rdiff, Gdiff, Bdiff) where
    Rdiff = |R1-R2| Gdiff = |G1-G2| Bdiff = |B1-B2|
    here's what I have:
    Code:
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.*;
    
    public class ImageDiff 
    {
    
      public static void main (String args[]) throws IOException 
      {
        int startX, startY, endX, endY, color1, color2;
    
        File imageFile1 = new File("pisa1.jpg");
        BufferedImage image1 = ImageIO.read(imageFile1);
        File imageFile2 = new File("pisa2.jpg");
        BufferedImage image2 = ImageIO.read(imageFile2);
    
        startX = image1.getMinX();
        startY = image1.getMinY();
        endX = startX + image1.getWidth();
        endY = startY + image1.getHeight();
    
        for (int i=startX; i<endX; i++) {
          for (int j=startY; j<endY; j++) {
            color1 = image1.getRGB(i,j);
            color2 = image2.getRGB(i,j);
            image1.setRGB(i,j,color1-color2);
          }
        }
    
        File outImage = new File("pisadiff.jpg");
        ImageIO.write(image1, "jpg", outImage);
      }
      
      // Converting R-G-B values into a single integer
      public static int RGBint (int R, int G, int B) 
      {
        return (R*256*256+G*256+B);
      }
    
      // grab the R value from a single integer
      public static int getRFromRGB (int color) 
      {
        return ((color & 0x00FF0000) >> 16);
      }
      
      // grab the G value from a single integer
      public static int getGFromRGB (int color) 
      {
        return ((color & 0x0000FF00) >> 8);
      }
    
      // grab the B value from a single integer
      public static int getBFromRGB (int color) 
      {
        return (color & 0x000000FF);
      }
    }
Working...