import numpy as np
import rawpy
import matplotlib.pyplot as plt
def pack_raw(raw):
    # pack Bayer image to 4 channels
    im = raw.raw_image_visible.astype(np.float32)
    im = np.maximum(im - 512, 0) / (16383 - 512)  # subtract the black level

    im = np.expand_dims(im, axis=2)
    img_shape = im.shape
    H = img_shape[0]
    W = img_shape[1]

    out = np.concatenate((im[0:H:2, 0:W:2, :],
                          im[0:H:2, 1:W:2, :],
                          im[1:H:2, 1:W:2, :],
                          im[1:H:2, 0:W:2, :]), axis=2)
    return np.expand_dims(out, axis=0)
def raw2rgb(matrix):
    # 4 channels raw to RGB
    tmp=matrix.copy()
    _,width,height,dim=tmp.shape
    tmp=tmp[:,:,:,:-1].reshape(width,height,3)
    tmp[:,:,1]=tmp[:,:,1]/2
    tmp=np.transpose(tmp,(1,0,2))
    tmp = np.flip(tmp,0)
    return tmp 

def rgb2raw(matrix):
    # RGB to 4 channels bayer raw image
    tmp=matrix.copy()
    height,width,dim=tmp.shape
    tmp = np.flip(tmp,0)
    tmp=np.transpose(tmp,(1,0,2))
    tmp[:,:,1]=tmp[:,:,1]*2
    tmp=np.concatenate((tmp,tmp[:,:,1].reshape(width,height,1)),axis=2)
    #print(tmp.shape)
    return tmp.reshape(1,width,height,4)

标签: none

评论已关闭