27 September, 2007

Lens Distortion Correction for Apple Motion

I've spent the last couple of days working on a Core Image, Image Unit that can correct for radial lens distortion. If you have ever used Apple Shake, then you will know that it had a node that could correct lens distortion on a video source. This feature is missing in Apple's Motion 3, so I thought that if I wrote an Image Unit to do the correction, then it could be used in any program that supported Core Image, including but not limited to Motion, and Final Cut Pro.

I just downloaded and digested a paper I found on the subject of lens distortion correction, it's really quite a good read (see:Automatic Correction of Lens Distortion by Using Digital Image Processing). After that I followed Apple's reasonably well written tutorials on the subject of creating a Core Image plugin: I have to say that the devil is in the details, because it was really hard to find any documentation on non-executable (GPU-only) plugins, for reasons beyond the scope of my knowledge, these simply don't work inside Motion. They work in Core Image Fun House, and Quartz Compositor, but not inside Motion. This got me frustrated enough to think about reprogramming my graphics card with my fists. So in case your wondering, GPU-only Image Units don't work inside Motion 3. (like you care!) I did get it all working in the end, but had to program some Core Image glue code to make all the little micro-chips happy!

Well without further ado, the results:



There is some great software out there, for doing lens distortion correction on still images, if that's what you need I would try Lens-Fix CI (I assume it also uses Core Image). Lens-Fix CI comes with a database full of correction parameters for different optics at different focal lengths. My plugin is more designed with movies in mind, and doesn't come with a sweet user interface.

To correct your images, you'll need to know the calibration polynomial coefficients of your lens — If you have no idea what this means, then twiddle the sliders back and forth until the picture looks right. If you want a mathematically accurate correction, you will need to calibrate your lenses with a grid or something, and probably some software. Google it. Seriously.

You can download the Image Unit (Universal Binary) and Source here: UMLensDistortionCorrection.zip

To install simply copy “UMLensDistortionCorrection.plugin” to “/Library/Graphics/Image Units/” and away you go, it should appear in the category “Distortion Effects”.

1 comment:

FiveElementProductions said...

I can't get the plugin to appear. I have copied it to the folder suggested but nothing seems to appear in the Distortion Effects menu.