Sunday, December 22, 2013

Easier focus stacking with Magic Lantern


Magic Lantern is really cool software: it allows you to do all kinds of awesome things with Canon EOS cameras. On Thursday I bought a 600D for Surice and myself, and before Friday I had installed Magic Lantern.

I was particularly interested in focus stacking because I sometimes like to take pictures of very small things. The basic principle is that you first make sure all the pictures are aligned, for example using a tool like align_image_stack and then stack them on top of each other, with the out-of-focus parts of each picture erased. This gives you a composite picture that's in focus all over. Of course, erasing the blurry bits by hand would take forever, so you use a tool like Enfuse to do that.

I've tested it, and it works relatively well. For my test picture, I used three Matryoshka dolls that I got from a Russian colleague.

Here's the standard "depth of field" Matryoshka Doll shot:


That was actually the middle picture from a set of 12, automatically taken one after the other by the camera, due to the magic of Magic Lantern. Normally, I'd look online (like on eduperez's website) for the commands to align the images, but when downloading the files off my flash disk, I saw some .sh files… files containing commands like this:

#!/usr/bin/env bash

# FST_0186.JPG from IMG_0186.JPG ... IMG_0197.JPG with aligning first

align_image_stack -m -a FST_AIS_0186 IMG_0186.JPG IMG_0187.JPG IMG_0188.JPG IMG_0189.JPG IMG_0190.JPG IMG_0191.JPG IMG_0192.JPG IMG_0193.JPG IMG_0194.JPG IMG_0195.JPG IMG_0196.JPG IMG_0197.JPG
enfuse "$@" --contrast-window-size=9 --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --output=FST_0186.JPG FST_AIS_0186*
rm FST_AIS_0186*


"What does that script do?" you ask: well, because Magic Lantern is written by Free Software geniuses, they gave a nice little GNU bash script as a bonus to their Free Software buddies. I just run that script, and got this:
Quite nice really: all three dolls are now in focus. However, if you look closely, you'll see some fringing or halo effect from the focus stack:

This comes from the algorithm that's erasing the out-of-focus areas not quite working perfectly. I found that someone had solved a similar issue by adding the parameters --gray-projector=l-star --contrast-edge-scale=0.3 to the command line - now while writing this up, I stumbled on Patrick David's blog - he explains how it works, and also introduces the GUI for Enfuse. Enfuse is pretty sophisticated software with lots of options: I'll have to RTFM to see what I can find out to make it work better.

Of course, in this case I could have achieved this effect by just selecting a very small aperture and a longer shutter opening. That's not always possible, though: sometimes you can't get everything in focus just by stopping down your aperture, and due to diffraction the resolution of a lens also gets worse if you stop it down too far, so focus stacking is still useful in these situations.

Tuesday, September 10, 2013

A cheap lightbox for photographing small objects without shadows

Last week, I decided to make a lightbox for the lab. Not the kind where you have lighting from below to look at negatives or slides, but the kind which can light an object from all sides, so that you can get clear photographs.

To do this, I bought a white LED strip and a transformer from the local hardware shop for R400. The LED strip is backed with adhesive, and all the LEDs are connected in parallel, in groups of 3.
The reel with the backing strip covering the adhesive.
The leftover lighting strip still on the reel

The LEDs are connected in parallel in groups of 3
This means that you can cut the strip at any point between two groups of three LEDs, and it will still work. Also, if any one LED fails, the rest of the strip still works.  This is what a working unit of three LEDs looks like:

Unlit, on my laptop keyboard for scale

All lit up!
This made the construction of the lightbox trivially easy: Cut a small hole in the side of a plastic box; feed the cable through the hole; stick the lighting strip in a spiral around the inside of the box; connect the contacts to the transformer, and cut a large hole in the base of the box for the camera lens to poke through (OK, that was a bit of a cosmetic failure, because the plastic shattered while I was cutting it. Clumsy!)

Here is the completed lightbox: you'll see that I put some paper inside it, so that there would be no harsh reflections from the LEDs.


Here's a picture of some shiny candy taken with and without the paper diffuser:


The effect with all those little lights is quite pretty, but it can be distracting.  Also, the complete absence of shadows is a bit strange.  I'll have to experiment with casting intentional shadows (e.g. a black piece of paper at one side of the lightbox?) and with different backgrounds (a 1 cm grid for size reference? a solid black background?)

The light is pretty close to a neutral white: I didn't actually measure the spectrum, but because it's not identical to daylight or fluorescent light, I used a white sheet of paper to set my camera's white balance before taking pictures.

Finally, here's a test picture I took of the previous Chocolate Eclairs packaging (because I had one lying around):
If you look closely, you'll see that the background that I used was a piece of paper with printing on the reverse side: lazy, I know!  Still, I got a well-lit picture with light coming in from all sides. Proof of concept done!

Saturday, July 6, 2013

Smoking Booze

In the ultimate example of misguided health-consciousness, Skippy62able on YouTube decided he needed a way to get hammered without getting fat. His genius idea? Inhaling!

What he does is a boozy version of a classic school-level science experiment: pump air into a bottle with a bit of water in it, swirl so that the air gets saturated with water vapour, and suddenly release the pressure. The resultant drop in temperature makes water vapour condense to make fog. He does this with alcoholic beverages, and then inhales the vapour. He claims to get very drunk in the process.

Now, there is some validity to the concept of huffing alcohol to get drunk. As I've just pointed out in Wikipedia, your liver gets a chance to remove some of the alcohol that you drink before it gets a chance to addle your brain. If you breathe it in, however, it gets absorbed in your lungs, goes back to your heart, and then to your body and brain via the aorta and carotid artery, respectively. Theoretically, you'll get drunker faster off the same amount of alcohol if you inhale it.

Of course, you won't dodge all the calories. According to this article, alcohol contributes a very significant part of the energy of most alcoholic beverages. For example, most of the energy from a beer comes from the alcohol, even though it still contains some carbs. Even though you might sweat and pee out some more of the alcohol because it bypasses the liver the first time around, you'll still metabolise most of it.

The actual demonstration is completely fake, for a number of reasons.

First, although the pressure release cause some dramatic vapour, all the air that escapes the bottle is booze that you lose. If you really wanted to get all that ethanolic goodness, you'd want to inhale the escaping air.

Second, he only takes one hit from each drink, leaving most of the alcohol still sloshing around in the liquid phase!.  Even if he were to inhale all the vapour from the bottle, he really wouldn't get much out of it. Let's see what the maximum would be. A very generous assumption is that the vapour is in equilibrium with the liquid when he inhales it, so the amount of alcohol depends on the concentration in the liquid phase.  Let's see:
  • Beer - about 5%
  • Pre-blended alcohol/energy drink  - up to 12%
  • Champagne - maybe 15%
  • Vodka and tonic (about 1:2, guessing at flow rates) - about 40/3 = 13%
  • Whiskey and cola (about 1 to 1) - about 20%
  • Branded RTD beverage - about 5%
  • Box Merlot - about 13%
  • Branded German herbal liqueur and energy drink (about 1:3) - about 35/4 = almost 9%
calculated the amount of ethanol in the vapour phase, using vapour-liquid equilibrium from Chemsof.  By my calculations, each time he gets only about 5% of a gram, assuming he can breathe in about 1.5 L each time, and it's all absorbed by the lungs.  In the end, he gets less than a tenth of the alcohol that he would have gotten from one shot of that Vodka.

So how do you explain his "saturated" breathalyser?  Very simple: he either drank alcohol during the camera breaks, or else he held some alcohol in his mouth while breathing over it.

Now what would one do if one wanted to get high off alcohol without drinking it?  My immediate idea was something like a hookah.  If you could bubble air through booze for long enough, you'd extract most of the ethanol and deliver it to your lungs. Apparently this is not new. Alcohol without liquid sells something similar, and makes some unsurprising claims.  Apparently it takes around 20 minutes of inhaling to get sloshed.