How do you debayer an image in Magick.Net

166 Views Asked by At

I have a raw bayer image and I'm trying to figure out if there is an option to debayer the image inside of Magick.Net

I see there is a MagickFormat.Bayer, but I'm not sure how to specify the bayer pattern. The bayer pattern is GRBG.

this is what I've tried

var readSettings = new MagickReadSettings { Depth = 8, Width = 2750, Height = 2200, Format = MagickFormat.Bayer };
var image = new MagickImage(rawImageBytes, readSettings);

which is resulting is a slightly colorized image, but things that should be red are green, and the colors are generally muted.

enter image description here

If I read the image in as a grayscale image it looks like this

enter image description here

and if you zoom in on the grayscale image you can see the bayer pattern (below is zoomed in on the tailight of the vehicle parked outside

enter image description here

Linked below is the raw image. It's 2750 x 2200 8 bits per pixel and it's in a Bayer GRBG pattern. This is just the raw pixel data from a machine vision camera, so it's not a normal raw file format from a digital camera like Canon or Nikon, so no header information, just raw pixels. https://drive.google.com/file/d/1A_uuobZSt-WczD8ysam7gIhZ-zzspSDM/view?usp=drive_link

2

There are 2 best solutions below

0
TJR On BEST ANSWER

If you look at the Bayer code in ImageMagick here

The default pattern is RGGB, and it looks like there is no way to specify a different Bayer pattern. Also, it looks like the debayer algorithm is a simplified approach and there doesn't seem to be any way to change the algorithm used like bilinear or VNG or nearest neighbor etc, so unless the base libraries are updated to support things like this, it looks like this is just not possible currently.

1
Christoph Rackwitz On

This is to be expected.

Debayering is necessary but not sufficient.

You're missing parts of the image decoding pipeline:

  • subtracting darkfield
  • dividing by brightfield/flatfield, which itself must be darkfield-corrected
    • this operation implicitly does white balance, if the brightfield is within sensor value range
    • it also removes vignette
  • any white balance you might desire, which is one factor on each color channel
  • gamma mapping

Further reading: https://www.eecs.yorku.ca/~mbrown/ICCV19_Tutorial_MSBrown.pdf

Actually using GRBG, I get this:

debayered straight

And with some range stretching but no gamma mapping:

enter image description here

That is WITHOUT proper white balance. I could pick an area on the white truck out there and apply "gray world assumption".

If I applied gamma mapping, you wouldn't be able to see the indoor parts anymore.