Like most websites, SGL uses cookies in order to deliver a secure, personalised service, to provide social media functions and to analyse our traffic. Continued use of SGL indicates your acceptance of our cookie policy.

Lens distortion and correcting

Recommended Posts

When building mosaics and when doing very wide field photographing it could sometimes be useful to correct the optical distortion.

I try to implement something in the software AstroImageJ were I can do it. There are some plugins but still a bit tricky to get it to work. It's a self learning project/tutorial for myself and maybe others also find it interesting.

Here what I have written down so far:
http://www.astrofriend.eu/astronomy/tutorials/tutorial-astroimagej-optical-distortion/tutorial-aij-01-optical-distortion.html

It's slowly in progress. The last thing I have done this week is to find the parameters of the optic distortion, page II.

i.e. find a, b and c

r' = a*r + b*r^2 + c*r^3 ...

Correction for rotation symmetric distortion.

There are other software that can do this very easy I think, but I want to do it from the ground to learn more and have more control over it.

/Lars

Edited by Astrofriend

Share on other sites

Interesting. I once recommended Hugin to a guy from ESA who needed to know how to undo the distortions of a certain Nikon lens in a set of images taken through such a lens. He bought the lens, shot a few panoramas with it and quantified its distortion using Hugin to stitch the panoramas. It worked well.

Hugin reported the distortion coefficients calculated with its Pano Tools routines (Pano Tools is by Helmut Dersch).  Many stitchers depend on Pano Tools and it works  pretty well.

Have you tried a panorama taken from a high vantage point, with no nearby objects in the shots?  It's hard to know where the nodal point of a lens is, and for valid coefficients the nodal point of the lens must be in the same place for all photos used. But, for distant objects it matters less if you don't rotate the camera precisely around the lens's nodal point. You might get better data and faster results with a real life distant pictures panorama.

Also, for different focal lengths and different focal distances the lenses inside the objective move to different positions, and you will get different coefficients. The nodal point too moves around inside the objective.

Share on other sites

How much are you willing to implement your self for this in ImageJ?

I can give you a rough sketch of algorithm that would do pretty good job of aligning images regardless of any distortion but there are bunch of bits and pieces that you will need to code additionally to get a usable plugin.

First off is understanding how simple align process works:

1. Identify star centers (some sort of centroid / gaussian fit)

2. Match stars in two image (either by intensity, using RANSAC, or matching triangles - or combination thereof)

3. Least squares method for finding transform matrix (bit of matrix algebra) on pair of star coordinates

To have working algorithm that maps given any sort of distortion, you first need to adjust transform matrix to each star (reference point), so while your matrix will not work on all stars to match them to high degree, you can always make per star matrix that will ideally match that particular star and minimize error in all other stars - this will give you bunch of transform matrices (one for each star).

Now you can take a point and apply weighted transform depending on closeness of that point to any particular star - take neighboring stars and combine their transform matrices depending on distance of that particular point to each of neighboring stars (weighted sum of matrices depending on point distances).

Share on other sites

Hi Vlaiv,

Thanks a lot for the information and suggestions how to solve it.

The matemathics is not the big problem, more how to implement it in AstroImageJ. I took a look at the PixInsight software, what I can see it works in a simular way that AstroImageJ do, but much more astro function friendly and there is also a lot of functions that I can use. But I like the open sourch software better. There is also Imagej2, but can't see many functions oriented to astronomy yet, maybe in the future, at least they are talking about ImageJ2 as a base for astronomy functions.

Lets see how it develops, now I'm preparing to do some astrophotographing, without images, no image processing. Sorry to say, I'm living in a big city.

/Lars

Share on other sites
2 hours ago, Astrofriend said:

Hi Vlaiv,

Thanks a lot for the information and suggestions how to solve it.

The matemathics is not the big problem, more how to implement it in AstroImageJ. I took a look at the PixInsight software, what I can see it works in a simular way that AstroImageJ do, but much more astro function friendly and there is also a lot of functions that I can use. But I like the open sourch software better. There is also Imagej2, but can't see many functions oriented to astronomy yet, maybe in the future, at least they are talking about ImageJ2 as a base for astronomy functions.

Lets see how it develops, now I'm preparing to do some astrophotographing, without images, no image processing. Sorry to say, I'm living in a big city.

/Lars

AstroImageJ is of course based on ImageJ, so plugins written for them should be interchangeable.

All being implemented in Java - plugins are straight forward to do. You just extend base plugin class and write your code. There is well documented API for it. Not sure if there is one for AstroImageJ, but having support code related to astronomy is certainly a plus - you would not need to implement all the little helpers that you would need (like star detection / centroid or gaussian fitting - these are already present in AstroImageJ - it handles photometry with aperture photometry very well).

Api reference: https://imagej.nih.gov/ij/developer/api/index.html

Here is what a simple plugin looks like - you can use that as skeleton code to do your own little plugins:

```import ij.*;
import ij.plugin.filter.*;
import ij.process.*;
import ij.measure.*;
import ij.gui.*;

import java.awt.*;

public class Cosmetic_correction implements PlugInFilter {

ImagePlus imp;
ImageProcessor ip;

public int setup(String arg, ImagePlus imp) {
IJ.register(Cosmetic_correction.class);
if (IJ.versionLessThan("1.32c") || imp==null)
return DONE;
imp.unlock();
this.imp = imp;
return DOES_32;
}

public void run(ImageProcessor ip) {
int width = imp.getWidth();
int height = imp.getHeight();

GenericDialog dia = new GenericDialog("Cosmetic correction", IJ.getInstance());

dia.showDialog();

if (dia.wasCanceled())
return;

float threshold = (float)dia.getNextNumber();

ImageStack imp_stack = imp.getImageStack();

int slices = imp_stack.size();

for(int i=0; i<slices;i++) {
FloatProcessor proc = (FloatProcessor) imp_stack.getProcessor(i+1);

FloatProcessor smooth_proc = (FloatProcessor)proc.duplicate();

RankFilters rf =  new RankFilters();

rf.rank(smooth_proc, 3, rf.MEDIAN);

for(int y=0; y<height; y++)
for(int x=0; x<width; x++) {
float original = proc.getPixelValue(x,y);
float smooth = smooth_proc.getPixelValue(x,y);

float diff = Math.abs((original - smooth) / smooth);

if(diff>threshold)
proc.putPixelValue(x,y, smooth);
}

IJ.showProgress(i+1, slices);
}

}

}	```

Share on other sites

Hi Vlaiv

Thanks a lot for all the details. When reading through this I got ideas why the plugins I have download lately doesn't work. I think there is some problem with the compilation of them. In the links/text you provided I found other compilators that I can use. When I get some sparetime later I shall down load these compilators and redo the Java run file. I'm not very used with Java so it take some time for me.

Now I'm working with the photos I took with my Pentax 67 medium format lens at M45 nebula. Some clouds destroyed it for me but at least I got something to do analyze on.

Again, thanks a lot!

/Lars

Create an account

Register a new account