Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. //Image percentage match
  2. public float getPercentMatch(String imageFile1, String imageFile2) {
  3.  
  4. //Set how close a pixel has to be for a match, pixel deviation
  5. int pd = 3;
  6.  
  7. try {
  8. //get height, width and image from first file
  9. File input1 = new File(imageFile1);
  10. image1 = ImageIO.read(input1);
  11. width1 = image1.getWidth();
  12. height1 = image1.getHeight();
  13.  
  14. //Do the same for the second one
  15. File input2 = new File(imageFile2);
  16. image2 = ImageIO.read(input2);
  17. width2 = image2.getWidth();
  18. height2 = image2.getHeight();
  19.  
  20. int matchCount = 0;
  21.  
  22. //Make sure they're the same height. You could also choose to use the
  23. //smaller picture for dimensions
  24. if (height1 != height2 || width1 != width2) {return 0.0;}
  25.  
  26.  
  27. //Iterate over each pixel
  28. for(int i=0; i<height1; i++){
  29.  
  30. for(int j=0; j<width1; j++){
  31.  
  32. //Get RGB values for each image at certain pixel
  33. Color c1 = new Color(image1.getRGB(j, i));
  34. Color c2 = new Color(image2.getRGB(j, i));
  35.  
  36. // If the colors are close enough based on deviation value...
  37. if ( (Math.abs(c1.getRed() - c2.getRed()) < pd ) &&
  38. (Math.abs(c1.getGreen() - c2.getGreen()) < pd ) &&
  39. (Math.abs(c1.getBlue() - c2.getBlue() ) < pd ) ) {
  40. //...it's a match, so increment the match count
  41. matchCount++;
  42. }
  43. }
  44. }
  45. return matchCount / (height1 * width1);
  46.  
  47. } catch (Exception e) {}
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement