Skip to content

Commit

Permalink
BLC model correction (#7)
Browse files Browse the repository at this point in the history
* Copy of infiniteISP v1.1

* Update CCM for RTL, change CCM matrix from config

* Update CSC for RTL, change np.round

* remove float32

* disable sharpen, enable linearization

* change CSC 8 bit division

* rounding off after 2**4 division

* automate_execution currently works on config_automate file to
generate RTL compatible results.

* added in_single_folder flag to set the structure
of the out_for_RTL folder.
If True, all input files are saved in a single folder.

* - automate_execution.py works on configs.yml now
instead of a separate config_automate.yml.
- added is_save flag in configs.yml

* added DG manual flag to bypass auto update by AE operation

* added images to complete 8 sensor RAW inputs

* added short script to output 16-bit reg value for WB gains in config file

* shifted fixed float fraction list to relevant function description

* changes made to run script for windows OS

* added blc scaling factor register value conversion

* generated test vectors in .raw for RAW domain ISP blocks

* modification of BLC model and WB function for parameterized call to fixed point function

* added printing for U16.14 precision fixed point linearize factor

* corrected fixed point generator function arguments

---------

Co-authored-by: muqudas-10xe <[email protected]>
Co-authored-by: Maria_Nadeem-10x <[email protected]>
  • Loading branch information
3 people authored Mar 18, 2023
1 parent 27beb54 commit 50c2a22
Show file tree
Hide file tree
Showing 8 changed files with 26,056 additions and 190 deletions.
22 changes: 11 additions & 11 deletions automate_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@
# 'auto_exposure', 'color_space_conversion', 'ldci', 'sharpen', '2d_noise_reduction', 'scale', 'yuv_conversion_format']


DUT = ["black_level_correction"]
DUT = ["OECF"]


# Modules:
CROP = {"isEnable": False,
"new_width": 1280,
"new_height": 720}

DPC = {"isEnable": False,
DPC = {"isEnable": True,
"dp_threshold": 80}


HDR = {"isEnable": False}

BLC = {"isEnable": True,
"isLinear": False,
"r_offset": 100,
"gr_offset": 110,
"gb_offset": 120,
"b_offset": 130}
"isLinear": True,
"r_offset": 200,
"gr_offset": 200,
"gb_offset": 200,
"b_offset": 200}

OECF = {"isEnable": False}
OECF = {"isEnable": True}
DG = {"is_save": False}

LSC = {"isEnable": False}

BNR = {"isEnable": False,
"filt_window": 9}

WB = {"isEnable": False,
WB = {"isEnable": True,
"isAuto": False}
DEM = {"is_save": False}
AWB = {}
Expand Down Expand Up @@ -73,7 +73,7 @@
# define folder name to save outputs
folder_name = datetime.now().strftime("%Y%m%d_%H%M%S")

input_ext = ".bin"
input_ext = ".raw"


# Set this flag to True if all input files need to be saved in a single folder
Expand Down Expand Up @@ -150,7 +150,7 @@
yaml.safe_dump(config, file, sort_keys=False, default_flow_style=False)

# run isp_pipeline.py on the raw image
subprocess.run(["python3", "./isp_pipeline.py"], check=True)
subprocess.run(["python", "./isp_pipeline.py"], check=True)

shutil.copy(RETAINED_CONFIG, CONFIG_PATH)
os.remove(RETAINED_CONFIG)
Expand Down
26,088 changes: 25,961 additions & 127 deletions config/configs.yml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions isp_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
# x = np.load(save_dir +"Out_scale_" + inFile.split(".")[0]+".npy")
# print(np.array_equal(x, scaled_img))

#=====================================================================
#=====================================================================
# YUV saving format 444, 422 etc
yuv = YUV_C(nr2d_img, sensor_info, parm_yuv, inFile) #parm_csc)
Expand Down
60 changes: 28 additions & 32 deletions modules/black_level_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ def apply_blc_parameters(self):

raw = np.float32(self.img)

## Get Approximates for Linearization - U16.14 precision
# print("Approximated Linearization Factor")
r_linfact, r_linfact_bin = get_approximate(((2**bpp) - 1) / (r_sat - r_offset), 16, 14)
gr_linfact, gr_linfact_bin = get_approximate(((2**bpp) - 1) / (gr_sat - gr_offset), 16, 14)
gb_linfact, gb_linfact_bin = get_approximate(((2**bpp) - 1) / (gb_sat - gb_offset), 16, 14)
b_linfact, b_linfact_bin = get_approximate(((2**bpp) - 1) / (b_sat - b_offset), 16, 14)
print(r_linfact, gr_linfact, gb_linfact, b_linfact)
print('R linearization factor: ' + r_linfact_bin)
print('Gr linearization factor: ' + gr_linfact_bin)
print('Gb linearization factor: ' + gb_linfact_bin)
print('B linearization factor: ' + b_linfact_bin)

if bayer == "rggb":

# implementing this formula with condition
Expand All @@ -52,35 +64,19 @@ def apply_blc_parameters(self):
raw[1::2, 0::2] = raw[1::2, 0::2] - gb_offset
raw[1::2, 1::2] = raw[1::2, 1::2] - b_offset

## Actual Values
print("Actual Linearization Factor")
# r_sat = (r_sat - r_offset) / ((2**bpp) - 1)
# gr_sat = (gr_sat - gr_offset) / ((2**bpp) - 1)
# gb_sat = (gb_sat - gb_offset) / ((2**bpp) - 1)
# b_sat = (b_sat - b_offset) / ((2**bpp) - 1)
# print(r_sat1, gr_sat1, gb_sat1, b_sat1)


## Get Approximates for Linearization
# print("Approximated Linearization Factor")
r_sat, _ = get_approximate((r_sat - r_offset) / ((2**bpp) - 1))
gr_sat, _ = get_approximate((gr_sat - gr_offset) / ((2**bpp) - 1))
gb_sat, _ = get_approximate((gb_sat - gb_offset) / ((2**bpp) - 1))
b_sat, _ = get_approximate((b_sat - b_offset) / ((2**bpp) - 1))
print(r_sat, gr_sat, gb_sat, b_sat)

if self.is_linearize is True:
raw[0::2, 0::2] = (
raw[0::2, 0::2] / (r_sat)
raw[0::2, 0::2] * r_linfact
)
raw[0::2, 1::2] = (
raw[0::2, 1::2] / (gr_sat)
raw[0::2, 1::2] * gr_linfact
)
raw[1::2, 0::2] = (
raw[1::2, 0::2] / (gb_sat)
raw[1::2, 0::2] * gb_linfact
)
raw[1::2, 1::2] = (
raw[1::2, 1::2] / (b_sat)
raw[1::2, 1::2] * b_linfact
)

elif bayer == "bggr":
Expand All @@ -91,16 +87,16 @@ def apply_blc_parameters(self):

if self.is_linearize is True:
raw[0::2, 0::2] = (
raw[0::2, 0::2] / (b_sat)
raw[0::2, 0::2] * b_linfact
)
raw[0::2, 1::2] = (
raw[0::2, 1::2] / (gb_sat)
raw[0::2, 1::2] * gb_linfact
)
raw[1::2, 0::2] = (
raw[1::2, 0::2] / (gr_sat)
raw[1::2, 0::2] * gr_linfact
)
raw[1::2, 1::2] = (
raw[1::2, 1::2] / (r_sat)
raw[1::2, 1::2] * r_linfact
)

elif bayer == "grbg":
Expand All @@ -111,16 +107,16 @@ def apply_blc_parameters(self):

if self.is_linearize is True:
raw[0::2, 0::2] = (
raw[0::2, 0::2] / (gr_sat)
raw[0::2, 0::2] * gr_linfact
)
raw[0::2, 1::2] = (
raw[0::2, 1::2] / (r_sat)
raw[0::2, 1::2] * r_linfact
)
raw[1::2, 0::2] = (
raw[1::2, 0::2] / (b_sat)
raw[1::2, 0::2] * b_linfact
)
raw[1::2, 1::2] = (
raw[1::2, 1::2] / (gb_sat)
raw[1::2, 1::2] * gb_linfact
)

elif bayer == "gbrg":
Expand All @@ -131,16 +127,16 @@ def apply_blc_parameters(self):

if self.is_linearize is True:
raw[0::2, 0::2] = (
raw[0::2, 0::2] / (gb_sat)
raw[0::2, 0::2] * gb_linfact
)
raw[0::2, 1::2] = (
raw[0::2, 1::2] / (b_sat)
raw[0::2, 1::2] * b_linfact
)
raw[1::2, 0::2] = (
raw[1::2, 0::2] / (r_sat)
raw[1::2, 0::2] * r_linfact
)
raw[1::2, 1::2] = (
raw[1::2, 1::2] / (gr_sat)
raw[1::2, 1::2] * gr_linfact
)

raw = np.where(raw >= 0, np.floor(raw+ 0.5),
Expand Down
4 changes: 2 additions & 2 deletions modules/white_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def apply_wb_parameters(self):
bluegain = self.parm_wbc["b_gain"]
self.raw = np.float32(self.img)

redgain, _ = get_approximate(redgain)
bluegain, _ = get_approximate(bluegain)
redgain, _ = get_approximate(redgain, 16, 8)
bluegain, _ = get_approximate(bluegain, 16, 8)

if self.bayer == "rggb":
self.raw[::2, ::2] = self.raw[::2, ::2] * redgain
Expand Down
19 changes: 19 additions & 0 deletions util/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,22 @@
# print(np.array_equal(gt_file[:,:,0], out_file[0,:,:]))
# print(np.array_equal(gt_file[:,:,1], out_file[1,:,:]))
# print(np.array_equal(gt_file[:,:,2], out_file[2,:,:]))

# print(np.array_equal(lst_ch[2], r_bin))
#======================================================
# test arrange_channel function
path_rearr = "./out_for_RTL/Results/Output/rearranged_Out_gmc_ColorCheckerRaw_100DPs_ISO100_2592x1536_12bits_RGGB.npy"
path_org = "./out_for_RTL/Results/Output/Out_gmc_ColorCheckerRaw_100DPs_ISO100_2592x1536_12bits_RGGB.npy"
# get_RTL_input(path)
# re_arranged = np.load(path_rearr)
# org_arr = np.load(path_org)
# out_arr = np.zeros(org_arr.shape)
# out_arr[:, :,0] = re_arranged[0,:,:]
# out_arr[:, :,1] = re_arranged[1,:,:]
# out_arr[:, :,2] = re_arranged[2,:,:]
# print(np.array_equal(org_arr, out_arr))
#======================================================
from pathlib import Path
path_rearr = "./out_for_RTL/Results/"
new_dir= Path(path_rearr).joinpath(Path("Mine/"))
new_dir.mkdir(parents=True, exist_ok=False)
38 changes: 20 additions & 18 deletions util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,19 @@
------------------------------------------------------------
"""
import os
import os
import random
from fxpmath import Fxp
import warnings
import shutil
import numpy as np
from shutil import move
from pathlib import Path
from shutil import move
from pathlib import Path
from scipy.signal import correlate2d


# FIXED_FLOATS = np.array(
# [
# 0.5,
# 0.25,
# 0.125,
# 0.0625,
# 0.03125,
# 0.015625,
# 0.0078125,
# 0.00390625,
# 0.00195312,
# 0.00097656,
# ]
# )


def introduce_defect(img, total_defective_pixels, padding):

"""
Expand Down Expand Up @@ -161,7 +148,22 @@ def stride_convolve2d(matrix, kernel):
:: kernel.shape[0], :: kernel.shape[1]
]

def get_approximate(decimal):

# FIXED_FLOATS = np.array(
# [
# 0.5,
# 0.25,
# 0.125,
# 0.0625,
# 0.03125,
# 0.015625,
# 0.0078125,
# 0.00390625,
# 0.00195312,
# 0.00097656,
# ]
# )
def get_approximate(decimal, register_bits, frac_precision_bits):
"""
Returns Fixed Float Approximation of a decimal
"""
Expand All @@ -182,7 +184,7 @@ def get_approximate(decimal):
# return approx + fixed_float

# return integral + approx, "".join(flags)
fixed_float = Fxp(decimal, False, 16, 8)
fixed_float = Fxp(decimal, False, register_bits, frac_precision_bits)
return fixed_float(), fixed_float.bin()


Expand Down
14 changes: 14 additions & 0 deletions util/wbg_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np
from utils import get_approximate

#put white balance gain values from config.yml as arguments of get_approximate
redgain, rgainpattern = get_approximate(1.24609375)
bluegain, bgainpattern = get_approximate(2.80859375)
blc_scalingfact, blc_scalingfactpattern = get_approximate(4095/(3000-203))

print('check values in debug window')
print('red gain reg value (U8.8 format): ' + rgainpattern)
print('blue gain reg value (U8.8 format): ' + bgainpattern)
print('blc scaling fact reg value (U8.8 format): ' + blc_scalingfactpattern)


0 comments on commit 50c2a22

Please sign in to comment.