View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
barrel and pincushion lens distortion correction

4.4 | 12 ratings Rate this file 64 Downloads (last 30 days) File Size: 586 KB File ID: #37980 Version: 1.1
image thumbnail

barrel and pincushion lens distortion correction


Jaap de Vries (view profile)


31 Aug 2012 (Updated )

I = LENSDISTORT(I, k)corrects for radially symmetric distortions

| Watch this File

File Information

I = LENSDISTORT(I, k)corrects for radially symmetric distortions, where
I is the input image and k is the distortion parameter. lens distortion
can be one of two types: barrel distortion and pincushion distortion.
In "barrel distortion", image magnification decreases with
distance from the optical axis. The apparent effect is that of an image
which has been mapped around a sphere (or barrel). In "pincushion
distortion", image magnification increases with the distance from the
optical axis. The visible effect is that lines that do not go through the
centre of the image are bowed inwards, towards the centre of the image,
like a pincushion [1].

I = LENSDISTORT(...,PARAM1,VAL1,PARAM2,VAL2,...) creates a new image,
specifying parameters and corresponding values that control various aspects
of the image distortion correction. Parameter names case does not matter.

Parameters include:

'bordertype' String that controls the treatment of the image
                    edges. Valid strings are 'fit' and 'crop'. By
                    default, 'bordertype' is set to 'crop'.

'interpolation' String that specifies the interpolating kernel
                    that the separable resampler uses. Valid
                    strings are 'cubic', 'linear' and 'nearest'. By
                    default, the 'interpolation' is set to 'cubic'

'padmethod' string that controls how the resampler
                    interpolates or assigns values to output elements
                    that map close to or outside the edge of the input
                    array. Valid strings are 'bound', circular',
                    'fill', 'replicate', and symmetric'. By
                    default, the 'padmethod' is set to 'fill'

'ftype' Integer between 1 and 4 that specifies the
                    distortion model to be used. The models
                    available are

                    'ftype' = 1: s = r.*(1./(1+k.*r));

                    'ftype' = 2: s = r.*(1./(1+k.*(r.^2)));

                    'ftype' = 3: s = r.*(1+k.*r);

                    'ftype' = 4: s = r.*(1+k.*(r.^2));

                    By default, the 'ftype' is set to 4.

Class Support
An input intensity image can be uint8, int8, uint16, int16, uint32,
int32, single, double, or logical. An input indexed image can be uint8,
uint16, single, double, or logical.

  % read image
  I = imread('cameraman.tif');

  % Distort Image
  I2 = lensdistort(I, 0.1);

  % Display both images
  imshow(I), figure, imshow(I2)

[1], August 2012.

[2] Harri Ojanen, "Automatic Correction of Lens Distortion by Using
  Digital Image Processing," July 10, 1999.

[3] G.Vassy and T.Perlaki, "Applying and removing lens distortion in post
  production," year???

[4], August 2012.

Created by Jaap de Vries, 8/31/2012


Required Products Image Processing Toolbox
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
16 Dec 2016 Scott Weidenkopf

I notice that you are 'imdistcorrect' 3 times for RGB images, once for each color channel. Can't you process all three channels in one call for better performance?

Also I think that linear interpolation might make a better default. Just some thoughts; great work!

Comment only
14 Oct 2016 Peter Cai

09 Oct 2016 hu tao

hu tao (view profile)

Cool code.
It will be better if the distortion ratio is measured.

23 Oct 2015 Robert Reed

Excellent code! Thank you. Suggest you change the normalization or r to be compatible with the standard computer vision convention. Specifically, the standard normalization of r is the focal of the length of lens in pixel units ( r --> r/f) - not (r --> r/max(r) ). For example, the distortion coeffs k1, k2, etc. returned by the MATLAB Computer Vision cameraCalibrator app and other similar codes (e.g.Peter Kovesi or Daesik Kim websites) all use the r/f normalization. Also, serious computer vision people often use a 2 or 3 term array for k (power series in r^2). This is a minor upgrade that you might want to consider for a future upgrade.

Comment only
14 Jul 2015 PeterB

PeterB (view profile)

Can someone post an example with al the parameters? Somehow it isn't working out for me. I'm trying I = lens distort(I,-.1,2,'crop','linear', symmetric'). There's an error somewhere with the input arguments.

Comment only
01 Apr 2015 Yang Zhao

Hello Jaap,
Nice work. Thank you!

25 Feb 2015 Jaap de Vries

Jaap de Vries (view profile)

Hmm, Sina could you send me an example of what you trying to correct?


Comment only
24 Feb 2015 Sina Tebianian

Hi Jaap,Your code works perfectly but I have a question:
When k is positive the distortion function s=r(1+kr^2) should magnify the image through the edges producing a pincushion distortion. However, your algorithm produces an opposite trend with Barrel pincushion given by positive k’s and pincushion distortion with negative values. I went through your code and could not find the reason of this. Thanks for your help

02 Nov 2014 Jacques Nel

Matthijs, baie dankie! How do you know those are the correct settings for GoPro Hero 3? Of course they work, but is there a way to know them without using trial and error? Thanks!Jaap, thanks a lot!

Comment only
30 Sep 2014 Wern-Juin Choy

09 Jun 2014 Ken

Ken (view profile)

18 Mar 2014 Brooke

Brooke (view profile)

Does anyone's images get the edges chopped off when using the 'undistortimage' tool? If so how can I fix this to where my whole image is shown and not a cropped version. Thank you!

Comment only
19 Nov 2013 Sina Tebianian

Thanks Jaap. Yes, my images have pincushion distrotion and should be corrected. I saw that lensdistort(I,k) works well for small pincushion distortions specially for the central part of the image. The edges get the opposite effect of Barrel. I can send you a sample image I started working on as an example. How I can I use the other algorithms?

Comment only
14 Nov 2013 Jaap de Vries

Jaap de Vries (view profile)

Sina, do you have an image with radial distortion? barrel or pincushion.

if so, import the image and assign to a variable (e.g., I)

Then aply correction I2 = lensdistort(I, 0.1) play with the 0.1 to get the wanted effect.

Diffrerent algorithms are available if you wish too.

Comment only
13 Nov 2013 Sina Tebianian

Hi Jaap, I did not quite understand how to correct the distorsion i.e. from a distorted image arrive to the non-distorted one. I appreciate your help.

Comment only
12 Nov 2013 Bsh kh

Bsh kh (view profile)

16 Sep 2013 Jaap de Vries

Jaap de Vries (view profile)

Dank je wel Matthijs, :-)

Comment only
16 Sep 2013 Alaster McDonach

Many Thanks. Using it to undistort images;Fantastic

26 Aug 2013 Matthijs

For GoPro Hero3 use ftype=3 and k=-0.32. You're welcome :).

Comment only
02 Aug 2013 piao lin

good,it works,correct the image. wonderful。

18 Apr 2013 MML

MML (view profile)

13 Dec 2012 Jaap de Vries

Jaap de Vries (view profile)

Robert, thank you.
When a distortion is radial, you can apply an inverse correction. I guess this code can do both then. Yes the border correction as of now only works for the default correction function. It was actually one of the harder things to do.

Comment only
19 Nov 2012 Robert Jacobsen

Very nice code!

It appears that your border correction is intended for the default ftype only.

Regarding Eyal's comment, I think he means that this way of manipulating images is usually thought of as simulating distortion, not removing it.

24 Oct 2012 Jaap de Vries

Jaap de Vries (view profile)

Thank you Carlos, yes I don't understand Eyal's comment either, I even supplied example files and showed how they work. The distortion models are clearly shown to be axisymetrix, i.e., f(r).

Comment only
24 Oct 2012 Carlos

Carlos (view profile)

Awesome job, i dont understand the last post about creates distortion but does not correct for radially symmetric distortions.

05 Oct 2012 Eyal

Eyal (view profile)

The help is misleading.
The file creates distortion but does not correct for radially symmetric distortions.

Comment only
04 Sep 2012 Jaap de Vries

Jaap de Vries (view profile)

Please rate my files or leave helpful commenst so I can make improvements.


Comment only
31 Aug 2012 1.1

8-31-2012 Made some spelling corrections and added an example.pdf file and example images

Contact us