-
Notifications
You must be signed in to change notification settings - Fork 2
/
CircleFinder.java
62 lines (53 loc) · 2.47 KB
/
CircleFinder.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
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
public class CircleFinder {
private int threshold;
private int numberOfCircles;
private int minRadius;
private int maxRadius;
public CircleFinder(int threshold, int numberOfCircles, int minRadius, int maxRadius) {
this.numberOfCircles = numberOfCircles;
this.threshold = threshold;
this.minRadius = minRadius;
this.maxRadius = maxRadius;
}
/*
* Detect circles on the given image
*/
public List<Circle> circleDetection(BufferedImage image, double[][] sobel) throws Exception {
// sets a 3D integer space array to contain 'hits' circles
int[][][] accumulator = new int[image.getWidth()][image.getHeight()][maxRadius];
int max = 0;
for (int x = 0; x < image.getWidth(); x++)
for (int y = 0; y < image.getHeight(); y++) {
/*
* checks if the pixel is above threshold, checks if its coordinates are valid and increases accumulator
*/
if (sobel[x][y] > threshold) {
for (int rad = minRadius; rad < maxRadius; rad++) {
for (int t = 0; t <= 360; t++) {
Integer a = (int) Math.floor(x - rad * Math.cos(t * Math.PI / 180));
Integer b = (int) Math.floor(y - rad * Math.sin(t * Math.PI / 180));
// checks if a or b are outside the bounds of the image, then ignore them
if (!((0 > a || a > image.getWidth() - 1) || (0 > b || b > image.getHeight() - 1))) {
accumulator[a][b][rad] += 1;
if(accumulator[a][b][rad] > max){
max = accumulator[a][b][rad];
}
}
}
}
}
}
List<Circle> circles = new ArrayList<>();
for (int x = 0; x < image.getWidth(); x++)
for (int y = 0; y < image.getHeight(); y++)
for (int rad = minRadius; rad < maxRadius; rad++)
circles.add(new Circle(x, y, rad, accumulator[x][y][rad]));
return circles;
}
public int getNumberOfCircles() {
return this.numberOfCircles;
}
}