-
Notifications
You must be signed in to change notification settings - Fork 5
/
Svd2.java
139 lines (115 loc) · 3.79 KB
/
Svd2.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
package org.genericsystem.cv;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.math3.util.Precision;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Point3;
import org.opencv.core.Scalar;
public class Svd2 {
static {
NativeLibraryLoader.load();
}
public static List<Point3> solve(List<Point> srcPts, int[][] rects) {
List<Point3> pts = new ArrayList<>();
for (Point point : srcPts)
pts.add(new Point3(point.x, point.y, 1));
double[] stdxy = { Math.sqrt(pts.stream().mapToDouble(pt -> pt.x * pt.x).average().getAsDouble()), Math.sqrt(pts.stream().mapToDouble(pt -> pt.y * pt.y).average().getAsDouble()) };
for (Point3 pt : pts) {
pt.x /= stdxy[0];
pt.y /= stdxy[1];
}
double xmin = Double.POSITIVE_INFINITY;
double ymin = Double.POSITIVE_INFINITY;
double xmax = Double.NEGATIVE_INFINITY;
double ymax = Double.NEGATIVE_INFINITY;
for (Point3 pt : pts) {
if (pt.x < xmin)
xmin = pt.x;
if (pt.y < ymin)
ymin = pt.y;
if (pt.x > xmax)
xmax = pt.x;
if (pt.y > ymax)
ymax = pt.y;
}
double meanspan = Math.max(Math.max(Math.abs(xmin), Math.abs(xmax)), Math.max(Math.abs(ymin), Math.abs(ymax)));
double lambda = 1 / (meanspan * meanspan);
int n = pts.size();
int m = rects.length;
double[] polarity = { -1, 1, -1, 1 };
int nDim = 3;
System.out.println("n = " + n);
// m * nDim constraints....
Mat A = new Mat(nDim * m, 3 * n, CvType.CV_64FC1, new Scalar(0));
// coplanar terms...
for (int i = 0; i < m; i++) {
// % rect i: rects(i, 1) --- rects(i, 2)
// % | |
// % rects(i, 4) --- rects(i, 3)
for (int j = 0; j < nDim; j++) {
int constraint_index = nDim * i + j;
for (int k = 0; k < 4; k++)
A.put(constraint_index, (3 * rects[i][k]) + j, polarity[k]);
}
}
Mat B = new Mat(2 * n, 3 * n, CvType.CV_64FC1, new Scalar(0));
// data-terms...
for (int i = 0; i < n; i++) {
// % X - x_i Z...
B.put(2 * i, 3 * i, 1d);
B.put(2 * i, 3 * i + 2, -pts.get(i).x);
// % Y - y_i Z...
B.put(2 * i + 1, 3 * i + 1, 1d);
B.put(2 * i + 1, 3 * i + 2, -pts.get(i).y);
}
// solve the homogenous equation Az = 0
// [U, D, V] = svd(A + sqrt(lambda) * B);
// [minSingularValue, minIndex] = min(diag(D));
Mat dst = new Mat();
Core.gemm(A.t(), A, 1, new Mat(), 0, dst);
Mat dst2 = new Mat();
Core.gemm(B.t(), B, 1, new Mat(), 0, dst2);
Mat M = new Mat();
// Mat M = A.t() * A + lambda * B.t() * B;
Core.addWeighted(dst, 1, dst2, lambda, 0, M);
Mat eigenValues = new Mat();
Mat eigenVectors = new Mat();
Core.eigen(M, eigenValues, eigenVectors);
int minIndex = -1;
double minValue = Double.POSITIVE_INFINITY;
for (int i = 0; i < eigenValues.rows(); i++) {
if (eigenValues.get(i, 0)[0] > Precision.EPSILON && eigenValues.get(i, 0)[0] < minValue) {
minValue = eigenValues.get(i, 0)[0];
minIndex = i;
}
}
Mat result = eigenVectors.row(minIndex);
double sum = 0;
for (int i = 0; i < pts.size(); i++)
sum += result.get(0, 3 * i + 2)[0];
for (int i = 0; i < pts.size(); i++) {
pts.get(i).x = sum > 0 ? -result.get(0, 3 * i)[0] : result.get(0, 3 * i)[0];
pts.get(i).y = sum > 0 ? -result.get(0, 3 * i + 1)[0] : result.get(0, 3 * i + 1)[0];
pts.get(i).z = sum > 0 ? -result.get(0, 3 * i + 2)[0] : result.get(0, 3 * i + 2)[0];
}
// normalize it back
for (int i = 0; i < pts.size(); i++) {
pts.get(i).x *= stdxy[0];
pts.get(i).y *= stdxy[1];
}
return pts;
}
static double[] mul(double[][] data, double[] sol) {
double[] result = new double[data.length];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
result[i] += data[i][j];
}
}
return result;
}
}