Jump to content

Camera lens distortion correction


Recommended Posts


Here is a simple plugin to correct the barrel or pincushion distortions in digital photos.

(More information about these distortions in wikipedia article)


You can download the plugin here:




To use it simply unzip it to c:/Program Files/Paint.NET/Effects/, the plugin is in Filters->Photo->Distortion


Here is an example of the plugin usage. On the first photo you can see the effect of the lens distortion (I traced it with dotted red line), which was eliminated in the second image using the plugin:



This effect is even stronger with macro shots, cheaper lenses on compact cameras and ultra-wide (fisheye) lenses.

I wrote the plugin using Codelabs, if there is someone who would like to implement it in C# (in order to give a nice interface or to optimize it), i can post the source code. :D

Edited by toe_head2001
Fixed attachment
  • Upvote 1
Link to comment
Share on other sites

Yes, sure I may help you (if you want me to)...

I already think about the IndirectUI features:

...Choose the center X,Y Offset,

...Keep background (yes/no, instead transparent)

...X,Y separated factors

...Limits +/- 500 (instead 50) : ok perhaps it's crazy, but just for the fun of distortion!

Like I did here:


(you could use this source as a template by your own...)

Let me(us) know...

Link to comment
Share on other sites

Screenshots please!

I made some. @Pyro: This issue is occuring when I use it, too.

Edit: Please note: I forgot to change the language to English. To prevent confusion, "Abbrechen" is not a weird plugin-related button, but means just "Cancel". :wink:

And please forgive me the bad quality of the pictures, but I want them to be suitable for all bandwiths.




Link to comment
Share on other sites

Good people, here's the code for CodeLabs! Have fun playing with it, I hope you'll make it even cooler!

int Amount1=-20;   //[-50,50] Amount per thousand(°/oo)

float [] rd = null;
int Achange=0;	//To verify if there is a slider change

void Render(Surface dst, Surface src, Rectangle rect)
   float Percent= (float)(Amount1 / 500.0f);  // Distortion percent

   PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);

   Rectangle selection = this.EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();

   int w,h,x0,y0,w2,h2,raymax;
   float xd,yd,rdcur,ru;
   w = selection.Right - selection.Left;
   h = selection.Bottom - selection.Top;
   if(rd == null){ //first run->alloc and init
       rd = new float[raymax];
   }else if(Achange!=Amount1){  //slider change->reinit
   for(int y = rect.Top; y < rect.Bottom; y++){
       for (int x = rect.Left+1; x < rect.Right/2+1; x++){	//BUG: if it goes from rect.Left it gives an exception
           rdcur=rd[(int)ru]*(1-ru+(float)Math.Floor(ru)) +  rd[(int)ru+1]*(ru-(float)Math.Floor(ru));
    dst[x,y] = src.GetBilinearSample(w2-xd,h2-yd);
    dst[w-x,y] = src.GetBilinearSample(w2+xd,h2-yd);
       }	//idea of optimisation: the function is symmetrical horizontally and vertically,
       	//so at the same moment (x,y),(w-x,y),(x,h-y),(w-x,h-y) can be calculated
void Init(int raymax, float percent, int w, int h)
{		//We precalculate the distortion function in rd
   	double k,r,inc,ru1,ru0,m;
int l;
// *** Calculate K *** (k is the distortion constant based on the distortion %)
double yd1,yd2,rd2,alfa;

while(l		ru0=r+r*r*r*k;

Link to comment
Share on other sites

And sorry I didn't post any screenshots, here's mine, a real example with lens distortion:


On the original I marked the distorted edgees with red dotted line. There are lenses with even stronger distortions (especially the wide angle zoom lenses). I hope you find this plugin usefull.

Link to comment
Share on other sites

I updated the original plugin, still using my codelabs version. You can download it in the original post!

This fixes just the nasty edge bug, there is no other change.

Thanks for the quick modification, MadJik, I think I'll modify your version too (when i'll have time)!

Link to comment
Share on other sites

There are some major rendering goof-ups if you move the center/offset around. It looks like the effect is not writing to every pixel in DstArgs, and so they just retain their old values.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html


Link to comment
Share on other sites

  • 3 weeks later...
  • 8 years later...

I doubt kbarni is still visiting the forum, so not too much point in posting this?. (I was using it within a selection).

Shame, as it is a useful effect. Thanks for posting the code, I may try to squash some bugs :bug: at some point.


File: C:\Program Files\NEWpdn\Effects\kb_lens.dll
      Effect Name: PaintDotNet.Effects.UserScript
      Full error message: PaintDotNet.WorkerThreadException: Worker thread threw an exception ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.


Red ochre Plugin pack.............. Diabolical Drawings ................Real Paintings



Link to comment
Share on other sites

  • 4 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...