Log in

View Full Version : Defish plugin for correcting wide angle distortion


wonkey_monkey
27th June 2007, 17:39
A little while back (in this thread (http://forum.doom9.org/showthread.php?t=126916)) I asked if there was a plugin to correct the circular distortion caused by fisheye and other non-rectilinear lenses, such as you find in camcorders.

There wasn't then, but there is now - it's my first plugin and it's definitely beta (and this post is currently all the documentation there is), but if anyone thinks they can make use of it, please give it a try and let me know of any bugs. Source code is included, but is not well commented.

Here's what it does:

http://www.channeltv.co.uk/defish.jpg

It takes a few parameters:


fov - the horizontal field of view of the image in degrees (must be greater than 0).
aspect - the pixel aspect ratio of the image (1 if you have square pixels, 0.6875 for anamorphic PAL, etc -defaults to 1)
scale - 0 by default, which automatically scales the image to show as much of the image as possible without showing the edges. scale=1 will keep the centre of the image at the original scale. You'll get an error if you try a value that is smaller than the automatic value (this restriction may be removed later, but for now if you want some crazy effects, try adding borders and increasing fov).
resample - 1 (default): nearest neighbour, 2: bilinear interpolation (a better interpolator will be added later)


I've missed out on some parameter checks, so don't be surprised if you play around and get a blank clip :cool:

Download (http://www.channeltv.co.uk/defish.zip)

Oh yes, I almost forgot - input must be RGB32 (I haven't decided if I'll bother with other colour spaces later) and have an even width and height.

David

Fizick
27th June 2007, 19:51
congrat with a first plugin! :)

Is fish-eye focal length constant or variable (zoomed) in camcorders?

Is it possible to automaticaly calculate the needed anti-fish correction?

foxyshadis
27th June 2007, 23:13
Does it correct barrel and pincushion distortion as well, or is it only good for linear distortion currently? (Pin/barrel distortions can be modeled on something like ±xⁿ where n is some high even number, rather than the linear/parabolic of fisheye. Mostly flat, then sharp upturn.)

wonkey_monkey
27th June 2007, 23:23
Is fish-eye focal length constant or variable (zoomed) in camcorders?

It's constant for a given level of zoom, if that's what you mean...

Is it possible to automaticaly calculate the needed anti-fish correction?

Not with my plugin, but you could use something like PTAssembler. It allows you to mark what should be straight lines and will calulate the fov and other parameters (such as pincushion and barrel). I just adjust by eye until I get a pleasing result :)

Does it correct barrel and pincushion distortion as well, or is it only good for linear distortion currently?

It's currently linear only, and I've noticed some slight remaining distortion in my test footage. I may update it sometime, but for now it does what I need :)

David

vatz32
18th July 2007, 23:50
Dear David,

As an AVIsynth noob, I'm having trouble getting the correct script syntax to work with the Defish plugin. I managed to copy the cpp and dll files to my AVIsynth/Plugins folder, but then came a cropper ;-)

Please could you post a sample Defish script just to get me started.

I look forward to trying what promises to be a very useful tool.

Thanks, John.

wonkey_monkey
19th July 2007, 00:09
At its simplest, you'll need:

converttorgb32
defish(fov=120)

The three other parameters have defaults which are explained in the first post.

David

vatz32
23rd August 2007, 19:32
Here's the avs script that worked for me:
Clip=DirectShowSource("E:\test-1.avi", fps=25)
ConvertToRGB32(Clip, interlaced=true)
defish(fov=77, resample=2)

I am mightily impressed with the output - the bilinear interpolation (resample=2) looks a lot better on my interlaced footage. Everything else works beautifully straight out-of-the-box.

For calculating the Horizontal FoV of a given (rectilinear) lens on a given camera, I used "Horizontal FoV = 2 (arctan (d/2f))"

where d = horizontal width (mm) of the CCD
and f = focal length (mm) of the lens

My test lens was a 3.8mm Aspherical on a 4:3 1/2" CCD.
(The specification claimed diagonal FoV as 108°, whereas the calculation suggested 96°. I guess the difference of 12% is the amount of barrel distortion ?)
The formula suggested a HFoV of 77°, which worked better than my guestimates as the Defish filter parameter.

One thing I especially like about this Defish is that it makes a very clean stretch&crop into the bargain - removing vignetting or aberration artefacts from the corners and (apparently) any black borders ... by doing all this in one resample / interpolation, it saves time and definition loss.

I take my hat off to you David :)

scharfis_brain
23rd August 2007, 19:38
if you are filtering interlaced contents you need to bob-deinterlace it and afterwards reinterlace it corretly.

if you don't do this, you'll get messed up fields.

wonkey_monkey
24th August 2007, 11:36
Glad you like the plugin Vatz. There will eventually be a spline16 interpolator too, which will look better than bilinear.

The specification claimed diagonal FoV as 108°, whereas the calculation suggested 96°

The difference is that the calculation is for horizontal FoV (which Defish uses), which will always be less than diagonal FoV.

David