Looking at a Real NEF Bayer Pattern

(Article updated 8-May-2008 to reflect version 1.1 of the app: The Lighten checkbox makes images even lighter, and there’s a new demosaicing method, explained below.)

Most raw files contain data from a sensor arranged to record the image in a Bayer mosaic: Each pixel is a value of either red, green, or blue, and you get the image by demosaicing—using interpolation of some sort to generate color values at each pixel location.

(There’s a pretty good Wikipedia article on Bayer patterns.)

Diagrams are nice, but I wanted to see a real pattern, from a real image. So, I wrote a little app that shows exactly that. Here’s a sample image zoomed so far in (much farther than 1:1) that you can see the actual pixels in the Bayer mosaic:

What the program does to display the Bayer pattern is:

  1. Read the raw image data from a NEF file.
  2. Build a bitmap where either the red, green, or blue component for each pixel is taken from the associated data value, depending on which cell of the Bayer array it is. Only one component gets a value; the other two are zero.
  3. Display the bitmap as you see above. The zoom checkbox was checked so individual pixels (red, green, or blue, with half of them green) can be seen.

Here’s part of the image unzoomed, at actual size (1:1):

There are lots of ways to do demosaicing, but the simplest is called “nearest neighbor”. For each pixel, you get the two components it isn’t (e.g., red and blue) by taking the average of surrounding pixels. Here’s the image demosaiced; as you can see, the color balance is still way off:

[8-May-2008 update] In the newest version of the app (1.1), I’ve added another variant of nearest neighbor that reduces the value of the green component by half to make the image less green, which serves as a crude color balance:

There’s also a grayscale choice, produced by taking the single component and setting the other two to the same value:

If you’d like to play around with this little app, you can download a copy for free (Mac only). Don’t worry if you don’t have a NEF file to open, as the app will automatically open one that’s built in to it.

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

6 Comments »

 
  • Dennis Aubrey says:

    Re #3 above: Display the bitmap as you see above. The zoom checkbox was checked so individual pixels (red, green, or blue, with 2/3 of them green) can be seen.

    Shouldn’t 1/2 the pixels be green, 1/4 blue and 1/4 red?

  • marc says:

    Right you are, Dennis… good catch. (Correction made.)

  • Andreas Weber says:

    NEFs from what cameras is this app supposed to be able to read? I just tried files from D70, D200 and D3, none of which the app could understand …

  • Rafamaldo says:

    Ten program cannot open NEF files from my D200…

  • marc says:

    On your camera’s menu, turn RAW Compression off. The app should work on uncompressed D200 files. Other NEFs haven’t been tested, but might work as well.