Open in Colab

Convert RGB to RAW#

Download necessary files and libraries#

!pip install kornia
!pip install rawpy
!wget -q --show-progress --no-check-certificate '' -O raw.dng

Import necessary libraries#

import torch
import numpy as np
import kornia
import rawpy
import matplotlib.pyplot as plt
/home/docs/checkouts/ TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See
  from .autonotebook import tqdm as notebook_tqdm

Prepare the raw file through rawpy#

path = "raw.dng"
raw = rawpy.imread(path)
cfa = "".join([chr(raw.color_desc[i]) for i in raw.raw_pattern.flatten()])

# Figure out which cfa we are using by looking at the component order from rawpy
# if we do this directly from a camera this would of course be known ahead
# of time
if (cfa == "GRBG"):
    korniacfa = kornia.color.CFA.GB
elif (cfa == "GBRG"):
    korniacfa = kornia.color.CFA.GR
elif (cfa == "BGGR"):
    korniacfa = kornia.color.CFA.RG
elif (cfa == "RGGB"):
    korniacfa = kornia.color.CFA.BG

# This is a GB file i.e. top left pixel is Green follow by Red (and the pair
# starting at (1,1) is Green, Blue)

Get the data into kornia by doing the conversion#

# We find the data inside raw.raw_image
rawdata = raw.raw_image
#white level gives maximum value for a pixel
rawtensor = torch.Tensor(rawdata.astype(np.float) / raw.white_level).reshape(1, 1, raw.raw_image.shape[0], raw.raw_image.shape[1])
rgbtensor = kornia.color.raw.raw_to_rgb(rawtensor, korniacfa)
/home/docs/checkouts/ DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance:
  after removing the cwd from sys.path.


npimg = np.moveaxis(np.squeeze((rgbtensor * 255.0).numpy().astype(np.uint8)), 0, 2)

# Show the image
# Colors will look a little funky because they need to be balanced properly, but
# the leaves are supposed to be redm berries blue and grass green
<matplotlib.image.AxesImage at 0x7f77bfa01350>

Gotchas: Rotation gives a different cfa#

# if we do a pipeline were we first rotate the image, it will end up with a
# different cfa that isn't possible to describe since we are assuming all red
# samples are on t.he same row while they would not be rotated
rgbtensor = kornia.color.raw.raw_to_rgb(torch.rot90(rawtensor, 1, [2,3]), korniacfa)
npimg = np.moveaxis(np.squeeze((rgbtensor * 255.0).numpy().astype(np.uint8)), 0, 2)
<matplotlib.image.AxesImage at 0x7f77bad23650>
# If we crop, we can adjust for this by using a different cfa
rgbtensor = kornia.color.raw.raw_to_rgb(rawtensor[:,:,1:1023, 1:1023], kornia.color.raw.CFA.GR)
npimg = np.moveaxis(np.squeeze((rgbtensor * 255.0).numpy().astype(np.uint8)), 0, 2)
<matplotlib.image.AxesImage at 0x7f77bacc2290>