Advertisement
Cinster

Untitled

Nov 14th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. import java.awt.List;
  2. import java.awt.image.BufferedImage;
  3. import java.awt.image.DataBuffer;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11. import java.util.stream.Stream;
  12. import javax.imageio.ImageIO;
  13.  
  14. public class ImageComparisson {
  15.  
  16. public static int number;
  17.  
  18. public static ArrayList<String> list = new ArrayList<String>();
  19. public static ArrayList<Double> PercSim = new ArrayList<Double>();
  20. public static void print ()
  21. {
  22.  
  23. String[] S = new String[number];
  24. Double[] Perc = new Double[number];
  25. for (int i = 0 ; i < number ; i++ ) {
  26.  
  27. list.toArray(S);
  28. PercSim.toArray(Perc);
  29. System.out.println((i+1) +". "+S[i] +" at "+ Perc[i]+ "% ");
  30. }
  31. System.out.println();
  32. }
  33.  
  34. public static void max ()
  35. {
  36. String[] S = new String[number];
  37. Double[] Perc = new Double[number];
  38. double max = 0;
  39. int index = 0;
  40.  
  41. for (int i = 0 ; i < number ; i++ ) {
  42.  
  43. PercSim.toArray(Perc);
  44. list.toArray(S);
  45. if (max < Perc[i] && Perc[i] != 100) {
  46. max = Perc[i];
  47. index = i;
  48. }
  49.  
  50. }
  51. System.out.println("The file with maximum similarity is: "+ S[index]+" at "+ Perc[index]+ "%" );
  52. System.out.println();
  53. }
  54.  
  55. public static void min ()
  56. {
  57. String[] S = new String[number];
  58. Double[] Perc = new Double[number];
  59. double min = 100;
  60. int index = 0;
  61.  
  62. for (int i = 0 ; i < number ; i++ ) {
  63.  
  64. PercSim.toArray(Perc);
  65. list.toArray(S);
  66. if (min > Perc[i] && Perc[i] != 100) {
  67. min = Perc[i];
  68. index = i;
  69. }
  70.  
  71. }
  72. System.out.println("The file with minimum similarity is: "+ S[index]+" at "+ Perc[index]+ "%" );
  73. System.out.println();
  74. }
  75.  
  76.  
  77. public static double compareImage(File fileA, File fileB, int percent) {
  78.  
  79. double percentage = 0.0;
  80. try {
  81. // take buffer data from both image files //
  82. BufferedImage biA = ImageIO.read(fileA);
  83. DataBuffer dbA = biA.getData().getDataBuffer();
  84. double sizeA = dbA.getSize();
  85.  
  86. BufferedImage biB = ImageIO.read(fileB);
  87. DataBuffer dbB = biB.getData().getDataBuffer();
  88. double sizeB = dbB.getSize();
  89.  
  90. double count = 0;
  91.  
  92. // compare data-buffer objects
  93. if (sizeA == sizeB) {
  94.  
  95. for (int i = 0; i < sizeA; i++) {
  96. //check the first 100 pixels and if at least half the inputed percent of pixels are not the same
  97. //move to the next file
  98. if ( i == (sizeA / 100) && count < ( (sizeA * percent) / (2 * 100 * 100 ) ) )
  99. {
  100. return 0;
  101. }
  102. //check if pixel values are close enough in colour (acceptable range of 20)
  103. if (Math.abs( ( dbA.getElem(i) - dbB.getElem(i))) <= 20 ) {
  104. count = count + 1;
  105. }
  106. }
  107.  
  108. //calculate the percentage based on the similar pixels
  109. percentage = (count * 100) / sizeA;
  110.  
  111. }
  112. else {
  113.  
  114. }
  115.  
  116. }
  117. catch (Exception e) {
  118. System.out.println("Failed to compare image files ... " ) ;
  119. System.out.println(fileB.getPath());
  120. e.printStackTrace();
  121. }
  122. if (percentage >= percent)
  123. {
  124.  
  125. System.out.println("Image file "+ fileB.getPath()+" is similar to reference file" );
  126. System.out.println("These images are "+ percentage + "% similar" );
  127. System.out.println();
  128. list.add(fileB.getPath());
  129. PercSim.add(percentage);
  130. number++;
  131. }
  132. return 0;
  133. }
  134.  
  135.  
  136.  
  137.  
  138. public static void main(String[] args) throws IOException {
  139.  
  140. Scanner sc = new Scanner (System.in);
  141. Scanner sc2 = new Scanner (System.in);
  142. Scanner sc3 = new Scanner (System.in);
  143. System.out.println();
  144.  
  145. File fileA = new File("Project_1_datasets\\2\\40694.jpg");
  146. System.out.println("The reference file is: "+ fileA.getPath());
  147. System.out.println();
  148.  
  149. System.out.print("Enter percent of similarity to search for (0 to 100, recomended 75+ ) : ");
  150. int percent = sc.nextInt();
  151.  
  152. while (percent < 0 || percent > 100)
  153. {
  154. System.out.println("Percentage entered not in range, choose a number between 0 to 100");
  155. percent = sc.nextInt();
  156. }
  157. System.out.println();
  158.  
  159. int percents = percent;
  160.  
  161. System.out.print("Searching for files "+ percents+"% similar to the file "+fileA.getPath());
  162. System.out.print(" , start search? (Y/N) : ");
  163. String S = sc2.nextLine();
  164.  
  165. while (!S.equals("Y") && !S.equals("y")) {
  166. System.out.println("Please enter Y");
  167. S = sc2.nextLine();
  168. }
  169.  
  170. System.out.println();
  171. System.out.println("Starting the search ...");
  172. System.out.println();
  173.  
  174. try (Stream<Path> filePathStream=Files.walk(Paths.get("Project_1_datasets\\"))) {
  175. filePathStream.forEach(filePath -> {
  176. if (Files.isRegularFile(filePath)) {
  177. File fileB = new File(filePath.toString());
  178. compareImage(fileA ,fileB, percents);
  179. }
  180. });
  181.  
  182. System.out.println("Done searching");
  183. System.out.println("Number of matches : "+ number);
  184. System.out.println();
  185.  
  186. if(number == 0) {
  187. System.out.println("No matches closing the program ...");
  188. sc.close();
  189. sc2.close();
  190. sc3.close();
  191. }
  192.  
  193. else {
  194.  
  195. System.out.print("Print out the list of the " +number+" matching file(s) (Y/N) : ");
  196. String D = sc3.nextLine();
  197. while (!D.equals("Y") && !D.equals("N") && !D.equals("n") && !D.equals("y")) {
  198. System.out.println("Please enter Y or N: ");
  199. D = sc3.nextLine();
  200. }
  201. System.out.println();
  202. if (D.equals("Y") || D.equals("y")){
  203. print();
  204. max();
  205. min();
  206. }
  207.  
  208. if (D.equals("N") || D.equals("n")) {
  209. }
  210. System.out.println("Closing the program ...");
  211. sc.close();
  212. sc2.close();
  213. sc3.close();
  214. }
  215. }
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement