Open in Colab

Convert RGB to RAW#

Download necessary files and libraries#

%%capture
!pip install kornia
!pip install rawpy
!wget -q --show-progress --no-check-certificate 'https://docs.google.com/uc?export=download&id=1nSM_FYJ7i9-_57ecPY5sCG2s8zt9dRhF' -O raw.dng

Import necessary libraries#

import torch
import numpy as np
import kornia
import rawpy
import matplotlib.pyplot as plt
/home/docs/checkouts/readthedocs.org/user_builds/kornia-tutorials/envs/latest/lib/python3.7/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  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)
print(cfa)
print(korniacfa)
GRBG
CFA.GB

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/readthedocs.org/user_builds/kornia-tutorials/envs/latest/lib/python3.7/site-packages/ipykernel_launcher.py:4: 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: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  after removing the cwd from sys.path.

Visualize#

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

# 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
plt.imshow(npimg)
<matplotlib.image.AxesImage at 0x7f77bfa01350>
_images/af9f6c66e29bc9d076bb38a4919c7fb1dc90ffbd8da8451a546e7077eebdf2dd.png

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)
plt.figure()
plt.imshow(npimg)
<matplotlib.image.AxesImage at 0x7f77bad23650>
_images/b2130da67777de2041d0c4a6abd9e597efa73d8da3e406fad14b0efdb9648b53.png
# 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)
plt.figure()
plt.imshow(npimg)
<matplotlib.image.AxesImage at 0x7f77bacc2290>
_images/4e5031dc9e92fd6e350832d648042e5ac6c4e5d6817e82495ea8bb64233bb925.png