Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Image comparison in java

999429Mar 27 2013 — edited Mar 27 2013
Hi,
Currently I need a piece of code to compare 2 images and return a % match. I have written a sample and was wondering is its ok or there are better ways to do. Please let me know of any jars for this that can be used in java. I tried googling it but did not find anything straightforward. Iam new to this area so any help is highly appreciated.

The code :

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;

public class ImageUtil {
public static void main(String[] args) {
boolean result = false;
try {
result = compareImage(
new URL(
"http://images3.wikia.nocookie.net/__cb20120817021531/angrybirds/images/5/51/7632301_2.png"),
new URL(
"http://fc02.deviantart.net/fs70/f/2012/269/d/8/angry_birds___red___super_high_quality__by_tomefc98-d5fz9by.png"));
System.out.println(result);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static boolean compareImage(URL img1, URL img2) {
boolean ret = false; // Stores result.
int passed=0,failed=0,percentage=0;
try {


BufferedImage image1 = ImageIO.read(img1);

BufferedImage image2 = ImageIO.read(img2);

image1=Thumbnails.of(image1).forceSize(image2.getWidth(), image2.getHeight()).asBufferedImage();

Raster r1 = image1.getData();
Raster r2 = image2.getData();



// 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");
// image2.setData(r1);

}

else {
// #Bands and image bounds match so compare each sample in turn.
ret = true;
int numBands=0;
if( r1.getNumBands()>r2.getNumBands()){
numBands=r2.getNumBands();
}
else{
numBands=r1.getNumBands();
}
search: for (int i = 0; i < numBands; ++i) {
for (int x = 0; x < r2.getWidth(); ++x) {
for (int y = 0; y < r2.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;
failed++;
}
else{
passed++;
}
}
}
}

}
percentage= (passed* 100)/(passed+failed);
System.out.println(percentage);
if(percentage>50){
ret=true;
}
else{
ret=false;
}
return ret;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 24 2013
Added on Mar 27 2013
2 comments
1,592 views