# Question about Lens distortion

Suppose I have a 240x180 camera with the following distortion co-efficients,

$$k_1 = -0.138592767408$$ $$k_2=0.0933736664192$$ $$k_3=0.0$$ $$p_1=k_4=-0.000335586987532$$ $$p_2=k_5=0.000173720158228$$

The camera center is given to be

$$u_0 = 129.924663379$$ $$v_0 = 99.1864303447$$

And the distortion formulae are

$$r^2=(u-u_0)^2+(v-v_0)^2$$

\begin{align} \begin{bmatrix} u_d\\ v_d \end{bmatrix} = & (1+k_1r^2+k_2r^4+k_3r^6) \begin{bmatrix} u-u_0\\ v-v_0 \end{bmatrix} + \\ & \begin{bmatrix} 2k_4(u-u_0)(v-v_0)+k_5(r^2+2(u-u_0)^2)\\ k_4(r^2+2(v-v_0)^2+2k_5(u-u_0)(v-v_0) \end{bmatrix} + \begin{bmatrix} u_0\\ v_0 \end{bmatrix} \end{align}

In the above formulae, $$p_1=k_4$$ and $$p_2=k_5$$, $$u$$ and $$v$$ are the undistorted co-ordinates and $$u_d$$, $$v_d$$ the corresponding distorted ones.

My understanding of the undistorted co-ordinates is that it is a mapping of every undistorted co-ordinate to a distorted one. Therefore, the ranges of both undistorted and distorted co-ordinates should be between (0-240, 0-180). However, going by the above formula, I get extremely high numbers for distorted co-ordinates if I input undistorted co-ordinates in the range(0-240, 0-180) which goes against this understanding. Have I done something wrong here?

• On Robotics we are fortunate enough to have MathJax support enabled, allowing you to easily create subscripts, superscripts, fractions, square roots, greek letters and more. This allows you to add both inline and block element mathematical expressions in robotics questions and answers. For a quick tutorial, take a look at How can I format mathematical expressions here, using MathJax? – Mark Booth Oct 16 '18 at 13:11

## 2 Answers

You can only say that the distorted image coordinates are in the range (0-240, 0-180), since that's the image you are starting with. Typically you assume the dimensions of the undistorted image as being the same as the distorted image, and for every pixel in the undistorted image work out the corresponding coordinate in the distorted (input) image. It will have non-integer coordinates so often an interpolation is done to find the pixel value to drop into the undistorted image. It's possible that some values in the undistorted image map to points outside the distorted image, you should check for that, and maybe need to crop the result to eliminate pixels of indeterminate value.

A quick sanity check indicates that there's something badly wrong with your distortion coefficients, they are just too big. Take a value of r=100 which is possible towards the edge of your image, $$k_1 r^2 \approx 1300$$ which is a massive shift. The $$k_2 r^4$$ term is of the order of millions.

Do you know what the units of the distortion parameters are? They might be in units of pixels or units of metres on the image plane.

Hmm. Have you check the indices used in your code? Simple bugs like swapping i and j happen all the time for rows/columns.

Perhaps its a Hardware issue involving Little Endian vs BIG ENDIAN. in 8 bit 64=64 but 16 BIT 64 is swapped to highorder thus theres rotation in bit order results that the single bit 64 is represents as 256*64 the 256 comes from 2^8