1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.ar.epipolar;
17
18 import com.irurueta.geometry.CoordinatesType;
19 import com.irurueta.geometry.Line2D;
20 import com.irurueta.geometry.Point2D;
21 import com.irurueta.geometry.estimators.NotReadyException;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class SampsonSingleCorrector extends SingleCorrector {
40
41
42
43
44 public SampsonSingleCorrector() {
45 super();
46 }
47
48
49
50
51
52
53
54 public SampsonSingleCorrector(final FundamentalMatrix fundamentalMatrix) {
55 super(fundamentalMatrix);
56 }
57
58
59
60
61
62
63
64 public SampsonSingleCorrector(final Point2D leftPoint, final Point2D rightPoint) {
65 super(leftPoint, rightPoint);
66 }
67
68
69
70
71
72
73
74
75 public SampsonSingleCorrector(final Point2D leftPoint, final Point2D rightPoint,
76 final FundamentalMatrix fundamentalMatrix) {
77 super(leftPoint, rightPoint, fundamentalMatrix);
78 }
79
80
81
82
83
84
85
86 @Override
87 public void correct() throws NotReadyException {
88 if (!isReady()) {
89 throw new NotReadyException();
90 }
91
92 leftCorrectedPoint = Point2D.create(CoordinatesType.HOMOGENEOUS_COORDINATES);
93 rightCorrectedPoint = Point2D.create(CoordinatesType.HOMOGENEOUS_COORDINATES);
94 correct(leftPoint, rightPoint, fundamentalMatrix, leftCorrectedPoint, rightCorrectedPoint);
95 }
96
97
98
99
100
101
102 @Override
103 public CorrectorType getType() {
104 return CorrectorType.SAMPSON_CORRECTOR;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public static void correct(final Point2D leftPoint, final Point2D rightPoint,
121 final FundamentalMatrix fundamentalMatrix, final Point2D correctedLeftPoint,
122 final Point2D correctedRightPoint) throws NotReadyException {
123
124 final var leftEpipolarLine = new Line2D();
125 final var rightEpipolarLine = new Line2D();
126 correct(leftPoint, rightPoint, fundamentalMatrix, correctedLeftPoint, correctedRightPoint, leftEpipolarLine,
127 rightEpipolarLine);
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 protected static void correct(
149 final Point2D leftPoint, final Point2D rightPoint, final FundamentalMatrix fundamentalMatrix,
150 final Point2D correctedLeftPoint, final Point2D correctedRightPoint, final Line2D leftEpipolarLine,
151 final Line2D rightEpipolarLine) throws NotReadyException {
152
153
154 leftPoint.normalize();
155 rightPoint.normalize();
156 fundamentalMatrix.normalize();
157
158 fundamentalMatrix.leftEpipolarLine(rightPoint, leftEpipolarLine);
159 fundamentalMatrix.rightEpipolarLine(leftPoint, rightEpipolarLine);
160
161 leftEpipolarLine.normalize();
162 rightEpipolarLine.normalize();
163
164 final var numerator = rightPoint.getHomX() * rightEpipolarLine.getA()
165 + rightPoint.getHomY() * rightEpipolarLine.getB()
166 + rightPoint.getHomW() * rightEpipolarLine.getC();
167
168 final var denominator = Math.pow(rightEpipolarLine.getA(), 2.0)
169 + Math.pow(rightEpipolarLine.getB(), 2.0)
170 + Math.pow(leftEpipolarLine.getA(), 2.0)
171 + Math.pow(leftEpipolarLine.getB(), 2.0);
172
173 final var factor = numerator / denominator;
174
175 final var leftCorrectionX = factor * leftEpipolarLine.getA();
176 final var leftCorrectionY = factor * leftEpipolarLine.getB();
177 final var rightCorrectionX = factor * rightEpipolarLine.getA();
178 final var rightCorrectionY = factor * rightEpipolarLine.getB();
179
180 final var correctedLeftX = leftPoint.getInhomX() - leftCorrectionX;
181 final var correctedLeftY = leftPoint.getInhomY() - leftCorrectionY;
182 final var correctedRightX = rightPoint.getInhomX() - rightCorrectionX;
183 final var correctedRightY = rightPoint.getInhomY() - rightCorrectionY;
184
185 correctedLeftPoint.setInhomogeneousCoordinates(correctedLeftX, correctedLeftY);
186 correctedRightPoint.setInhomogeneousCoordinates(correctedRightX, correctedRightY);
187 }
188 }