skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, October 22, 2012

Finding the difference between two images using their samples

Posted by Raju Gupta at 12:00 PM – 0 comments
 
Helps in finding the difference between two images by obtaining their samples and comparing them. The number of bands assosiated with each image is also compared, along with their width and height.

File file1 = new File("C:\Documents and Settings\515956\Desktop\Raj_image.jpg"); 
File file2 = new File("C:\Documents and Settings\515956\Desktop\Local_image.jpg");  
BufferedImage image1 = ImageIO.read(file1); 
BufferedImage image2 = ImageIO.read(file2);

Raster r1 = image1.getData();
Raster r2 = image2.getData(); 
boolean ret; // Stores result. 
// Check image sizes and number of bands. If there are different
// then no need to compare images as they are definitely not equal. 

System.out.println("image1 band"+r1.getNumBands()+"  image2 band"+r2.getNumBands()+"  image1 width"+r1.getWidth()+"  image2 width"+ r2.getWidth()+"  image1 height"+ r1.getHeight()+"  image2 height"+r2.getHeight()); 

if (r1.getNumBands() != r2.getNumBands() ||   r1.getWidth() != r2.getWidth() ||   r1.getHeight() != r2.getHeight()) {  
 ret = false;
 System.out.println("Primary comparison failure");

} 

else {  
 // #Bands and image bounds match so compare each sample in turn. 
 ret = true;   
 search:   
  for (int i=0; i<r1.getNumBands(); ++i) { 
   for (int x=0; x<r1.getWidth(); ++x) {    
    for (int y=0; y<r1.getHeight(); ++y) {  


     if (r1.getSample(x, y, i) != r2.getSample(x, y, i)) {     
      // At least one sample differs so result is false; 
      ret = false; 
      System.out.println("sample difference");
      // Use labeled break to terminate all loops.
      break
      search;   
     }   
    }    
   }   
  }   

}
System.out.println(ret);

Labels: Java Image Examle

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger