Lens distortion: Fixing pincushion

rondom

Senior Member
Messages
3,625
Solutions
3
Reaction score
1,085
Location
US
Almost all post processing software have some sort of lens distortion correction. I have been trying several of them to figure out how to handle a particular lens with significant pincushion. I have found that GIMP does an excellent job with the EDGE slider: It fixes the edges without distorting the rest of the image. Photoshop and others seem to apply an algorithm that effects the entire image, leaving no untouched pixel within the entire image surface: in short with almost all software, what you get as a final image has nothing to do with what the glass has rendered. With gimp, only the edges are corrected. Surely, this results with a smaller image (a crop) but that's perfectly OK.

Problem 1: I can't use GIMP for raw files.

Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.

In order to illustrate the issue, I am including two images (gif)

The camera and the lens make are not an issue here, as I will be using this conversion lens with an unintended body. But for sake of illustration I am showing you the lens (fuji's x100 tele converter) with the body that it was intended for. (fuji x100)

The first file is straight out of camera jpeg with in camera correction applied. (Incidentally the in-camera algorithm happens to mirror the photoshop, dxo optics, dce tools, lightroom etc)

the second one is applying the GIMP edge function, which I believe is superior in every sense: just fix the effected pixels.

I am curious to know what you all think.

Many thanks in advance.

(I hope gif files animate properly)



c5d3a0c6d8fc4355a9543fd3fd1fc57b.jpg.gif



5db99bd71ede40379c595fbbb89c2358.jpg.gif
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?

Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
 
(I hope gif files animate properly)
they do, but it is weird. just click "view: original size" on the main message window. for some reason if you click on the image it doesn't work properly)
 
Last edited:
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
You can't open a RAW file (in my case DNG) in GIMP- unless I am missing a plugin or something. But in any case a batch conversion is what I need therefore your recommendation is interesting. When I searched in google ImageMagick came up but I didn't pay enough attention to it. I will see if I can replicate it to perform the EDGE function of GIMP. So far no program that I have looked into seems to do that. It is rather frustrating.
 
Before, going further on using a particular program, I would like to hear from you guys regarding the proper fixing method. does what I have described above and illustrated with images make any sense to you? shouldn't the goal be fixing with least amount of pixel manipulation?
 
Last edited:
Almost all post processing software have some sort of lens distortion correction. I have been trying several of them to figure out how to handle a particular lens with significant pincushion. I have found that GIMP does an excellent job with the EDGE slider: It fixes the edges without distorting the rest of the image. Photoshop and others seem to apply an algorithm that effects the entire image, leaving no untouched pixel within the entire image surface: in short with almost all software, what you get as a final image has nothing to do with what the glass has rendered. With gimp, only the edges are corrected. Surely, this results with a smaller image (a crop) but that's perfectly OK.
As far as I can see, what GIMP does is more correct as it does not change the central part of the image (which is not affected by lens distortion).

I think what Photoshop and other programs do is to correct distortion and then resize the image back to its original size simply because most photographers do not expect or want distortion correction to change the image size.

Many mirrorless cameras use a slightly oversize sensor to allow distortion correction in camera without having to resize the result.
 
Almost all post processing software have some sort of lens distortion correction. I have been trying several of them to figure out how to handle a particular lens with significant pincushion. I have found that GIMP does an excellent job with the EDGE slider: It fixes the edges without distorting the rest of the image. Photoshop and others seem to apply an algorithm that effects the entire image, leaving no untouched pixel within the entire image surface: in short with almost all software, what you get as a final image has nothing to do with what the glass has rendered. With gimp, only the edges are corrected. Surely, this results with a smaller image (a crop) but that's perfectly OK.

Problem 1: I can't use GIMP for raw files.

Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Have you tried GIMP + UFRaw ?

UFRaw can be used either stand-alone (with batch processing capabilities), or as a plug-in for GIMP, so you have the option of using either or both in combination.

I have not checked out the latest lens correction options in UFRaw since moving to Rawtherapee for most of my processing, but they seem to be pretty flexible. I don't know how far they allow you to correct the edges while leaving the centre region mostly intact.

I agree that GIMP offers excellent control over distortion and perspective adjustments, and I still use it occasionally when Rawtherapee falls short.

HTH
 
Last edited:
Almost all post processing software have some sort of lens distortion correction. I have been trying several of them to figure out how to handle a particular lens with significant pincushion. I have found that GIMP does an excellent job with the EDGE slider: It fixes the edges without distorting the rest of the image. Photoshop and others seem to apply an algorithm that effects the entire image, leaving no untouched pixel within the entire image surface: in short with almost all software, what you get as a final image has nothing to do with what the glass has rendered. With gimp, only the edges are corrected. Surely, this results with a smaller image (a crop) but that's perfectly OK.
As far as I can see, what GIMP does is more correct as it does not change the central part of the image (which is not affected by lens distortion).

I think what Photoshop and other programs do is to correct distortion and then resize the image back to its original size simply because most photographers do not expect or want distortion correction to change the image size.

Many mirrorless cameras use a slightly oversize sensor to allow distortion correction in camera without having to resize the result.
Thanks for your opinion.

that was my initial thought too: then I made another gif loop with GIMP cropped followed by OOC JPEG from Fuji, effectively bringing them to same size (scale) and to my surprise the OOC output added some barrel distortion while fixing the pincushion. So not just matter of resizing although it is also happening as part of the algorithm.
 
Almost all post processing software have some sort of lens distortion correction. I have been trying several of them to figure out how to handle a particular lens with significant pincushion. I have found that GIMP does an excellent job with the EDGE slider: It fixes the edges without distorting the rest of the image. Photoshop and others seem to apply an algorithm that effects the entire image, leaving no untouched pixel within the entire image surface: in short with almost all software, what you get as a final image has nothing to do with what the glass has rendered. With gimp, only the edges are corrected. Surely, this results with a smaller image (a crop) but that's perfectly OK.

Problem 1: I can't use GIMP for raw files.

Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Have you tried GIMP + UFRaw ?

UFRaw can be used either stand-alone (with batch processing capabilities), or as a plug-in for GIMP, so you have the option of using either or both in combination.

I have not checked out the latest lens correction options in UFRaw since moving to Rawtherapee for most of my processing, but they seem to be pretty flexible. I don't know how far they allow you to correct the edges while leaving the centre region mostly intact.

I agree that GIMP offers excellent control over distortion and perspective adjustments, and I still use it occasionally when Rawtherapee falls short.

HTH

--
Alan Robinson
Thanks, I did and I spent quite a bit of time with it, but failed to replicate it. I wrote an email to the author but didn't receive a reply yet, and probably won't ever. what did the gimp developers use, I wonder. It must be based on someone's work. I found another extremely sophisticated program, hugin, but so far failed to make it work. I am sure in the right hands on of these programs has the answer for my problem.
 
Last edited:
After years of messing around the best process to address vignetting and geometric distortion is use fula to or nona to perform the corrections. I use Hugin's Lens Callibration tool on a few images heavy with vertical and horizontal lines. This burps out the the distortion correction values (a,b,c,d) used by fulla. You can save these to a file or use a standard library of lens information. Its not the best thing ever, but it is scriptable and it works. It also works against high bit depth images.

-- Bob
http://bob-o-rama.smugmug.com -- Photos
http://www.vimeo.com/boborama/videos -- Videos
http://blog.trafficshaper.com -- Blog
 
After years of messing around the best process to address vignetting and geometric distortion is use fula to or nona to perform the corrections. I use Hugin's Lens Callibration tool on a few images heavy with vertical and horizontal lines. This burps out the the distortion correction values (a,b,c,d) used by fulla. You can save these to a file or use a standard library of lens information. Its not the best thing ever, but it is scriptable and it works. It also works against high bit depth images.

-- Bob
http://bob-o-rama.smugmug.com -- Photos
http://www.vimeo.com/boborama/videos -- Videos
http://blog.trafficshaper.com -- Blog
Thank you, I see that's fulla and Nona are also based on panotools. If only I could figure out a b c and d work fo me.

I also tried hugins, and created a lens profile as an ini file, but how do I apply that to the image, this I couldn't figure out. Can the ini file be used in another program? This hugins thing seems to be a stiching software.

Frustrating.
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
Photograph a rectangular grid, a brick wall, or a sheet of graph paper.

Open in UFRaw and click on the lens correction tab. Select "Panotools model" from the drop-down menu. Adjust a, b, c parameters to minimise distortion. Save the configuration details or make a note of the values. Options -> configuration gives an XML listing which includes lens parameters which you should be able to cut and paste.

If your lenses are supported then UFRaw has an option to load appropriate parameters automatically.

Otherwise, you can use the saved a, b, c values with ImageMagick's convert command line tool, as suggested by Frans,

HTH
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
Photograph a rectangular grid, a brick wall, or a sheet of graph paper.

Open in UFRaw and click on the lens correction tab. Select "Panotools model" from the drop-down menu. Adjust a, b, c parameters to minimise distortion. Save the configuration details or make a note of the values. Options -> configuration gives an XML listing which includes lens parameters which you should be able to cut and paste.

If your lenses are supported then UFRaw has an option to load appropriate parameters automatically.

Otherwise, you can use the saved a, b, c values with ImageMagick's convert command line tool, as suggested by Frans,

HTH
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
Photograph a rectangular grid, a brick wall, or a sheet of graph paper.

Open in UFRaw and click on the lens correction tab. Select "Panotools model" from the drop-down menu. Adjust a, b, c parameters to minimise distortion. Save the configuration details or make a note of the values. Options -> configuration gives an XML listing which includes lens parameters which you should be able to cut and paste.

If your lenses are supported then UFRaw has an option to load appropriate parameters automatically.

Otherwise, you can use the saved a, b, c values with ImageMagick's convert command line tool, as suggested by Frans,

HTH
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
If the lens is in the lensfun database, you can get the coefficients needed for ImageMagick as described here. Just copy and paste - not so difficult. There are over 800 lenses in the Lensfun database

If your lens is not in the list of lensfun supported lenses, you can either make your own calibration, or alternatively and more easily shoot the calibration images and upload the images. A few days later the XML calibration file will pop up in your email inbox. I much prefer this option as it gets the lens into the database so others can benefit as well, and does so with good quality assurance.
 
Last edited:
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
If the lens is in the lensfun database, you can get the coefficients needed for ImageMagick as described here. Just copy and paste - not so difficult. There are over 800 lenses in the Lensfun database

If your lens is not in the list of lensfun supported lenses, you can either make your own calibration, or alternatively and more easily shoot the calibration images and upload the images. A few days later the XML calibration file will pop up in your email inbox. I much prefer this option as it gets the lens into the database so others can benefit as well, and does so with good quality assurance.
Craig, sounds good, thanks. The lens is in the database but in this case that's not helpful because this is a teleconverter lens and my intent is to you use it with a camera that it wasn't designed for. I actually had to modify the camera and the lens mount to get it work. So in this case I don't even know if the good people at lensfun will bother to create an entry for it, and If they do I don't even know how it will get labeled. But I'll try my luck.
 
Last edited:
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
Photograph a rectangular grid, a brick wall, or a sheet of graph paper.

Open in UFRaw and click on the lens correction tab. Select "Panotools model" from the drop-down menu. Adjust a, b, c parameters to minimise distortion. Save the configuration details or make a note of the values. Options -> configuration gives an XML listing which includes lens parameters which you should be able to cut and paste.

If your lenses are supported then UFRaw has an option to load appropriate parameters automatically.

Otherwise, you can use the saved a, b, c values with ImageMagick's convert command line tool, as suggested by Frans,

HTH
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
Sorry about that :)

You can try "convert in.tif -distort Barrel "0.027 -0.045 0.03 1.0" out.tif

Took about 10 minutes of playing with the parameters, but the above set appears to remove the bulk of the distortion.

You can fine-tune them better on a larger version of the image (I just used the GIF), but the general recipe that I would recommend, starting from the above parameters, is:

1. To reduce pincushion at the extreme edges (red rectangle in your image), increase the first parameter (0.027) very slightly, something like 0.029.

2. This will typically cause the image to have some barrel distortion. Pull back the barrel distortion by decreasing the second parameter, e.g., -0.045 is decreased to maybe -0.05.

3. This might cause a little bit of pincushion to come back to the inner-most black rectangle. Increasing the third parameter will straighten that (say, 0.03 -> 0.035), but this might cause a little more barrel distortion in the outer red rectangle again.

3. Rinse and repeat.

The important thing to understand is what region of the image is affected by which parameter: first parameter controls the extreme edge, the first parameter controls the area around the innermost black rectangle, and the second parameter controls the region inbetween (very roughly stated). I use a negative value for the second parameter to balance the first parameter a bit. Anyhow, hopefully this will give you a bit of a feel for the parameters.

All the other suggestions like using Hugin, panotools, lensfun, etc. will give you a more accurate, and possibly more automated way of finding the best set of coefficients, so it might be worth investing some time to get them to work. Otherwise, just play around with the initial parameters I provided above.
 
Problem 1: I can't use GIMP for raw files.
Do you mean raw files as in before Bayer demosaicing, or do you mean 16-bit files?
Problem 2: Ideally, I would like to use a command line program that I can automatically run the raw files through.
Try ImageMagick "convert": distort

This will give you full control over the radial distortion correction, but it might be a bit tricky to find the correct coefficients manually. If you can estimate sufficient points on a regular chart to build an approximation to "map out" your radial distortion, then you can simply use R to fit the required polynomial to obtain the coefficients to use with ImageMagick.

ImageMagick usually works just fine with 16-bit images, although that means you will have to perform your demosaicing before you remove lens distortion with ImageMagick (not that I think the order should matter).
Thank you for your suggestion but I need a degree in physics and computer science to make this work :)
Sorry about that :)

You can try "convert in.tif -distort Barrel "0.027 -0.045 0.03 1.0" out.tif

Took about 10 minutes of playing with the parameters, but the above set appears to remove the bulk of the distortion.

You can fine-tune them better on a larger version of the image (I just used the GIF), but the general recipe that I would recommend, starting from the above parameters, is:

1. To reduce pincushion at the extreme edges (red rectangle in your image), increase the first parameter (0.027) very slightly, something like 0.029.

2. This will typically cause the image to have some barrel distortion. Pull back the barrel distortion by decreasing the second parameter, e.g., -0.045 is decreased to maybe -0.05.

3. This might cause a little bit of pincushion to come back to the inner-most black rectangle. Increasing the third parameter will straighten that (say, 0.03 -> 0.035), but this might cause a little more barrel distortion in the outer red rectangle again.

3. Rinse and repeat.

The important thing to understand is what region of the image is affected by which parameter: first parameter controls the extreme edge, the first parameter controls the area around the innermost black rectangle, and the second parameter controls the region inbetween (very roughly stated). I use a negative value for the second parameter to balance the first parameter a bit. Anyhow, hopefully this will give you a bit of a feel for the parameters.

All the other suggestions like using Hugin, panotools, lensfun, etc. will give you a more accurate, and possibly more automated way of finding the best set of coefficients, so it might be worth investing some time to get them to work. Otherwise, just play around with the initial parameters I provided above.
Thank you for your time. Appreciate your help. and also thanks for the explanation on which parameters does what. First of all let me understand this: does imagemagick let you graphically manipulate these values because I couldn't find a graphic interface for distortion. Ufraw seems to be better in that regard. But maybe I am missing it? I installed the windows version and it doesn't appear to be available. So I'm assuming you are just using the command line to change the values? Am I correct?

As I tried to describe in my first post, the images I shared were for sake of illustration and showed the conversion lens used with the dedicated camera. It behaves differently in combination with my camera:More pincushion. So I will need to try this with different values.
 

Keyboard shortcuts

Back
Top