Developing an Algorithm for Undistorting an Image
This example develops a mathematical model using the Symbolic Math Toolbox™ to undistort an image and features a local function in the live script.
Background
Any real world point can be defined with respect to some 3-D world origin.
Relative to a camera lens, this 3-D point can be defined as , which is obtained by rotating and translating .
=
The 3-D point is then projected into the camera's image plane as a 2D point, (,).
,
When a camera captures an image, it does not precisely capture the real points, but rather a slightly distorted version of the real points which can be denoted (,). The distorted points can be described using the following function:
where:
, = radial distortion coefficients of the lens
, = tangential distortion coefficients of the lens
Distortion
An example of lens distortion is shown below; original distorted image (left) and undistorted image (right).
Note the curvature of the lines toward the edges of the first image. For applications such as image reconstruction and tracking, it is important to know the real world location of points. When we have a distorted image, we know the distorted pixel locations (,). It's our goal to determine the undistorted pixel locations (,) given (,) and the distortion coefficients of the particular lens.
While otherwise straightforward, the nonlinear nature of the lens distortion makes the problem challenging.
Define Distortion Model
We begin by defining our distortion model:
% Parameters syms k_1 k_2 p_1 p_2 real syms r x y distortionX = subs(x * (1 + k_1 * r^2 + k_2 * r^4) + 2 * p_1 * x * y + p_2 * (r^2 + 2 * x^2), r, sqrt(x^2 + y^2))
distortionX =
distortionY = subs(y * (1 + k_1 * r^2 + k_2 * r^4) + 2 * p_2 * x * y + p_1 * (r^2 + 2 * y^2), r, sqrt(x^2 + y^2))
distortionY =
Radial Distortion
We plot a grid of pixel locations assuming our lens has a radial distortion coefficient . Note that distortion is smallest near the center of the image and largest near the edges.
% Set Parameters
parameters = [k_1 k_2 p_1 p_2];
parameterValues = [0 0 0 0];
plotLensDistortion(distortionX,distortionY,parameters,parameterValues)
spacing = 0.2000
distortionX =
distortionY =
Radial Distortion
Explore the sensitivity to changes in .
% Set Parameters
parameters = [k_1 k_2 p_1 p_2];
parameterValues = [0.15 0 0 0];
plotLensDistortion(distortionX,distortionY,parameters,parameterValues)
spacing = 0.2000
distortionX =
distortionY =
Calculate Inverse Distortion Model
Given a camera's lens distortion coefficients and a set of distorted pixel locations (,), we want to be able to calculate the undistorted pixel locations (,). We will look at the specific case where all distortion coefficients are zero except for which equals 0.2.
We begin by defining the distortion coefficients.
syms X Y positive eq1 = X == distortionX
eq1 =
eq2 = Y == distortionY
eq2 =
We define the distortion equations for given distortion coefficients, and solve for the undistorted pixel locations (,).
parameters = [k_1 k_2 p_1 p_2]; parameterValues = [0.2 0 0 0]; eq1 = expand(subs(eq1, parameters, parameterValues))
eq1 =
eq2 = expand(subs(eq2, parameters, parameterValues))
eq2 =
Result = solve([eq1, eq2], [x,y], 'MaxDegree', 3,'Real',true)
Result = struct with fields:
x: (X*(((5*Y^3)/(2*(X^2 + Y^2)) + ((25*Y^6)/(4*(X^2 + Y^2)^2) + (125*Y^6)/(27*(X^2 + Y^2)^3))^(1/2))^(1/3) - (5*Y^2)/(3*(X^2 + Y^2)*((5*Y^3)/(2*X^2 + 2*Y^2) + ((25*Y^6)/(4*(X^2 + Y^2)^2) + (125*Y^6)/(27*(X^2 + Y^2)^3))^(1/2))^(1/3))))/Y
y: ((5*Y^3)/(2*(X^2 + Y^2)) + ((25*Y^6)/(4*(X^2 + Y^2)^2) + (125*Y^6)/(27*(X^2 + Y^2)^3))^(1/2))^(1/3) - (5*Y^2)/(3*(X^2 + Y^2)*((5*Y^3)/(2*X^2 + 2*Y^2) + ((25*Y^6)/(4*(X^2 + Y^2)^2) + (125*Y^6)/(27*(X^2 + Y^2)^3))^(1/2))^(1/3))
Since element 1 is the only real solution, we will extract that expression into its own variable.
[Result.x Result.y]
ans =
Now we have analytical expressions for the pixel locations X and Y which we can use to undistort our images.
Function for Drawing the Lens Distortion
function plotLensDistortion(distortionX,distortionY,parameters,parameterValues) % distortionX is the expression describing the distorted x coordinate % distortionY is the expression describing the distorted y coordinate % k1 and k2 are the radial distortion coefficients % p1 and p2 are the tangential distortion coefficients syms x y % This is the grid spacing over the image spacing = 0.2 % Inspect and parametrically substitute in the values for k_1 k_2 p_1 p_2 distortionX = subs(distortionX,parameters,parameterValues) distortionY = subs(distortionY,parameters,parameterValues) % Loop over the grid for x_i = -1:spacing:1 for y_j = -1:spacing:1 % Compute the distorted location xout = subs(distortionX, {x,y}, {x_i,y_j}); yout = subs(distortionY, {x,y}, {x_i,y_j}); % Plot the original point plot(x_i,y_j, 'o', 'Color', [1.0, 0.0, 0.0]) hold on % Draw the distortion direction with Quiver p1 = [x_i,y_j]; % First Point p2 = [xout,yout]; % Second Point dp = p2-p1; % Difference quiver(p1(1),p1(2),dp(1),dp(2),'AutoScale','off','MaxHeadSize',1,'Color',[0 0 1]) end end hold off grid on end