-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ImageComparisonTools.java
140 lines (130 loc) · 5.02 KB
/
ImageComparisonTools.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package ua.comparison.image;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
/**
* Tools for the {@link ImageComparison} object.
*/
public class ImageComparisonTools {
/**
* Create GUI for represents the resulting image.
*
* @param image resulting image.
*/
public static Frame createGUI(BufferedImage image) {
JFrame frame = new JFrame("The result of the comparison");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel();
label.setIcon(new ImageIcon(image, "Result"));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(image.getWidth(), (int) (image.getHeight())));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
return frame;
}
/**
* Make a copy of the {@link BufferedImage} object.
*
* @param image the provided image.
* @return copy of the provided image.
*/
static BufferedImage deepCopy(BufferedImage image) {
ColorModel cm = image.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = image.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
/**
* Check images for equals their widths and heights.
*
* @param image1 {@link BufferedImage} object of the first image.
* @param image2 {@link BufferedImage} object of the second image.
*/
public static void checkCorrectImageSize(BufferedImage image1, BufferedImage image2) {
if (image1.getHeight() != image2.getHeight() || image1.getWidth() != image2.getWidth()) {
throw new IllegalArgumentException("Images dimensions mismatch");
}
}
/**
* Say if the two pixels equal or not. The rule is the difference between two pixels
* need to be more then 10%.
*
* @param rgb1 the RGB value of the Pixel of the Image1.
* @param rgb2 the RGB value of the Pixel of the Image2.
* @return {@code true} if they' are difference, {@code false} otherwise.
*/
public static boolean isDifferentPixels(int rgb1, int rgb2) {
int red1 = (rgb1 >> 16) & 0xff;
int green1 = (rgb1 >> 8) & 0xff;
int blue1 = (rgb1) & 0xff;
int red2 = (rgb2 >> 16) & 0xff;
int green2 = (rgb2 >> 8) & 0xff;
int blue2 = (rgb2) & 0xff;
double result = Math.sqrt(Math.pow(red2 - red1, 2) +
Math.pow(green2 - green1, 2) +
Math.pow(blue2 - blue1, 2))
/
Math.sqrt(Math.pow(255, 2) * 3);
return result > 0.1;
}
/**
* Populate binary matrix by "0" and "1". If the pixels are difference set it as "1", otherwise "0".
*
* @param image1 {@link BufferedImage} object of the first image.
* @param image2 {@link BufferedImage} object of the second image.
* @return populated binary matrix.
*/
static int[][] populateTheMatrixOfTheDifferences(BufferedImage image1, BufferedImage image2) {
int[][] matrix = new int[image1.getWidth()][image1.getHeight()];
for (int y = 0; y < image1.getHeight(); y++) {
for (int x = 0; x < image1.getWidth(); x++) {
matrix[x][y] = isDifferentPixels(image1.getRGB(x, y), image2.getRGB(x, y)) ? 1 : 0;
}
}
return matrix;
}
/**
* Read image from the provided path.
*
* @param path the path where contains image.
* @return the {@link BufferedImage} object of this specific image.
*/
public static BufferedImage readImageFromResources(String path) throws IOException, URISyntaxException {
return ImageIO.read(new File(ImageComparison.class.getClassLoader().getResource(path).toURI().getPath()));
}
/**
* Read image from the provided file path.
*
* @param path the path where contains image.
* @return the {@link BufferedImage} object of this specific image.
*/
public static BufferedImage readImageFromFile(File path) throws IOException {
return ImageIO.read(path);
}
/**
* Save image to the provided path.
*
* @param path the path to the saving image.
* @param image the {@link BufferedImage} object of this specific image.
*/
public static void saveImage(File path, BufferedImage image) throws IOException {
File dir = path.getParentFile();
boolean dirExists = dir == null || dir.isDirectory() || dir.mkdirs();
if (!dirExists) {
throw new RuntimeException("Unable to create directory " + dir);
}
ImageIO.write(image, "png", path);
}
}