Correcting Lens Aberrations in Digital Photography?
Kavau asks: "I've recently entered the world of digital photography, and bought a pocket-sized digital. While the resolution and the color accuracy are very nice, I was utterly disappointed by the (lack of) quality of the zoom lens: It has a clearly visible barrel distortion, especially in wide angle, so that straight lines appear curved in the picture. This is especially annoying in architectural shots or cityscapes. While grumbling about this shortcoming, I suddenly realized that I am dealing with digital imaging here: In principle it should be no problem to measure the distortion, and then to correct it with a digital mapping. Other lens faults such as vignetting could also be corrected. Now, since I don't want to reinvent the wheel, here is my question to Slashdot: Do you know of any open-source tools that deal with issues such as (1) applying general analytic transformations to a picture, (2) specifically correcting for barrel distortion, or (3) determining the amount of distortion from test pictures? Also, since people probably had this idea before, does anyone have experience with this issue that he/she wants to share, or some resources to point out?"
1. Grip each eyeball firmly between your middle fingers and thumbs.
2. With your forefingers at 9:00 (left) and 3:00 (right), press firmly against each eye. Distortion should now be minimized.
3. Should blindness occur, trim fingernails and repeat with alternate eyeballs.
Best Windows Freeware
...that your best bet would be to take a picture of a piece of graph paper, and then figure out exactly how to stretch it so that the lines are straight again. That said, I'm not entirely certain if there are any programs out there that would do this, but I think it'd be reasonably easy to write an OpenGL program that allows you to apply arbitrary distortions to an image, and then saves those distortions to apply to other images.
I'm just curious why there's an AMD logo for this edition of ask /.
Hosting: as low as $5.95/Mo
Check out PanoTools at http://www.path.unimelb.edu.au/~dersch/
It has a steep learning curve, but seems to be worth the effort. I've only played around with it some myself but I've seen lots of (seemingly) professional photographers on the web that use it.
Exellent tutorials: Big Ben's Panorama Tutorials
Search string: photo barrel distortionm
4'th link down:
www.philohome.com/barrelpers/barrelpers.ht
It mentions to use the "panorama tools" package, which is open source. However, from what I remember, someone sued the author for patent infringement or something, so he no longer has it on his web site www.fh-furtwangen.de/~dersch/, but I found it on www.path.unimelb.edu.au/~dersch/
I know, I know, not freeware. Yet everyone seems to have a copy.
a creative pro article about correcting barrel distortion, and one at Fred Miranda.
The ______ Agenda
This has been a problem in traditional photography for some time. Distortion for wide angle shots always occurs for any wide angle shot -- try a 28mm lense sometime and see for yourself.
Special lenses do exist to correct for this. However, the are very expensive. Generally, only architecture photographers and other professionals have them.
You'll probably want a better lens anyway for doing any sort of quality photography. Oh, and as for fixing it with a filter, good luck. Its not as easy as just applying a transform as the person with the graph paper suggests. The warping depends on distance from the lens.
ph34r teh p0w3r 0f th3 c0w
Not the most computationally efficient method but I wonder if you could do this with povray.
Use the picture as an image map on a sphere or cylinder. Keep adjusting the frame and object size until the lines look straight. Make sure the object is self illuminating and you don't use external light sources...
Hmmmm, seems like it might work....
One should not theorize before one has data. -Sherlock Holmes-
Woodle, Alan S., Scott Elliott: The Vector system for dynamic gait analysis. Clinics in podiatric medicine and surgery. 10(3)485-500, 1993.
There are referances to the math in that paper. Good key words to google might be DLT & photogrametry (sp?)
VIPS is a wonderful image processing library with all sorts of "evil stuff", NIP is a GTK interface to the library which gives you a kind of spreadsheet for images. I'm telling the author about this slashdot item so he can come and pimp it here himself, but in the mean time take a look.
I have personally used it for analysis of medical images, it's nice and versatile, although the built in scripting language is a bit sick (imo).
-Yarn - Rio Karma: Excellent
http://www.humansoftware.com/pages1200/PhotoFixle
Photoshop plugin for exactly this issue. Dont' know how well it works but it may be just waht you're looking for... Mac and Windows.
A fool throws a stone into a well and a thousand sages can not remove it.
It worked for me a couple years ago with cheap USB cameras. I haven't used it recently, so I want to know if there are better choices now. I've got a new robot platform with a lot more CPU power and USB bandwidth, so I'm going to try working on the vision thing again.
Back then, I started with the instructions at Program Your Computer to See. Note how the URLs inside the article are mostly out of date.
You may be familiar with the concept of a picture plane that is used in perspective drawings. The idea is that a transparent sheet is placed between the object and the eye, and rays are mapped onto the surface. That works fine for narrow fields of vision, but it falls apart as the lens gets wider.
Imagine an extreme case: You are standing in a train station. To your left the tracks disappear to a point on the horizon, straight ahead of you the train sits, and to your right the tracks disappear off to another point. If this image is cut up into small enought chunks, you won't notice the abberations of traditional perspective. You have a one point perspective of the train, and a two point perspective on each side. But try to stitch the images into a whole, which is similar to what you are trying to do with a wide angle lens, and all those straight lines are going to have to bend somewhere!
A more accurate picture plane, one that could actually capture a real image without any distortion, would be a sphere, with the eye at the center. Straight lines now map to great circles on the sphere, and whole images, just like the ones your eye sees, can be presented in their entirety.
But then you can't exactly paste them into your photo album. So you are back to the question of, how do you map a sphere onto a plane?
Or maybe you could get some funny looking virtual reality googles.
The advantage of a pocket-sized camera is that it fits in a shirt pocket. Nuff said.
You're right about the patent thing. Ipix aparently has a patent that they are using on Quicktime QTVR and panotools. more info warning - these links seem a bit old; I wonder what the progress has been. These are pretty simple algorithms (not trivial to implement, though) and are exactly the type of patents I hate... argh...
HIV Crosses Species Barrier... into Muppets
The advantage of a pocket-sized camera is that it fits in a shirt pocket. Nuff said.
Key thing is, buy cameras according to the optical reputation of the brand, not the electronics/digital reputation. 99% of the time things will work out better that way.
How would we have worked it out without you!
There are places where the networks are not touching,and there are places where they are-Boeing's Lori Gunter
& if it's big enough you might need a pocket map !
Don't forget a pocket torch in case you fall inside it!
You could see how deep it is by timing your fall with the in-built pocket watch!
I'm so funny !
There are places where the networks are not touching,and there are places where they are-Boeing's Lori Gunter
Not always true. Reputable companies sometimes throw cheap stuff at you, too. The camera in question here is a Pentax Optio S, and Pentax certainly has a good reputation in optics (maybe not as good as Nikon, but certainly good enough). The problem with the Optio S is its "revolutionary sliding lens design", which makes the camera as thin as a deck of cards. However, as in world politics, revolutions often introduce a whole new set of problems...
What if digital camera makers were to incorporate software like you mention into their cameras. For one, they could use much cheaper lenses and correct the image as it's being saved. They could also give the photographer the option of choosing between a number of predefined virtual lenses to use when shooting the photograph.
http://www.frc.ri.cmu.edu/~hpm/project.archive/rob ot.papers/2000/ARPA.MARS.reports.00/Report.0001.ht ml
This describes a piece of software called FLATFISH that can correct the type of distortion you're seeing.
I'll warn you, it can be a bit tempermental until you get used to working with it, and it's not meant for the faint of heart. But I've personally used it for camera calibration on a robot using a webcam for vision, and I can say that it does work pretty well. It will very likely give you the technical, correct solution, assuming that your lens is radially symmetric. Sadly, unless you spent a lot of money on your camera lens, it's probably not symmetric. But it'll still do a decent job.
Best stuff is here: http://www-2.cs.cmu.edu/afs/cs.cmu.edu/user/rgw/ww w/TsaiCode.html
I have a couple of examples, both involve correcting perspective at the same time:
A single photo, corrected
Two photos stitched, corrected and perspective adjusted
There is a project to build an easy-to-use front-end for panorama tools: Hugin, it has a Mailing-list, anyone welcome.
If you just want to batch process individual photos without having to learn Panorama Tools, try this perl-script, it implements everything required to correct barrel distortion (though you have to calibrate your camera first).
Get a copy of octave (because I assume you aren't going to drop cash on Matlab)
.m files to be found via google).
Find a way to get the images from camera format into octave (its not exactly 1-2-3, but there's
Use techniques from above post's links and the powerful matrix manipulation features of octave to do the dirty work.
View the results....
If you get a procedure that works eventually, you can script the whole thing.
The most annoying part is you might have to open up the picture in Gimp and "hand paint" the depth information into another channel to give clues to the DLT algorithm. Either that or take two shots of each picture (stereo).
#1: you care about this stuff with a pocket-sized digital? if you want good pictures you need to get a good NON-ZOOM lens, like you can get for an SLR. Only use zoom lenses if you are taking pictures someplace where you can't walk closer/further from the subject (edge of a cliff?).
If you are stuck with a cheapo zoom lens, find the zoom setting with the best picture (straightest lines, least distortion) and ONLY use that setting (unless you're at the cliff).
Personally I use macro lenses for everything, macro lenses are just fine for regular work, they are usually higher quality, and they focus in a flat plane instead of equidistant points. Drawbacks, they are heavier, slower f stop, slower to focus, and more expensive. But I do a lot of copy work and can't justify paying for another 50mm lense when I already have a perfectly good 50mm macro, etc.
#2: barrel distortion (curved lines): even the best lens will have this, you can't avoid it. There are several photoshop plugins available to fix it, including the one from Human Software mentioned above which you use by basically marking some points that should be straight, and it does the rest. It also has a database of lenses. Again you will have better predictable results if you avoid zoom lenses.
#3: perspective distortion ("keystoning"): if you have this (lines are straight but not parallel), you can use "perspective crop" in photoshop. Learn about it. The Gimp has a similar feature I've used.
#4: other distortion: don't bother trying to fix other distortion like misfocus or abberation. information has been lost in your image. Just get a better lens.
I'd like to see some open source tools for this purpose. I've thought about writing a GPL PS plugin to measure and correct barrel distortion and keystoning but adobe's license terms don't allow GPL (or any similar license, I couldn't believe it), and the SDK wasn't available for zero-cost last I checked so I gave up on it. Would be nice though.
The math is straightforward but the trick is to interpolate/resample appropriately so that your pictures don't become all jaggy. That takes some cleverness or you chew up lots of CPU time doing it the direct way.
Have you checked Tabaware? The author of Pixelzap and others, offers a ton of info on post-processing digital images to do neat tricks, including perspective correction for panorama assembly, etc.
I know digital pornography inflicts a lot of eye strain, but I never thought that there are specific correcting lens that deal with this sort or eyes strain... oh wait..
FYI, Pentax is/was a pioneer in multicoated optics. (For superior anti-reflection properties.)
I believe that no other manufacturere can approach Pentax's MC technology w/o having to pay Pentax royalties.
Nikon may be ahead of Pentax in other optical areas, but Pentax is the leader in coatings, or at least used to be.
But I agree, even the reputable companies can often give you cheap crap.
retrorocket.o not found, launch anyway?
Take Nikon, for example. All their sub-$1000 digital cameras have crappy f/2.8 zoom lenses. Even Sony offer better glass than that.
GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
Dave Dyer wrote a photoshop plugin called LensDoc for this purpose for Andromeda Software. I haven't personally used it, but the sample results look pretty nice, and the guy definitely knows what he's doing. Check out some of his other stuff as well.
In the great CONS chain of life, you can either be the CAR or be in the CDR.
Almost...spherical distortion is a long standing problem, and it DOES depend on the subject's distance from the lens (this is why spherical abberation is generally fixed in microscope lenses). If you take a close up picture (of a face for example) with a lens with bad distortion at close distances (like a wide angle lens), objects that are just a little closer to the lens than others in the frame (like the tip of the person's nose) will seem a great deal closer as a result of the distortion (creating a pinnochio effect) The distance from the lens, the depth-of-field and the focal length all play a role in the distortion.
My GF had this problem measuring the length of feather barbules in her biology thesis - we could find no (Free) software that easily corrected for spherical distortion, and did not have the means to try the more expensive image analysis software. But simply taking a picture of graph paper and correcting for the distortion you see will _NOT_ work as the depth of field and distance from the lens change - therefore it would be somewhat useless to correct for graph paper at ~1/2 m from the lens, and then take pictures of buldings ~500 m away. That said, the effect should be diminished at greater distances, so you really should not have a problem with architectural pictures. If you really want everything to be accurate, you will either have to get a SLR digital camera (with interchangable lenses) and spend some cash on a nice lens (note that most auto-zoom lenses are not that high quality - this is why a lot of pro photagraphers carry like 10 lenses or several different cameras all the time); or you could try to write it yourself...but I don't think you'll find the time/price tradeoff of either option worth the improvement for your particular application.
Extended Depth Imaging is definately whats called for here. Basically, you stick a filter on the front of the lens that is comprised of mostly transparent grid lines. Then you right software that searches for the grid lines in the image, and maps the source image using the distorted grid lines *contained* within each picture. High-end systems generally have a piece of hardware stuck on the camera that
:P
does it for you prior to dumping to the storage unit. This technique is used extensively in robotics to "extend the plane of depth" e.g. if you are using a stereoscopic matching algorithm to reconstruct 3D structures from stereopictures, you can SIGNIFICANTLY improve the 3d reconstruction by sticking aforementioned filter on the front of a lense, and digitally preprocessing to remove distortions introduced by lens aberrations. It shouldn't be too difficult to code up a stupid simple algorithm to sample a picture according to detect said distorted grid in each picture, and then remap the distorted picture into a corrected image.
BTW - if you are serious about digital photography, another technique thats been pioneered recently is using a range finder (associated with the CCD) to store the z depths of each pixel in the image; the benefit being that if you are attempting to do any collage work, you can mask out area of the image based on depth (ergo saying: "Please provide me with all pixels 6 feet or less away from the camera position")... I'm currently researching techniques (- film deposition) for correcting for chromatic lens aberrations, so if anyone knows a good way, let me know