Skip to content

Commit

Permalink
Fix nbits for global shutter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebezzam committed Oct 3, 2023
1 parent 30a5287 commit aa43d38
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions configs/capture_rpi_gs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ capture:
gray: False
down: null
awb_gains: null
nbits_out: 10
nbits: 10 # 8 or 10 for global shutter
2 changes: 1 addition & 1 deletion lensless/hardware/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
21 changes: 13 additions & 8 deletions lensless/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions scripts/measure/on_device_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 8 additions & 1 deletion scripts/measure/remote_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit aa43d38

Please sign in to comment.