Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.List;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBuffer;
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.stream.Stream;
- import javax.imageio.ImageIO;
- public class ImageComparisson {
- public static int number;
- public static ArrayList<String> list = new ArrayList<String>();
- public static ArrayList<Double> PercSim = new ArrayList<Double>();
- public static void print ()
- {
- String[] S = new String[number];
- Double[] Perc = new Double[number];
- for (int i = 0 ; i < number ; i++ ) {
- list.toArray(S);
- PercSim.toArray(Perc);
- System.out.println((i+1) +". "+S[i] +" at "+ Perc[i]+ "% ");
- }
- System.out.println();
- }
- public static void max ()
- {
- String[] S = new String[number];
- Double[] Perc = new Double[number];
- double max = 0;
- int index = 0;
- for (int i = 0 ; i < number ; i++ ) {
- PercSim.toArray(Perc);
- list.toArray(S);
- if (max < Perc[i] && Perc[i] != 100) {
- max = Perc[i];
- index = i;
- }
- }
- System.out.println("The file with maximum similarity is: "+ S[index]+" at "+ Perc[index]+ "%" );
- System.out.println();
- }
- public static void min ()
- {
- String[] S = new String[number];
- Double[] Perc = new Double[number];
- double min = 100;
- int index = 0;
- for (int i = 0 ; i < number ; i++ ) {
- PercSim.toArray(Perc);
- list.toArray(S);
- if (min > Perc[i] && Perc[i] != 100) {
- min = Perc[i];
- index = i;
- }
- }
- System.out.println("The file with minimum similarity is: "+ S[index]+" at "+ Perc[index]+ "%" );
- System.out.println();
- }
- public static double compareImage(File fileA, File fileB, int percent) {
- double percentage = 0.0;
- try {
- // take buffer data from both image files //
- BufferedImage biA = ImageIO.read(fileA);
- DataBuffer dbA = biA.getData().getDataBuffer();
- double sizeA = dbA.getSize();
- BufferedImage biB = ImageIO.read(fileB);
- DataBuffer dbB = biB.getData().getDataBuffer();
- double sizeB = dbB.getSize();
- double count = 0;
- // compare data-buffer objects
- if (sizeA == sizeB) {
- for (int i = 0; i < sizeA; i++) {
- //check the first 100 pixels and if at least half the inputed percent of pixels are not the same
- //move to the next file
- if ( i == (sizeA / 100) && count < ( (sizeA * percent) / (2 * 100 * 100 ) ) )
- {
- return 0;
- }
- //check if pixel values are close enough in colour (acceptable range of 20)
- if (Math.abs( ( dbA.getElem(i) - dbB.getElem(i))) <= 20 ) {
- count = count + 1;
- }
- }
- //calculate the percentage based on the similar pixels
- percentage = (count * 100) / sizeA;
- }
- else {
- }
- }
- catch (Exception e) {
- System.out.println("Failed to compare image files ... " ) ;
- System.out.println(fileB.getPath());
- e.printStackTrace();
- }
- if (percentage >= percent)
- {
- System.out.println("Image file "+ fileB.getPath()+" is similar to reference file" );
- System.out.println("These images are "+ percentage + "% similar" );
- System.out.println();
- list.add(fileB.getPath());
- PercSim.add(percentage);
- number++;
- }
- return 0;
- }
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner (System.in);
- Scanner sc2 = new Scanner (System.in);
- Scanner sc3 = new Scanner (System.in);
- System.out.println();
- File fileA = new File("Project_1_datasets\\2\\40694.jpg");
- System.out.println("The reference file is: "+ fileA.getPath());
- System.out.println();
- System.out.print("Enter percent of similarity to search for (0 to 100, recomended 75+ ) : ");
- int percent = sc.nextInt();
- while (percent < 0 || percent > 100)
- {
- System.out.println("Percentage entered not in range, choose a number between 0 to 100");
- percent = sc.nextInt();
- }
- System.out.println();
- int percents = percent;
- System.out.print("Searching for files "+ percents+"% similar to the file "+fileA.getPath());
- System.out.print(" , start search? (Y/N) : ");
- String S = sc2.nextLine();
- while (!S.equals("Y") && !S.equals("y")) {
- System.out.println("Please enter Y");
- S = sc2.nextLine();
- }
- System.out.println();
- System.out.println("Starting the search ...");
- System.out.println();
- try (Stream<Path> filePathStream=Files.walk(Paths.get("Project_1_datasets\\"))) {
- filePathStream.forEach(filePath -> {
- if (Files.isRegularFile(filePath)) {
- File fileB = new File(filePath.toString());
- compareImage(fileA ,fileB, percents);
- }
- });
- System.out.println("Done searching");
- System.out.println("Number of matches : "+ number);
- System.out.println();
- if(number == 0) {
- System.out.println("No matches closing the program ...");
- sc.close();
- sc2.close();
- sc3.close();
- }
- else {
- System.out.print("Print out the list of the " +number+" matching file(s) (Y/N) : ");
- String D = sc3.nextLine();
- while (!D.equals("Y") && !D.equals("N") && !D.equals("n") && !D.equals("y")) {
- System.out.println("Please enter Y or N: ");
- D = sc3.nextLine();
- }
- System.out.println();
- if (D.equals("Y") || D.equals("y")){
- print();
- max();
- min();
- }
- if (D.equals("N") || D.equals("n")) {
- }
- System.out.println("Closing the program ...");
- sc.close();
- sc2.close();
- sc3.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement