-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBliffoscopeDetector.java
More file actions
78 lines (61 loc) · 2.21 KB
/
BliffoscopeDetector.java
File metadata and controls
78 lines (61 loc) · 2.21 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
package array.bliffoscope;
import java.util.*;
import java.awt.Point;
public class BliffoscopeDetector {
public final static double DEFAULT_ACCURACY = 0.75;
public static BliffoscopeDetector getInstance() {
return new BliffoscopeDetector( DEFAULT_ACCURACY );
}
private double accuracy;
private BliffoscopeDetector( double accuracy ) {
this.accuracy = accuracy;
}
/**
* @param value percentage must be within 0 to 1 range
* @see #detect(Image, Image)
*/
public void setAccuracy( double value ) {
if( value >= 0 && value <= 1 ) {
accuracy = value;
}
}
/**
* @return Detection accuracy percentage within 0 to 1 range
*/
public double getAccuracy() {
return accuracy;
}
/**
* Detects all potential instances of a target image in source image.
* Detection allows for approximation of target image. An accuracy percentage
* controls how exact a match has to be.
*
* @return A non-null list of detection results
* @see #setAccuracy(double)
* @throws NullPointerException if either source or target are null
*/
public List<DetectionResult> detect( Image source, Image target ) {
List<DetectionResult> results = new ArrayList<>();
int xEnd = source.getLength() - target.getLength();
int yEnd = source.getHeight() - target.getHeight();
for( int x = 0; x <= xEnd; x++ ) {
for( int y = 0; y <= yEnd; y++ ) {
Image subsetImage = source.createSubset( x, y, target.getLength(), target.getHeight() );
double imageSimilarity = subsetImage.calculateSimilarity( target );
if( imageSimilarity >= accuracy ) {
DetectionResult result = new DetectionResult( target.getName(), imageSimilarity );
int endTargetX = x + target.getLength() - 1;
int endTargetY = y + target.getHeight() - 1;
Point topLeft = new Point( x, y );
Point topRight = new Point( endTargetX, y );
result.setTopPoints( topLeft, topRight );
Point bottomLeft = new Point( x, endTargetY );
Point bottomRight = new Point( endTargetX, endTargetY );
result.setBottomPoints( bottomLeft, bottomRight );
results.add( result );
}
}
}
return results;
}
}