I have 2 main images and I want to compare a set of other images to them. What I do is computing the byte array of those two images and then compare those 2 with the current image being processed. This are the 2 main images:
File upper = new File("sweater.jpg");
byte [] upperPart = Files.readAllBytes(upper.toPath());
File lower = new File("pants.jpg");
byte [] lowerPart = Files.readAllBytes(lower.toPath());
Then in the loop where I treat all the images from a directory I do the following:
byte [] currentImage = Files.readAllBytes(f.toPath());
float differenceUpper = 0;
float differenceLower = 0;
for (int i=0;i< currentImage.length;i++) {
differenceUpper += (upperPart[i] - currentImage[i])^2;
differenceLower += (lowerPart[i] - currentImage[i])^2;
}
float euclideanDistanceUpper = (float) Math.sqrt(differenceUpper);
float euclideanDistanceLower = (float) Math.sqrt(differenceLower);
if (euclideanDistanceUpper < euclideanDistanceLower){
filename = filename + "Upper";
} else {
filename = filename + "Lower";
}
However, computing the euclidean distance with the UpperImage works perfectly, comparing to the lowerImage does not work. The following error appears:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55881
at org.apache.commons.io.ShapeRecognition.main(ShapeRecognition.java:54)
The image sizes are all the same, so I really dont understand why it works for upper and not for lower. Any hint? Thanks!
UPDATE so basically the problem is that the Byte Arrays generated are not the same size even if the images sizes are the same.