Coding fun during the holidays

During the holidays, I had an idea about a highlight reconstruction method. Knowing that naïve methods invented by people with no background in image processing are highly unlikely to work, I sat down to code it. I ended up with an awfully slow and useless program, but had tremendous fun (I left parsing the raw file to dcraw, running it with dcraw -4 -T -D to get the Bayer data). I now have something that can demosaic the Bayer data, apply white balance, and clip the blown pixels (plus, it can burn serious amounts of CPU time trying and failing to recover any meaningful details with my magic algorithm :smiley: ). No camera colour profile is applied yet, and the output is linear PNM.

Certainly more fun than my day programming job (business apps).

3 Likes

Then please show us the code and/or example images.

There is really nothing to see. The images were just random samples from my LX7. The code is in Java, and was not written for performance, and I’m not ready to share it, it’s in such a mess.
I may come back and share it once I’ve cleaned it up.
I recommend the article
https://www.odelama.com/photo/Developing-a-RAW-Photo-by-hand/
instead.
For demosaicing, I have two algorithms, one is simply binning:
red out = red pixel’s value from the G-R-B-G; blue the same way; green is average of the two greens
The other is full-res, it’s simply taking the values from the neighbours for each pixel. If the Bayer pixels are:

       0  1  2  3  4  5
row 0: R |G |R |G |R |G
       --+--+--+--+--+--
row 1: G |B'|G |B |G |B
       --+--+--+--+--+--
row 2: R |G |R |G |R |G

Then for the blue (B’) at [1, 1]:

  • blue is taken from B’
  • red is taken as the average of the 4 R corners of B’
  • green is taken as the average of the top, bottom, left and right G pixels
    It’s the simplest algorithm I could come up with to produce full-sized output.

The grey conversion: I keep track which raw pixels where blown, and set all pixels to grey to which they contribute. So, if B’ at [1, 1] is blown, I set to grey the following:
[0, 0] [0, 1] [0, 2] [1, 0] [1, 1] [1, 2] [2, 0] [2, 1] [2, 2]
Setting to grey is done by setting the pixel’s R, G and B to the average of the three, so some lightness detail is preserved.
The greying is done after applying white balance.