Thursday, 15 March 2012

bytearray - calculating euclidean distance of 2 byte arrays java -


I have 2 main images and I want to compare them to the set of other images. I am calculating the byte array of those two images and then the image being compared with the image being compared to the 2 is being processed. It has 2 main images:

  file is upper = New file ("sweater.jpg"); Byte [] upper page = files Readelbates (upper.pet ()); File less = new file ("pants.jpg"); Byte [] Bottom Page = Files Readablebytes (low. Path ());  

Then in the loop where I treat all the images from a directory, I do the following:

  byte [] currentImage = Files.readAllBytes ( F. ToPath ()); Float interval = 0; Float difference lower = 0; For (Int i = 0; I & lt; current image. Lamp; i ++) {focus on + = (upper part [i] - current image [i]) ^ 2; Difference lower = = (bottom [i] - current image [i]) ^ 2; } Float Eclidian Distances = (Float) Math SQL (difference above); Float euclideanstance lower = (float) monastery. SCTR (interloor); If (Euclidean Dastensupter & lt; euclideanDistanceLower) {filename = filename + "Upper"; } Else {filename = filename + "lower"; }  

However, calculation of Euclidean distance with upper image works perfectly, does not work in comparison to low events. The following error appears:

  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55881 at org.apache.commons.io.ShapeRecognition.main (ShapeRecognition.java:54)  

Image sizes are all the same, so I really can not understand why it works for upper and lower signs? Thanks!

Update so basically the problem is that generated byte arrays are not the same size, even if the images are the same size.

Images may have the same size, about resolution (as they Both can be 800x600 pixels). But you are not comparing pictures , but files and ... do not understand images like this, whatever. Two JPG files of 800x600-size images can be 10 KB or 50 KB. It depends on image content and compression level.

What you want to do (most likely ...) is to compare pixels of images, therefore, to get the actual pixel data, you get the ImageIO , and decoded JPG files

you

  BufferedImage upper = ImageIO.read (new file ("upper" .JPG ");); Buffer image less = ImageIO.read (new file ("lower.jpg"));  

Then, to compare images, you have to calculate the differences in pixels. It is not trivial that there is no general, globally applicable "distance measure" for pixels. The approach can be done to iterate over pixels of both images, and can calculate something like

  int argb0 = image0.getRGB (x, y); Int argb1 = image1.getRGB (x, y); Int a0 = (argb0> & gt; 24) & amp; 0xFF; Int R = 0 (RGB 0>> 16) & amp; 0xFF; Int g0 = (argb0> 8) & amp; 0xFF; Int b0 = (argb0) & amp; 0xFF; Int a1 = (argb1> 24) & amp; 0xFF; Int R1 = (RGB 1>> 16) & amp; 0xFF; Int g1 = (argb1> 8) & amp; 0xFF; Int b1 = (argb1) & amp; 0xFF; Int aDiff = Math. Box (a1 - a0); Int Rdf = Math. Abs (R1 - R); Int gDiff = Math.base (G1 - G0); Int bDiff = Math.abs (b1 - b0);  

To get the differences in the red, green, blue and alpha channels, and to get these values ​​together, to get value which you later calculate the square root.


On one side: you calculated

  on the difference + = (upper part [i] - the current image [i]) ^ 2;  

But note that ^ 2 does no calculate the power, as you can expect, you can either < Code> Math.pow (difference, 2) , but something like preference

  differenceSum + = difference * difference;  

No comments:

Post a Comment