Wednesday, February 13, 2008

X-Ray damage fix


An issue with shooting movies on film is that you don't see what you get until some time in the future. You have to take a lot of things on faith, that if all the machines work right, and the professionals working on the project do their jobs correctly, you will end up with the images you want on film when it gets back from the lab. A significant industry and methodology of working has been built up to make sure that this happens as well as it possibly can, and in large part it does the job. This is a real credit to people at Kodak, Arri, Panavision, the labs, and so on.

But sometimes things fail to work, and then it gets interesting.

I got a sequence of film recently that ended up having very bad X-Ray damage. It looked very much like this image from a Kodak publication I'm not using the images from the actual film for the obvious reasons.

Now, what you saw on this film was this blue band that moves from left to right periodically, this is how the X-rays expose this particular kind of film. The sinusoidal motion of the band is related to the way the film was exposed -- the X-rays slice through the wound roll of film in the can. A slice diagonally through a roll of film obviously makes a sinusoidal moving image.  [As an aside, it was clear that the period of the sine wave decreased over time, allowing us to determine that the X-ray damage happened before the film was exposed, rather than on the way back to the lab...]

If one could calculate the amount of damage at a particular point on the frame, then perhaps one could subtract that exposure and you would end up with a clean image. Now, it turns out that with this frame from Kodak, you could do this, as the sky is really absolutely black. If you assume that the beam doesn't change its character too much from the top to the bottom of the frame (this is empirically true) then you could just project the exposure generated from the top scanlines down to the bottom of the frame. You could even go further, and look at the top (black) part of the next frame, and make an X-ray exposure image that interpolates from the top of the frame to the bottom.

It turns out that this works pretty well. A significant issue, though, is that the X-ray damage is very noisy. When you try to subtract the blurry X-ray exposure interpolated image from the original damaged scan, you will find that many of the pixels would be less than zero. Clearly you would clip those to zero -- but then you wouldn't be subtracting enough exposure from that part of the film. The trick that works, then, is to diffuse that error (the amount that you couldn't subtract, because the value was less than zero) to neighboring brighter pixels.

I did this error propagation two ways. First, I took all the pixels that had remaining energy, and put them in a random order. Taking each pixel in turn, I would spiral away from that pixel looking for other pixels to subtract energy from.

The second way is that for every pixel with error, I divide the error by four, and add it to the adjacent pixels. This will slowly diffuse the error out. Doing this for multiple passes will allow us to finally find propagate all the error. It turns out that for large dark areas of the screen I needed many passes (on the order of 90) to get reasonable results. This is the technique I ended up using.

Great. Now you have something that works perfectly, as long as the top edge of your frame is black. It turns out that in reality (and in this particular movie I was working on) this is very seldom the case. Cinematographers want to take pictures of actual things, not black skies.

So...here's the tricky part. How can you find some black scanlines in your image. I urge you to think about this for a minute, because there are always some black scanlines, scanlines guaranteed to be only exposed by the X-ray, not by light. Where might one find these black scanlines?

Well, you say it that way and the question sort of answers itself. Film cameras expose frames one at a time, and film scanners scan those frames -- but there is almost always a black band between the frames! So, you just have to cajole your friendly film scanning service to load up the negative wrong, that is, so that rather than scanning frames, in the center of the scanned area you get the black band between the frames.

Anyway, our friendly neighborhood scanning service did this, (while rolling their eyes, wondering at the crazy things digital people ask them to do), and we got beautiful interstitial scans, analyzed them for the X-ray exposure, modeled the period, frequency, and phase of the sine wave to correctly smear the interstitial scanline across the frame, subtracted it out (in linear space, of course,) and it worked...pretty well. It got us about 95% of the way there.

A good friend of mine, Bill Taylor (ASC, and president of the Visual Effects Branch of the Academy) notes that this is one more example of the integrity of the analog, uncompressed, redundant nature of film. I think he has a very good point.