File Exchange

image thumbnail

barrel and pincushion lens distortion correction

version 1.1 (586 KB) by

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

4.46154
13 Ratings

39 Downloads

Updated

View License

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.

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

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

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

References
--------------
[1] http://en.wikipedia.org/wiki/Distortion_(optics), 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] http://www.mathworks.com/help/images/examples/creating-a-gallery-of-transformed-images.html, August 2012.

Created by Jaap de Vries, 8/31/2012
jpdvrs@yahoo.com

-----------------------------------------------------------------------%

Comments and Ratings (28)

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!

Peter Cai

hu tao

hu tao (view profile)

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

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.

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.

Yang Zhao

Hello Jaap,
Nice work. Thank you!

Jaap de Vries

Jaap de Vries (view profile)

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

Jaap

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

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!

Ken

Ken (view profile)

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!

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?

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.

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.

Bsh kh

Bsh kh (view profile)

Jaap de Vries

Jaap de Vries (view profile)

Dank je wel Matthijs, :-)

Many Thanks. Using it to undistort images;Fantastic

Matthijs

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

piao lin

good,it works,correct the image. wonderful。

MML

MML (view profile)

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.

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.

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).

Carlos

Carlos (view profile)

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

Eyal

Eyal (view profile)

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

Jaap de Vries

Jaap de Vries (view profile)

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

J.

Updates

1.1

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

MATLAB Release
MATLAB 7.14 (R2012a)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today