-
Notifications
You must be signed in to change notification settings - Fork 5
/
Calibrated.java
198 lines (168 loc) · 5.83 KB
/
Calibrated.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.genericsystem.cv;
import java.util.Arrays;
import java.util.List;
import org.genericsystem.cv.Deperspectiver.Line;
import org.genericsystem.cv.Deperspectiver.Lines;
public class Calibrated {
final double x, y, z;
public Calibrated(double[] vp, double[] pp, double f) {
double[] calibratedxyz = calibrate(vp, pp, f);
if (calibratedxyz[2] < 0) {
calibratedxyz[0] = -calibratedxyz[0];
calibratedxyz[1] = -calibratedxyz[1];
calibratedxyz[2] = -calibratedxyz[2];
}
x = calibratedxyz[0];
y = calibratedxyz[1];
z = calibratedxyz[2];
}
public Calibrated(double[] calibratedxyz) {
if (calibratedxyz[2] < 0) {
calibratedxyz[0] = -calibratedxyz[0];
calibratedxyz[1] = -calibratedxyz[1];
calibratedxyz[2] = -calibratedxyz[2];
}
x = calibratedxyz[0];
y = calibratedxyz[1];
z = calibratedxyz[2];
}
public double[] getCalibratexyz() {
return new double[] { x, y, z };
}
Calibrated dumpXyz(double[] xyz, int dumpSize) {
return new Calibrated(new double[] { ((dumpSize - 1) * x + xyz[0]) / dumpSize, ((dumpSize - 1) * y + xyz[1]) / dumpSize, ((dumpSize - 1) * z + xyz[2]) / dumpSize });
}
public static double[] calibrate(double[] vpImg, double[] pp, double f) {
double[] vp = new double[] { vpImg[0] / vpImg[2] - pp[0], vpImg[1] / vpImg[2] - pp[1], f };
if (vp[2] == 0)
vp[2] = 0.0011;
double N = Math.sqrt(vp[0] * vp[0] + vp[1] * vp[1] + vp[2] * vp[2]);
vp[0] *= 1.0 / N;
vp[1] *= 1.0 / N;
vp[2] *= 1.0 / N;
if (vp[2] < 0) {
vp[0] = -vp[0];
vp[1] = -vp[1];
vp[2] = -vp[2];
}
return vp;
}
public double[] uncalibrate(double[] pp, double f) {
double[] result = new double[3];
result[0] = x * f / z + pp[0];
result[1] = y * f / z + pp[1];
result[2] = 1.0;
return result;
}
public static class AngleCalibrated extends Calibrated {
final double theta;
final double phi;
public AngleCalibrated(double[] vp, double[] pp, double f) {
super(vp, pp, f);
theta = Math.atan2(this.y, this.x);
phi = Math.acos(this.z);
}
public AngleCalibrated(double[] calibratedxyz, Object nullObject) {
super(calibratedxyz);
this.theta = Math.atan2(this.y, this.x);
this.phi = Math.acos(this.z);
}
public AngleCalibrated(double theta, double phi) {
this(new double[] { theta, phi });
}
public AngleCalibrated(double[] tethaPhi) {
super(new double[] { Math.sin(tethaPhi[1]) * Math.cos(tethaPhi[0]), Math.sin(tethaPhi[1]) * Math.sin(tethaPhi[0]), Math.cos(tethaPhi[1]) });
this.theta = Math.atan2(this.y, this.x);
this.phi = Math.acos(this.z);
}
public double getTheta() {
return theta;
}
public double getPhi() {
return phi;
}
public double[] getThetaPhi() {
return new double[] { theta, phi };
}
public AngleCalibrated dumpThetaPhi(double[] tethaPhi, int dumpSize) {
return new AngleCalibrated(new double[] { ((dumpSize - 1) * theta + tethaPhi[0]) / dumpSize, ((dumpSize - 1) * phi + tethaPhi[1]) / dumpSize });
}
@Override
AngleCalibrated dumpXyz(double[] xyz, int dumpSize) {
return new AngleCalibrated(new double[] { ((dumpSize - 1) * x + xyz[0]) / dumpSize, ((dumpSize - 1) * y + xyz[1]) / dumpSize, ((dumpSize - 1) * z + xyz[2]) / dumpSize }, null);
}
public AngleCalibrated getOrthoFromAngle(double lambda) {
double[] vp1 = getCalibratexyz();
double k1 = vp1[0] * Math.sin(lambda) + vp1[1] * Math.cos(lambda);
double k2 = vp1[2];
double phi = Math.atan(-k2 / k1);
double[] result = new double[] { Math.sin(phi) * Math.sin(lambda), Math.sin(phi) * Math.cos(lambda), Math.cos(phi) };
if (result[2] == 0.0)
result[2] = 0.0011;
double N = Math.sqrt(result[0] * result[0] + result[1] * result[1] + result[2] * result[2]);
result[0] *= 1.0 / N;
result[1] *= 1.0 / N;
result[2] *= 1.0 / N;
// if (result[2] < 0) {
// result[0] *= -1.0;
// result[1] *= -1.0;
// result[2] *= -1.0;
// }
return new AngleCalibrated(result, null);
}
public AngleCalibrated getOrthoFromVps(Calibrated calibrated2) {
double[] vp1 = getCalibratexyz();
double[] vp2 = calibrated2.getCalibratexyz();
double[] vp3 = cross(vp1, vp2);
if (vp3[2] == 0.0)
vp3[2] = 0.0011;
double N = Math.sqrt(vp3[0] * vp3[0] + vp3[1] * vp3[1] + vp3[2] * vp3[2]);
vp3[0] *= 1.0 / N;
vp3[1] *= 1.0 / N;
vp3[2] *= 1.0 / N;
// if (vp3[2] < 0) {
// vp3[0] *= -1.0;
// vp3[1] *= -1.0;
// vp3[2] *= -1.0;
// }
return new AngleCalibrated(vp3, null);
}
static double[] cross(double[] a, double b[]) {
return new double[] { a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] };
}
@Override
public String toString() {
return "(" + theta * 180 / Math.PI + "°, " + phi * 180 / Math.PI + "°)";
}
public double distance(List<Line> lines, double[] pp, double f) {
double[] uncalibrate = uncalibrate(pp, f);
double error = 0;
for (Line line : lines)
error += Math.pow(Math.min(AngleCalibrated.distance(uncalibrate, line), 0.3) * line.size(), 2);
return error;
}
public double distance(Line line, double[] pp, double f) {
return Math.pow(Math.min(AngleCalibrated.distance(uncalibrate(pp, f), line), 0.3) * line.size(), 2);
}
static double distance(double[] vp, Line line) {
double dy = line.y1 - line.y2;
double dx = line.x2 - line.x1;
double dz = line.y1 * line.x2 - line.x1 * line.y2;
double norm = Math.sqrt(dy * dy + dx * dx + dz * dz);
double n0 = -dx / norm;
double n1 = dy / norm;
double nNorm = Math.sqrt(n0 * n0 + n1 * n1);
double[] midPoint = new double[] { (line.x1 + line.x2) / 2, (line.y1 + line.y2) / 2, 1d };
double r0 = vp[1] * midPoint[2] - midPoint[1];
double r1 = midPoint[0] - vp[0] * midPoint[2];
double rNorm = Math.sqrt(r0 * r0 + r1 * r1);
double num = r0 * n0 + r1 * n1;
if (num < 0)
num = -num;
double d = 0;
if (nNorm != 0 && rNorm != 0)
d = num / (nNorm * rNorm);
return d;
}
}
}