%%capture
!pip install kornia
!pip install rawpy
Convert RGB to RAW
Basic
Color spaces
kornia.color
In this tutorial we are going to learn how to convert image from raw color using
kornia.color
.
Download necessary files and libraries
import io
import requests
def download_image(url: str, filename: str = "") -> str:
= url.split("/")[-1] if len(filename) == 0 else filename
filename # Download
= io.BytesIO(requests.get(url).content)
bytesio # Save file
with open(filename, "wb") as outfile:
outfile.write(bytesio.getbuffer())
return filename
= "https://docs.google.com/uc?export=download&id=1nSM_FYJ7i9-_57ecPY5sCG2s8zt9dRhF"
url "raw.dng") download_image(url,
'raw.dng'
Import necessary libraries
import kornia
import matplotlib.pyplot as plt
import numpy as np
import rawpy
import torch
Prepare the raw file through rawpy
= "raw.dng"
path = rawpy.imread(path)
raw = "".join([chr(raw.color_desc[i]) for i in raw.raw_pattern.flatten()])
cfa
# 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":
= kornia.color.CFA.GB
korniacfa elif cfa == "GBRG":
= kornia.color.CFA.GR
korniacfa elif cfa == "BGGR":
= kornia.color.CFA.RG
korniacfa elif cfa == "RGGB":
= kornia.color.CFA.BG
korniacfa
# 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
= raw.raw_image
rawdata # white level gives maximum value for a pixel
= torch.Tensor(rawdata.astype(np.float32) / raw.white_level).reshape(
rawtensor 1, 1, raw.raw_image.shape[0], raw.raw_image.shape[1]
)= kornia.color.raw.raw_to_rgb(rawtensor, korniacfa) rgbtensor
Visualize
= np.moveaxis(np.squeeze((rgbtensor * 255.0).numpy().astype(np.uint8)), 0, 2)
npimg
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)
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
= kornia.color.raw.raw_to_rgb(torch.rot90(rawtensor, 1, [2, 3]), korniacfa)
rgbtensor = np.moveaxis(np.squeeze((rgbtensor * 255.0).numpy().astype(np.uint8)), 0, 2)
npimg
plt.figure() plt.imshow(npimg)
# If we crop, we can adjust for this by using a different cfa
= kornia.color.raw.raw_to_rgb(rawtensor[:, :, 1:1023, 1:1023], kornia.color.raw.CFA.GR)
rgbtensor = np.moveaxis(np.squeeze((rgbtensor * 255.0).numpy().astype(np.uint8)), 0, 2)
npimg
plt.figure() plt.imshow(npimg)