-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTriangleImagePositionLocator.java
154 lines (123 loc) · 4.71 KB
/
TriangleImagePositionLocator.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Copyright (C) 2014,2015 Maximilian Diedrich
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.hu_berlin.informatik.spws2014.ImagePositionLocator;
import org.opencv.core.MatOfFloat6;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.imgproc.Subdiv2D;
import java.util.ArrayList;
import java.util.List;
/**
* Implements an algorithm for matching GpsPoints to
* Point2D's based on Markers
*/
public class TriangleImagePositionLocator implements ImagePositionLocator {
IPLSettingsContainer settings;
private ArrayList<ProjectionTriangle> projs;
private Point2D imageSize;
public TriangleImagePositionLocator(Point2D imageSize, IPLSettingsContainer settings) {
this.imageSize = imageSize;
this.settings = settings;
}
public ArrayList<ProjectionTriangle> getProjectionTriangles() {
return projs;
}
public Point2D getPointPosition(GpsPoint currentPosition) {
if (projs == null || currentPosition == null)
return null;
System.out.println("\n\nNew Point : " + currentPosition.toString());
FPoint2D result = new FPoint2D();
double sum = 0;
double distances[] = new double[projs.size()];
double distToClosestPivot = Double.MAX_VALUE;
//Get closest pivot
for (int i = 0; i < projs.size(); i++) {
distances[i] = projs.get(i).getPivot().getSphericalDistance(currentPosition);
if (distToClosestPivot > distances[i])
distToClosestPivot = distances[i];
}
for (int i = 0; i < projs.size(); i++) {
double unnormdist = distToClosestPivot / distances[i];
double tmp = distanceFallofFunction(unnormdist) * projs.get(i).getWeigth();
int weight = (distances[i] < 0.01) ?
(int) (0.01 - distances[i]) * 1000 : 1;
FPoint2D dbgtmp = projs.get(i).project(currentPosition, weight);
result.fma(dbgtmp, tmp);
sum += tmp;
System.out.println("Proj of " + projs.get(i).toString() + " x:" + dbgtmp.x + " y:" + dbgtmp.y + " actweight: " + tmp);
}
result.div(sum);
System.out.println("Result: " + result.toString());
return new Point2D(result);
}
/**
* Builds ProjectionTriangles from triangulated markers.
* Requires OpenCV!
*/
public void newMarkerAdded(List<Marker> markers) {
if (markers.size() < 2) return;
if (markers.size() == 2) {
//Guess third marker
projs = new ArrayList<ProjectionTriangle>();
projs.add(new ProjectionTriangle(markers.get(0), markers.get(1)));
} else {
Subdiv2D subdiv = new Subdiv2D();
subdiv.initDelaunay(new Rect(0,0,imageSize.x,imageSize.y));
for (Marker m : markers)
System.out.println("-> " + m.realpoint.longitude + " / " + m.realpoint.latitude);
for (Marker m : markers)
subdiv.insert(new Point(m.imgpoint.x, m.imgpoint.y));
MatOfFloat6 mafloat = new MatOfFloat6();
subdiv.getTriangleList(mafloat);
float[] tmparray = mafloat.toArray();
ArrayList<ProjectionTriangle> tmplist = new ArrayList<ProjectionTriangle>();
for (int i = 0; i < tmparray.length; i += 6) {
Marker m1 = findMarkerByPoint(markers, tmparray[i], tmparray[i + 1]);
Marker m2 = findMarkerByPoint(markers, tmparray[i + 2], tmparray[i + 3]);
Marker m3 = findMarkerByPoint(markers, tmparray[i + 4], tmparray[i + 5]);
if (m1 != null && m2 != null && m3 != null)
tmplist.add(new ProjectionTriangle(m1, m2, m3,
settings.getMaxDissimilarityPercent(),
settings.getBadTriWeightPenalty(),
settings.getMinTriAngleSize()));
}
for (ProjectionTriangle mainPt : tmplist) {
for (ProjectionTriangle subPt : tmplist) {
if (mainPt != subPt)
mainPt.tryAddToProjGroup(subPt);
}
}
projs = tmplist;
}
}
/**
* Decides if the lng and lat values represent a GpsPoint of a Marker.
* @return The found Marker.
*/
private Marker findMarkerByPoint(List<Marker> markers, float x, float y) {
for (Marker m : markers) {
if (m.imgpoint.x == (int) x
&& (m.imgpoint.y == (int) y)) {
return m;
}
}
return null;
}
private double distanceFallofFunction(double d) {
if (d > 1) throw new IllegalArgumentException("The distance fallof is only defined from 0..1!");
return Math.pow(d, settings.getFallofExponent());
}
}