@@ -51,37 +51,48 @@ yu = yc + (yd + yc) / (1 + K1*(r^2) + K2*(r^4) + ...)
5151```
5252
5353DevNotes (Contributions welcome):
54- - TODO manage image clamping if `dest` is too small and data should be cropped out.
55- - TODO buffer radii matrix for better reuse on repeat image size sequences
56- - TODO dispatch with either CUDA.jl or AMDGPU.jl <:AbstractArray objects.
57- - TODO use Tullio.jl with multithreading and GPU
58- - TODO check if LoopVectorization.jl tools like `@avx` help performance
54+ - TODO : provide a GPU implementation
55+ -> either via dispatch with AMDGPU and CUDA (should be avoided)
56+ -> or using a vendor-agnostic code with KernelAbstractions.jl
5957"""
6058function radialDistortion! (
6159 cc:: CameraCalibration{<:Real, N} ,
6260 dest:: AbstractMatrix ,
6361 src:: AbstractMatrix
6462 ) where {N}
65- # loop over entire image
66- for h_d in size (src, 1 ), w_d in size (src, 2 )
67- # temporary coordinates
68- @inbounds h_ = h_d - cc. center[1 ]
69- @inbounds w_ = w_d - cc. center[2 ]
70- # calculate the radius from distortion center
71- _radius2 = h_^ 2 + w_^ 2
72- # calculate the denominator
73- _denomin = 1
74- @inbounds @fastmath for k in 1 : N
75- _denomin += cc. kc[k] * (_radius2^ k)
63+
64+ center = SVector {2} (cc. center)
65+ k = ntuple (i -> cc. kc[i], N)
66+ H, W = size (src)
67+ c₁, c₂ = center
68+
69+ # pre-allocate buffers for the radius powers
70+ buf = Vector {T} (undef, max (H, W))
71+
72+ @tturbo for h_d in 1 : H, w_d in 1 : W
73+ h_ = h_d - c₁
74+ w_ = w_d - c₂
75+ r² = h_ * h_ + w_ * w_
76+
77+ num = one (T)
78+ rⁿ = r²
79+ @inbounds for i in 1 : N
80+ num += k[i] * rⁿ
81+ rⁿ *= r²
7682 end
77- # calculate the new 'undistorted' coordinates and set equal to incoming image
78- @inbounds @fastmath h_u = cc. center[1 ] + h_ / _denomin
79- @inbounds @fastmath w_u = cc. center[2 ] + h_ / _denomin
80- dest[h_u, w_u] = src[h_d, w_d]
83+
84+ h_u = c₁ + h_ / num
85+ w_u = c₂ + w_ / num
86+
87+ # nearest-neighbour lookup (round + clamp)
88+ hi = clamp (round (Int, h_u), 1 , H)
89+ wi = clamp (round (Int, w_u), 1 , W)
90+ dest[h_d, w_d] = src[hi, wi]
8191 end
8292 return nothing
8393end
8494
95+
8596"""
8697 intersectLineToPlane3D(planenorm, planepnt, raydir, raypnt) -> point
8798
0 commit comments