diff --git a/configs/capture_rpi_gs.yaml b/configs/capture_rpi_gs.yaml index 94c3202c..455b1ef0 100644 --- a/configs/capture_rpi_gs.yaml +++ b/configs/capture_rpi_gs.yaml @@ -21,3 +21,5 @@ capture: gray: False down: null awb_gains: null + nbits_out: 10 + nbits: 10 # 8 or 10 for global shutter diff --git a/lensless/hardware/sensor.py b/lensless/hardware/sensor.py index c842dc2d..0785204e 100644 --- a/lensless/hardware/sensor.py +++ b/lensless/hardware/sensor.py @@ -88,7 +88,7 @@ class SensorParam: SensorParam.RESOLUTION: np.array([1088, 1456]), SensorParam.DIAGONAL: 6.3e-3, SensorParam.COLOR: True, - SensorParam.BIT_DEPTH: [8, 12], + SensorParam.BIT_DEPTH: [8, 10], SensorParam.MAX_EXPOSURE: 15534385e-6, SensorParam.MIN_EXPOSURE: 29e-6, }, diff --git a/lensless/utils/io.py b/lensless/utils/io.py index 6a4d5e42..4bf07890 100644 --- a/lensless/utils/io.py +++ b/lensless/utils/io.py @@ -83,13 +83,15 @@ def load_image( RGB image of dimension (height, width, 3). """ assert os.path.isfile(fp) + + nbits = None # input bit depth if "dng" in fp: import rawpy assert bayer raw = rawpy.imread(fp) img = raw.raw_image - # # # TODO : use raw.postprocess? + # # # TODO : use raw.postprocess? to much unknown processing... # img = raw.postprocess( # adjust_maximum_thr=0, # default 0.75 # no_auto_scale=False, @@ -110,6 +112,7 @@ def load_image( # red_gain = camera_wb[0] # blue_gain = camera_wb[1] + nbits = int(np.ceil(np.log2(raw.white_level))) ccm = raw.color_matrix[:, :3] black_level = np.array(raw.black_level_per_channel[:3]).astype(np.float32) elif "npy" in fp or "npz" in fp: @@ -119,11 +122,12 @@ def load_image( if bayer: assert len(img.shape) == 2, img.shape - if img.max() > 255: - # HQ camera - n_bits = 12 - else: - n_bits = 8 + if nbits is None: + if img.max() > 255: + # HQ camera + nbits = 12 + else: + nbits = 8 if back: back_img = cv2.imread(back, cv2.IMREAD_UNCHANGED) @@ -132,10 +136,11 @@ def load_image( img = np.clip(img, a_min=0, a_max=img.max()) img = img.astype(dtype) if nbits_out is None: - nbits_out = n_bits + nbits_out = nbits + img = bayer2rgb_cc( img, - nbits=n_bits, + nbits=nbits, blue_gain=blue_gain, red_gain=red_gain, black_level=black_level, diff --git a/scripts/measure/on_device_capture.py b/scripts/measure/on_device_capture.py index f0aae9b5..26787dd6 100644 --- a/scripts/measure/on_device_capture.py +++ b/scripts/measure/on_device_capture.py @@ -73,6 +73,10 @@ def capture(config): res = config.res nbits_out = config.nbits_out + assert ( + nbits_out in sensor_dict[sensor][SensorParam.BIT_DEPTH] + ), f"nbits_out must be one of {sensor_dict[sensor][SensorParam.BIT_DEPTH]} for sensor {sensor}" + # https://www.raspberrypi.com/documentation/accessories/camera.html#hardware-specification sensor_param = sensor_dict[sensor] assert exp <= sensor_param[SensorParam.MAX_EXPOSURE] diff --git a/scripts/measure/remote_capture.py b/scripts/measure/remote_capture.py index 188a7dde..6777347b 100644 --- a/scripts/measure/remote_capture.py +++ b/scripts/measure/remote_capture.py @@ -34,7 +34,7 @@ import matplotlib.pyplot as plt import rawpy from lensless.hardware.utils import check_username_hostname -from lensless.hardware.sensor import SensorOptions +from lensless.hardware.sensor import SensorOptions, sensor_dict, SensorParam from lensless.utils.image import rgb2gray, print_image_info from lensless.utils.plot import plot_image, pixel_histogram from lensless.utils.io import save_image @@ -61,6 +61,13 @@ def liveview(config): source = config.capture.source plot = config.plot + assert ( + nbits_out in sensor_dict[sensor][SensorParam.BIT_DEPTH] + ), f"capture.nbits_out must be one of {sensor_dict[sensor][SensorParam.BIT_DEPTH]} for sensor {sensor}" + assert ( + config.capture.nbits in sensor_dict[sensor][SensorParam.BIT_DEPTH] + ), f"capture.nbits must be one of {sensor_dict[sensor][SensorParam.BIT_DEPTH]} for sensor {sensor}" + if config.save: if config.output is not None: # make sure output directory exists