33
44import numpy as np
55from numba import float64 , int32 , njit
6- from numpy import cos , sin , sqrt
76
87from .calibration import Calibration
98from .parameters import ControlPar
@@ -153,7 +152,7 @@ def distort_brown_affine(x: float,
153152 if x == 0 and y == 0 :
154153 return 0 , 0
155154
156- r = sqrt (x ** 2 + y ** 2 )
155+ r = np . sqrt (x ** 2 + y ** 2 )
157156
158157 x += (
159158 x * (ap .k1 * r ** 2 + ap .k2 * r ** 4 + ap .k3 * r ** 6 )
@@ -166,15 +165,13 @@ def distort_brown_affine(x: float,
166165 + 2 * ap .p1 * x * y
167166 )
168167
169- x1 = ap .scx * x - sin (ap .she ) * y
170- y1 = cos (ap .she ) * y
168+ x1 = ap .scx * x - np . sin (ap .she ) * y
169+ y1 = np . cos (ap .she ) * y
171170
172171 return x1 , y1
173172
174173@njit (fastmath = True , cache = True , nogil = True )
175- def correct_brown_affine (
176- x : float , y : float , ap : np .recarray , tol : float = 1e-5
177- ) -> Tuple [float , float ]:
174+ def correct_brown_affine (x : float , y : float , ap : np .recarray , tol : float = 1e-5 ) -> Tuple [float , float ]:
178175 """Correct a distorted point using the Brown affine model."""
179176 r , rq , xq , yq = 0.0 , 0.0 , x , y
180177 itnum = 0
@@ -183,21 +180,29 @@ def correct_brown_affine(
183180 return xq , yq
184181
185182 rq = np .sqrt (x ** 2 + y ** 2 )
183+ two_p1 = 2 * ap .p1
184+ two_p2 = 2 * ap .p2
185+ cos_she = np .cos (ap .she )
186+ sin_she = np .sin (ap .she )
186187
187188 while True :
188189 r = rq
190+ common_term = (ap .k1 * r ** 2 + ap .k2 * r ** 4 + ap .k3 * r ** 6 )
191+ xq_common = xq * common_term
192+ yq_common = yq * common_term
193+
189194 xq = (
190- (x + yq * np . sin ( ap . she ) ) / ap .scx
191- - xq * ( ap . k1 * r ** 2 + ap . k2 * r ** 4 + ap . k3 * r ** 6 )
195+ (x + yq * sin_she ) / ap .scx
196+ - xq_common
192197 - ap .p1 * (r ** 2 + 2 * xq ** 2 )
193- - 2 * ap . p2 * xq * yq
198+ - two_p2 * xq * yq
194199 )
195200
196201 yq = (
197- y / np . cos ( ap . she )
198- - yq * ( ap . k1 * r ** 2 + ap . k2 * r ** 4 + ap . k3 * r ** 6 )
202+ y / cos_she
203+ - yq_common
199204 - ap .p2 * (r ** 2 + 2 * yq ** 2 )
200- - 2 * ap . p1 * xq * yq
205+ - two_p1 * xq * yq
201206 )
202207
203208 rq = np .sqrt (xq ** 2 + yq ** 2 )
@@ -207,27 +212,26 @@ def correct_brown_affine(
207212
208213 itnum += 1
209214
210- if itnum >= 201 or abs (rq - r ) / r <= tol :
215+ if itnum >= 201 or np . abs (rq - r ) <= tol * r :
211216 break
212217
213218 r = rq
214219 x1 = (
215- (x + yq * np . sin ( ap . she ) ) / ap .scx
216- - xq * ( ap . k1 * r ** 2 + ap . k2 * r ** 4 + ap . k3 * r ** 6 )
220+ (x + yq * sin_she ) / ap .scx
221+ - xq * common_term
217222 - ap .p1 * (r ** 2 + 2 * xq ** 2 )
218- - 2 * ap . p2 * xq * yq
223+ - two_p2 * xq * yq
219224 )
220225
221226 y1 = (
222- y / np . cos ( ap . she )
223- - yq * ( ap . k1 * r ** 2 + ap . k2 * r ** 4 + ap . k3 * r ** 6 )
227+ y / cos_she
228+ - yq * common_term
224229 - ap .p2 * (r ** 2 + 2 * yq ** 2 )
225- - 2 * ap . p1 * xq * yq
230+ - two_p1 * xq * yq
226231 )
227232
228233 return x1 , y1
229234
230-
231235def flat_to_dist (flat_x : float , flat_y : float , cal : Calibration ) -> Tuple [float , float ]:
232236 """Convert flat-image coordinates to real-image coordinates.
233237
0 commit comments