# Undistort Points in Julia

**URL:** <https://discourse.julialang.org/t/undistort-points-in-julia/113027>\
**Category:** Signal and Image Processing\
**Created:** [April 16, 2024, 4:08pm UTC](https://discourse.julialang.org/t/undistort-points-in-julia/113027 "2024-04-16T16:08:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennispureza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dennispureza/32/50957_2.png) [@dennispureza](https://discourse.julialang.org/u/dennispureza)\
**Post date:** [April 16, 2024, 4:08pm UTC](https://discourse.julialang.org/t/undistort-points-in-julia/113027/1 "2024-04-16T16:08:46Z")

</div>

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^2_k1 + 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.

```julia
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:

```julia
3×2 Matrix{Float64}:
 51.9125 53.0559
 98.9849 99.6313
 57.6659 46.6789

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 16, 2024, 6:50pm UTC](https://discourse.julialang.org/t/undistort-points-in-julia/113027/2 "2024-04-16T18:50:45Z")

</div>

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.
