Skip to content

Instantly share code, notes, and snippets.

@vigneshgarrapally
Created June 10, 2022 19:51
Show Gist options
  • Select an option

  • Save vigneshgarrapally/dcb83deabb1ce56e6de3b3a427162be9 to your computer and use it in GitHub Desktop.

Select an option

Save vigneshgarrapally/dcb83deabb1ce56e6de3b3a427162be9 to your computer and use it in GitHub Desktop.

Revisions

  1. vigneshgarrapally created this gist Jun 10, 2022.
    26 changes: 26 additions & 0 deletions bayer_to_rgb.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/usr/bin/env python3
    #source: https://stackoverflow.com/a/58693798/15374191
    import numpy as np
    from skimage.io import imsave

    # Width and height of Bayer image, not original which is w/2 x h/2
    w, h = 1013, 672
    ow, oh = w//2, h//2

    # Load in Bayer8 image, assumed raw 8-bit GBRG
    bayer = np.fromfile('bayer.bin', dtype=np.uint8).reshape((h,w))

    # Pick up raw uint8 samples
    R = bayer[1::2, 0::2] # rows 1,3,5,7 columns 0,2,4,6
    B = bayer[0::2, 1::2] # rows 0,2,4,6 columns 1,3,5,7
    G0 = bayer[0::2, 0::2] # rows 0,2,4,6 columns 0,2,4,6
    G1 = bayer[1::2, 1::2] # rows 1,3,5,7 columns 1,3,5,7

    # Chop any left-over edges and average the 2 Green values
    R = R[:oh,:ow]
    B = B[:oh,:ow]
    G = G0[:oh,:ow]//2 + G1[:oh,:ow]//2

    # Formulate image by stacking R, G and B and save
    out = np.dstack((R,G,B))
    imsave('result.png',out)