forked from KilianB/JImageHash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainAlgorithms.java
More file actions
118 lines (94 loc) · 3.92 KB
/
ChainAlgorithms.java
File metadata and controls
118 lines (94 loc) · 3.92 KB
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
package com.github.kilianB.examples;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.github.kilianB.hashAlgorithms.AverageHash;
import com.github.kilianB.hashAlgorithms.DifferenceHash;
import com.github.kilianB.hashAlgorithms.DifferenceHash.Precision;
import com.github.kilianB.hashAlgorithms.HashingAlgorithm;
import com.github.kilianB.hashAlgorithms.PerceptiveHash;
import com.github.kilianB.hashAlgorithms.WaveletHash;
import com.github.kilianB.matcher.exotic.SingleImageMatcher;
/**
* To increase the quality of the returned results it can be useful to chain
* multiple algorithms back to back due to the different features each
* algorithms compares.
*
* @author Kilian
*
*/
public class ChainAlgorithms {
// Images used for testing
private HashMap<String, BufferedImage> images = new HashMap<>();
public void defaultMatcher() {
/*
* A single image matcher allows to compare two images against each other. The
* default matcher chains an average hash followed by a perceptive hash
*/
SingleImageMatcher matcher = new SingleImageMatcher();
//Add hashing algorithms as you please. Both hashes will be queried
matcher.addHashingAlgorithm(new AverageHash(64),.3);
matcher.addHashingAlgorithm(new WaveletHash(32,3),.3);
// Lets get two images
BufferedImage img1 = images.get("ballon");
BufferedImage img2 = images.get("lowQuality");
// Check if the images are similar
if (matcher.checkSimilarity(img1, img2)) {
System.out.println("Ballon & Low Quality are likely duplicates");
} else {
System.out.println("Ballon & Low Quality are distinct images");
}
}
/**
* Demonstrates how to fully configure a SingleImageMatcher. Choose own
* algorithms and thresholds
*
* @param image1 First image to be matched against 2nd image
* @param image2 Second image to be matched against the first image
*/
public void chainAlgorithms(BufferedImage image1, BufferedImage image2) {
/*
* Create multiple algorithms we want to test the images against
*/
HashingAlgorithm dHash = new DifferenceHash(32, Precision.Double);
// When shall an image be classified as a duplicate [0 - keyLenght]
// DHashes double precision doubles the key length supplied in the constructor
double dHashThreshold = .6;
HashingAlgorithm pHash = new PerceptiveHash(32);
// When shall an image be counted as a duplicate? [0-1]
double normalizedPHashThreshold = 0.6;
boolean normalized = true;
// This instance can be reused. No need to recreate it every time you want to
// match 2 images
SingleImageMatcher matcher = new SingleImageMatcher();
// Add algorithm to the matcher
// First dirty filter
matcher.addHashingAlgorithm(dHash, dHashThreshold);
// If successful apply second filer
matcher.addHashingAlgorithm(pHash, normalizedPHashThreshold, normalized);
System.out.println(matcher.checkSimilarity(image1, image2));
}
public static void main(String[] args) {
new ChainAlgorithms();
}
public ChainAlgorithms() {
loadImages();
System.out.println("defaultMatcher()");
defaultMatcher();
System.out.println("\nchainAlgorithms()");
chainAlgorithms(images.get("highQuality"), images.get("lowQuality"));
}
private void loadImages() {
// Load images
try {
images.put("ballon", ImageIO.read(getClass().getResourceAsStream("images/ballon.jpg")));
images.put("copyright", ImageIO.read(getClass().getResourceAsStream("images/copyright.jpg")));
images.put("highQuality", ImageIO.read(getClass().getResourceAsStream("images/highQuality.jpg")));
images.put("lowQuality", ImageIO.read(getClass().getResourceAsStream("images/lowQuality.jpg")));
images.put("thumbnail", ImageIO.read(getClass().getResourceAsStream("images/thumbnail.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
}
}