- Back to Home »
- Histogram deviation in java , image comparison in java »
- Histogram deviation in java
Posted by : Mukunthan GJ
Thursday, 22 August 2013
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class own {
private static BufferedImage image;
private static final int IMG_WIDTH = 100;
private static final int IMG_HEIGHT = 100;
private static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
public static void main(String[] args) throws IOException
{
Scanner s = new Scanner(new File("C:\\Users\\manoj\\Desktop\\1.txt"));
Scanner s1 = new Scanner(new File("C:\\Users\\manoj\\Desktop\\2.txt"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();
ArrayList<String> list1 = new ArrayList<String>();
while (s1.hasNext()){
list1.add(s1.next());
}
s.close();
int n=list1.size();
for (int j=0;j<n;j++)
{
String fu=list1.get(j);
String su=list.get(j);
URL url = new URL(fu);
image = ImageIO.read(url);
ImageIO.write(image, "jpg",new File("C:\\Users\\manoj\\Desktop\\1.jpg"));
URL url1 = new URL(su);
image = ImageIO.read(url1);
ImageIO.write(image, "jpg",new File("C:\\Users\\manoj\\Desktop\\2.jpg"));
for(int i=1;i<=2;i++)
{
if(i==1)
{
BufferedImage originalImage = ImageIO.read(new File("C:\\Users\\manoj\\Desktop\\1.jpg"));
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
ImageIO.write(resizeImageJpg, "jpg", new File("C:\\Users\\manoj\\Desktop\\4.jpg"));
}
if(i==2)
{
BufferedImage originalImage = ImageIO.read(new File("C:\\Users\\manoj\\Desktop\\2.jpg"));
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
ImageIO.write(resizeImageJpg, "jpg", new File("C:\\Users\\manoj\\Desktop\\5.jpg"));
}
}
BufferedImage biA= ImageIO.read(new File("C:\\Users\\manoj\\Desktop\\4.jpg"));
DataBuffer dbA = biA.getData().getDataBuffer();
int sizeA = dbA.getSize();
BufferedImage biB= ImageIO.read(new File("C:\\Users\\manoj\\Desktop\\5.jpg"));
DataBuffer dbB = biB.getData().getDataBuffer();
int sizeB = dbB.getSize();
int[] a =new int[sizeA];
int[] b=new int[sizeB];
int a1=0, b1=0;
for(int i=0; i<sizeA; i++) {
a[i]=dbA.getElem(i);
}
for(int i=0; i<sizeB; i++) {
b[i]=dbB.getElem(i);
}
for(int i=0; i<sizeA; i++)
{
if(a[i]==255)
a1=0+a1;
else
a1=a[i]+a1;
}
for(int i=0; i<sizeB; i++)
{
if(b[i]==255)
b1=0+b1;
else
b1=b[i]+b1;
}
int o1= (a1/sizeA);
int o2=(b1/sizeB);
int dev=(o1-o2);
System.out.println("Histogram deviation is "+(dev));
n++;
}
}
}