Undistort Points in Julia

Hello everyone,

I was looking for a Julia package with a function to undistort image points like Matlab’s undistortPoints(), and since I couldn’t find any, I am currently trying to code a function myself.I already have a intrinsic matrix and distortion coefficients.

From Park et al, I got that:

[Xu] = [Xd][1+R^2k1 + R^4*k2] with R = sqrt(xd^2 + yd^2)

Xu are the undistorted points;
Xd are the distorted points.

I realize that the coordinates Xd must be normalized as:

Xn = (Xd-pp)/f

pp are the principal point coordinates;
f is the focal length.

And that the Xu results must be unnormalized later.

I wrote the code below, but results are not ok. I don’t know what is wrong, or maybe I got this completly wrong and it is much complicated than this. Any help or pointing to a more in depth documentation of how to do this is appreciated.

points = [10 23; 100 100.3; 23.1 20.5] #distorted points

nPoints = 3

fx = 517.3 #focal point
fy = 516.5 
ppx = 318.6 #principal point
ppy = 244.3
k1 = 0.2624 #distortion coefficients
k2 = -0.9531

function Normalise(x,y,ppx,ppy,fx,fy)
    
    xn = (x-ppx)/fx
    yn = (y-ppy)/fy

    return xn,yn

end

function UndistortPoints(points,k1,k2)

    XYu = Array{Float64,2}(undef,nPoints,2) #initialize empty undistorted points array
    
    for i in 1:nPoints
        
        xd = points[i,1] #distorted points
        yd = points[i,2]

        xdn,ydn = Normalise(xd,yd,ppx,ppy,fx,fy) #normalized distorted points

        Rd = sqrt(xdn^2+ydn^2) #distance from point to principal point

        xun = xdn*(1 + (Rd^2)*k1 + (Rd^4)*k2) #undistorted points
        yun = ydn*(1 + (Rd^2)*k1 + (Rd^4)*k2)

        XYu[i,1] = xun*fx + ppx #unnormalized undistorted points
        XYu[i,2] = yun*fy + ppy

    end

    return XYu

end

UndistortPoints(points,k1,k2)

Results are:

3×2 Matrix{Float64}:
 51.9125  53.0559
 98.9849  99.6313
 57.6659  46.6789
1 Like

Try to use LaTeX formulas instead of raw text to explain the problem.

The documentation of undistoredPoints in Matlab mentions a simple nonlinear least-squares problem. You can probably get the same result with existing packages for least-squares approximations.