Correcting The Pink Photo From My Modified Phone: A Follow-Up

A week or so ago I posted an Ask Hackaday, in which I asked the community for help with a common camera hacking problem I was having with my mobile phone to C mount conversion. In short, when you take a camera with a tiny lens and change that lens for another, the resulting photographs have a colour cast. It’s usually the right colour in the centre, and moves to a pink colour in a ring around the photo. The commenters came up with the right answer, and identified the problem as the phone software applying a correction to compensate for the relatively awful tiny lens. It’s a problem not limited to me, it’s appeared in many such projects including this one. So having been educated as to the nature of the problem, my next  step was to look at how to fix it.

It’s A Flat Field Correction Problem, Folks!

This image has pink edges fading to white in the centre.
The Galaxy Note 3 calibration image.

Of course I am not the first to encounter this in some form, thus as expected there’s a well-tested technique in photography to correct it. It’s called flat field correction, and it works by correcting each pixel in the image to match the gain to that of the pixels in the centre of the image. It’s a straightforward enough mathematical procedure, and to measure those relative pixel gains it requires that the camera take a calibration image of a uniformly lit white surface. The idea is that each pixel in this image will deliver the difference between it and the pixels in the centre, and thus the required correction for that pixel.

Taking a calibration image is easy, and involves pointing the camera at something white and reasonably lit, and unfocusing it to remove any surface texture effects. The picture must be at the same resolution as the photo to be processed. I took a picture of the same white table in my hackerspace from the stuffed animals picture at somewhere under a metre’s distance, with the lens set to infinity focus. The resulting image shows the circular pink pattern alone, and very clearly indicates the nature of the problem.

Correction Method 1: GIMP

The same image of stuffed animals twice, top: fixed version, bottom: original with pink cast.
Top: fixed, Bottom: original.

Perhaps the easiest way to perform flat field correction is in your normal photo editing package. I use GIMP, but I am guessing that other packages will follow a similar path.

  1. Open your photo to be corrected. The result will be brighter so you’ll need to reduce its exposure (Colours menu > Exposure).
  2. Change the encoding precision to 32-bit floating point (Image menu > Encoding). Since this is a mathematical process, this reduces any rounding errors that might affect picture quality.
  3. Open the calibration image as a new layer above the photo (File menu > Open as layers). Some guides then suggest applying a Gaussian blur to this layer to further reduce noise. I tried either, and found it made little difference.
  4. Change the layer’s mode from “Normal”, to “Divide”. This can be found in the Layers panel, in a pulldown above the list of layers.

Following these steps delivers an image with the flat field correction applied. Of course, it may still need other processing such as white balance, but the pink ring is gone.

But What About The Command Line?

A straightforward GIMP workflow is an easy way to correct a single photo, but converting a day’s worth of pictures is likely to become a major pain. For a directory full of pictures it makes sense to use Image Magick if I can, and for a video I need to be able to use ffmpeg. So after a bit of messing about, here is the same process, for both command line packages. I have also placed the shell commands in a GitHub repository, in case your browser manages to mangle cut-and-pasting the commands below.

Image Magick

The first task is to create a calibration map from the calibration image I took earlier. This is a PNG image whose pixels represent the level difference from the centre of the image normalised to 50% grey, as opposed to the original image which shows their relative values at whatever illumination it was taken under. The -blur option applies a blur to remove any textures in the image, while the -fx option contains the mathematical formula required to apply the calculation. I am not ashamed to admit that I had a lot of help in getting these steps right, for which I am thankful.

magick calibration-photo.jpg \
  -blur 0x60 \
  -fx 'channel(
    0.5*u.r/max(u.g,0.02) *
        u.p{w/2,h/2}.g/max(u.p{w/2,h/2}.r,0.02),
    0.5,
    0.5*u.b/max(u.g,0.02) *
        u.p{w/2,h/2}.g/max(u.p{w/2,h/2}.b,0.02)
  )' \
  -depth 16 PNG48:calibration-map.png

Having created the calibration map image, it can now be applied to the photo. The multiplication by 0.5 is a compensation for the calibration map being centred around 50% grey, and the divide function is the same as that in the GIMP version above.

magick photo.jpg \
-evaluate multiply 0.5 \
calibration-map.png \
-compose DivideSrc -composite \
-quality 95 photo-corrected.jpg

ffmpeg

Now for video! Performing the same task in ffmpeg follows the same process as  with Image Magick, but the calibration map must be the same resolution as the video. The most straightforward way to do this is to make a short video of the illuminated white surface rather than a still photo, and average all of its frames into a single image. The result is a similar one to the still calibration image, but it’s important to do it this way as the video camera app may apply different processing to the still camera one.

ffmpeg -i calibration-video.mp4 \
-vf "tmix=frames=60,format=rgb48be" \
-frames:v 1 -update 1 calibration-image-video.png 

This image can then be processed in exactly the same way as the Image Magick version. Again, it’s applying a blur, and the same formula to make the calibration map.

ffmpeg -i calibration-image-video.png \
-vf "format=gbrp16le,gblur=sigma=60,\
geq=r='32768*r(X,Y)*g(W/2,H/2)/(max(g(X,Y),1)*max(r(W/2,H/2),1))':\
g='32768':\
b='32768*b(X,Y)*g(W/2,H/2)/(max(g(X,Y),1)*max(b(W/2,H/2),1))'" \
-frames:v 1 -update 1 -pix_fmt rgb48be calibration-map-video.png

Using the map, the conversion can then be performed on the video itself. This is a slow process though, on my run-of-the-mill Thinkpad it was processing only 1.8 frames per second.

ffmpeg -i your-video.mp4 -loop 1 -i calibration-map-video.png \
-filter_complex \
"[0:v]format=gbrp16le[video];\
[1:v]format=gbrp16le[map];\
[video][map]blend=all_expr='A*32768/max(B,1)':shortest=1,\
format=yuv420p[out]" \
-map '[out]' -map '0:a?' \
-c:v libx264 -preset medium -crf 18 \
-c:a copy your-video-corrected.mp4

So we’ve identified the problem as flat-field correction, and here are three ways to do it. The camera is now a lot closer to being useful, even if the processing steps can be a little cumbersome and the photos may require a little more post-processing. It’s obvious that in the phone hardware is the best place to do this, but without access to the internals of a 13-year-old Samsung this is the next best thing.

Mix Your Own ECN-2 Development Chemicals

After the digital camera rose to prominence, it became a cool hobby to keep taking photos on film. It was even cooler if you did the same with an old motion picture camera. The retro film revival has kept a dedicated bunch of photo labs in business over the years, but it’s still possible to save some cash on development by doing it yourself. If that’s your game, you might try mixing up your own development chemicals. 

As explained by [No Grain No Gain], it’s quite possible to mix up your own ECN-2 chemistry from scratch if you know what you’re doing. ECN-2 is the chemistry you’ll want if you’re trying to develop any of Kodak’s Vision3 films, along with CineStill films.

The problem with traditional methods of making developer is that once it’s mixed up, it doesn’t keep well, and the more you use it, the worse the quality gets. To beat this problem, this method involves producing two stock solutions which can be kept on the shelf for long periods of time. They can then be combined together with a little CD-3 developer on an as-needed basis. This makes it easy to always have fresh developer on hand for the best possible results on every roll processed. To make everything, you’ll need sodium sulfite, potassium bromide, sodium carbonate, sodium bicarbonate, and the specialist CD-3 developing agent. It’s then a simple job to mix up the dry chemicals with a bunch of distilled water to make the two necessary solutions to keep on hand. The video also explains how to deal with RemJet films if you happen to be shooting those.

[No Grain No Gain] estimates that this method can cut the cost of development to as little as 50 cents a roll. There’s plenty of labor involved, but if you want the freshest, best developer on hand for your home lab, it’s a method worth considering.

We’ve explored modern film development techniques before, too.

Continue reading “Mix Your Own ECN-2 Development Chemicals” →

A man's hand is shown holding a color photograph of a vase of flowers against a black background.

True-Spectrum Photography With Structural Color

Although modern cameras can, with skill and good conditions, produce photographs nearly indistinguishable from the original scene, this fidelity relies on the limitations of human vision. According to the trichromatic theory, humans perceive light as a mixture of three colors, which can be recorded and represented by cameras, displays, and color printing; a spectrometer, however, can detect a clear distance between the three colors present in a photograph and the wide range of spectra in the original scene. By contrast, one of the earliest color photography methods, Lippmann plates, captured not just true color, but true spectra.

A Lippmann plate, as [Jon Hilty] details, starts with a layer of photographic gel containing extremely fine silver halide crystals over the back of a glass plate. This layer is placed on top of a mirror, traditionally a mercury bath, and put in the camera. When light passes through the emulsion and reflects off the mirror, it interferes with incoming light to create a standing wave. The portions of the emulsion at the wave’s antinodes absorb the most energy, converting local silver halide crystals into reflective silver. The spacing of the silver particles depends on the incoming light’s wavelength, and is fixed in place during the development process.

This creates a matrix of vertically-stacked diffraction gratings, each diffracting back the original wavelength when illuminated with white light. Unlike normal diffraction gratings, the wavelength of diffracted light doesn’t depend strongly on the viewing angle; since the interference structure here is vertically-arranged, it refracts a narrow range of wavelengths across all possible viewing angles. The viewing angles, however, are limited; unlike with dye-based photographs, you can only view the colors nearly straight-on. This, along with the necessity for long exposures, the chance of producing washed-out colors, and the impossibility of creating reprints, kept Lippmann plates from ever really catching on. The basic concept lives on in holograms, which encode spatial information in a similar kind of photographically-formed diffraction pattern.

For a more conventional method of color photography, we’ve also seen a recreation of the autochrome method. Alternatively, check out this homemade silver halide photography emulsion.

Thanks to [Stephen Walters] for the tip!

A Computer That Fits Inside A Camera Lens

For a long while, digital single-lens reflex (DSLR) cameras were the king of the castle for professional and amateur photography. They brought large sensors, interchangeable lenses, and professional-level viewfinders to the digital world at approachable prices, and then cemented their lead when they started being used to create video as well. They’re experiencing a bit of a decline now, though, as mirrorless cameras start to dominate, and with that comes some unique opportunities. To attach a lens meant for a DSLR to a mirrorless camera, an adapter housing must be used, and [Ancient] found a way to squeeze a computer and a programmable aperture into this tiny space.

The programmable aperture is based on an LCD screen from an old cell phone. LCD screens are generally transparent until their pixels are switched, and in most uses as displays a backer is put in place so someone can make out what is on the screen. [Ancient] is removing this backer, though, allowing the LCD to be completely transparent when switched off. The screen is placed inside this lens adapter housing in the middle of a PCB where a small computer is also placed. The computer controls the LCD via a set of buttons on the outside of the housing, allowing the photographer to use this screen as a programmable aperture.

The LCD-as-aperture has a number of interesting uses that would be impossible with a standard iris aperture. Not only can it function as a standard iris aperture, but it can do things like cycle through different areas of the image in sequence, open up arbitrary parts or close off others, and a number of other unique options. It’s worth checking out the video below, as [Ancient] demonstrates many of these effects towards the end. We’ve seen some of these effects before, although those were in lenses that were mechanically controlled instead.

Continue reading “A Computer That Fits Inside A Camera Lens” →

Disposable Camera Viewfinder Becomes 3D Printed Lens

Disposable cameras are a fun way to get into classical photography. However, they can also be a valuable source of interesting parts that can be put to other uses. For example, as [Billt] demonstrates, their viewfinders can be repurposed into a rather interesting lens for more serious cameras.

[Billt] was lucky enough to score a grabbag of used disposable cameras from a local film lab, and tore them down for parts. He was particularly interested in the viewfinders, since Kodak equipped its disposable cameras with actual plastic lenses for this very purpose.

[Billt] wanted to see what these lenses would do when thrown on the front of a proper digital camera, and set about designing a mount for that purpose. The 3D printed part was designed to mount one of the viewfinder lens assemblies on the front of any Sony E-mount camera. In a rather nifty trick, [Billt] realized the lens assembly could be installed in the adapter by pausing mid-way through the 3D print to drop it in. The only unfortunate thing? The lenses didn’t really work, and all the camera could see was a haze of unfocused light.

With the aid of some cardboard experiments, [Billt] decided to make some changes. The front element of the viewfinder was dumped, with the rear element being used solo instead. This was fitted to the adapter on a simple slide mechanism so that focus could be reliably adjusted. With these changes, the lens came good, and provided some really interesting shots. It’s quite a cropped lens and it can achieve a very close focus distance, as little as 1 inch in testing. It’s quite sharp in the center of the image, while softly blurring out towards the edges—something that sounds very familiar if you’ve used one of these disposable cameras in the wild.

Sometimes it’s fun to grab a random piece of junk to see if you can turn it into something good. Video after the break.

Continue reading “Disposable Camera Viewfinder Becomes 3D Printed Lens” →

Game Boy Camera In Wedding Photo Booth

For those of a certain age the first digital camera many of us experienced was the Game Boy Camera, an add-on for the original Game Boy console. Although it only took pictures with the limited 4-tone monochrome graphics of this system, its capability of being able to take a picture, edit it, create drawings, and then print them out on the Game Boy Printer was revolutionary for the time. Of course the people who grew up with this hardware are about the age to be getting married now (or well beyond), so [Sebastian] capitalized on the nostalgia for it with this wedding photo booth that takes pictures with the Game Boy Camera.

The photo booth features the eponymous Game Boy Camera front-and-center, with a pair of large buttons to allow the wedding guests to start the photography process. The system takes video and then isolates a few still images from it to be printed with the Game Boy Printer. The original Game Boy hardware, as well as a Flask-based web app with a GUI, is all controlled with a Raspberry Pi 4. There’s also a piece of Game Boy hardware called the GB Interceptor that sits between the Game Boy console and the camera cartridge itself which allows the Pi to capture the video feed directly.

The booth doesn’t stop with Game Boy hardware, though. There’s also a modern mirrorless digital camera set up in the booth alongside the Game Boy Camera which allows for higher resolution, full color images to be taken as well. This is also controlled with the same hardware and provides a more modern photo booth experience next to the nostalgic one provided by the Game Boy. There have been many projects which attempt to modernize this hardware, though, like this build which adds color to the original monochrome photos or this one which adds Wi-Fi capability.

Continue reading “Game Boy Camera In Wedding Photo Booth” →

Fusing Cheap EBay Find Into A Digital Rangefinder

One of the earliest commercially-successful camera technologies was the rangefinder — a rather mechanically-complex system that allows a photographer to focus by triangulating a subject, often in a dedicated focusing window, and and frame the shot with another window, all without ever actually looking through the lens. Rangefinder photographers will give you any number of reasons why their camera is just better than the others — it’s faster to use, the focusing is more accurate, the camera is lighter — but in today’s era of lightweight mirrorless digitals, all of these arguments sound like vinyl aficionados saying “The sound is just more round, man. Digital recordings are all square.” (This is being written by somebody who shoots with a rangefinder and listens to vinyl).

While there are loads of analog rangefinders floating around eBay, the trouble nowadays is that digital rangefinders are rare, and all but impossible to find for a reasonable price. Rather than complaining on Reddit after getting fed up with the lack of affordable options, [Mr.50mm] decided to do something about it, and build his own digital rangefinder for less than $250.

Part of the problem is that, aside from a few exceptions, the only digital rangefinders have been manufactured by Leica, a German company often touted as the Holy Grail of photography. Whether you agree with the hype or consider them overrated toys, they’re sure expensive. Even in the used market, you’d be hard-pressed to find an older model for less than $2,000, and the newest models can be upwards of $10,000.

Rather than start from scratch, he fused two low-cost and commonly-available cameras into one with some careful surgery and 3D printing. The digital bits came from a Panasonic GF3, a 12 MP camera that can be had for around $120, and the rangefinder system from an old Soviet camera called the Fed 5, which you can get for less than $50 if you’re lucky. The Fed 5 also conveniently worked with Leica Thread Mount (LTM) lenses, a precursor to the modern bayonet-mount lenses, so [Mr.50mm] lifted the lens mounting hardware from it as well.

Even LTM lenses are relatively cheap, as they’re not compatible with modern Leicas. Anyone who’s dabbled in building or repairing cameras will tell you that there’s loads of precision involved. If the image sensor, or film plane, offset is off by the slightest bit, you’ll never achieve a sharp focus — and that’s just one of many aspects that need to be just right. [Mr.50mm]’s attention to detail really paid off, as the sample images (which you can see in the video below) look fantastic. Continue reading “Fusing Cheap EBay Find Into A Digital Rangefinder” →