Skip to content

Commit

Permalink
fix: inversion between red and blue channel, use all green signal
Browse files Browse the repository at this point in the history
feat: introduce black_leval_camera and init_black_level
  • Loading branch information
splanquart committed Jan 4, 2025
1 parent a7669d8 commit 5526d3e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
28 changes: 20 additions & 8 deletions app/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from libcamera import controls
from picamera2 import Picamera2, Controls
from picamera2.platform import Platform

from numba import jit
from abc import ABC
from abc import abstractmethod
from image import AbstractImageRaw12BitColor, factory_image_raw

def getMaxAduValue(array):
Expand Down Expand Up @@ -245,10 +246,22 @@ def capture_raw_image(self):

class IMX477Camera_CSI_rpi5(BaseIMX477Camera_CSI):
def __init__(self):
super().__init__(16)
self.depth = 16
"""
Initializes the camera with specific settings.
Attributes:
depth (int): The bit depth of the camera.
offset (int): The offset value for the camera.
tuning_file_name (str): The name of the tuning file for the camera.
black_level_camera (int): An offset of 250 ADU applied by the manufacturer,
which needs to be subtracted. We want to leave 80 ADU.
"""
super().__init__(12)
self.offset = 800
self.tuning_file_name = "imx477_scientific_pisp.json"
inti_black_level = 80
# The black level is 256 ADU, we want to leave 80 ADU
# see more about camera black level https://www.strollswithmydog.com/pi-hq-cam-sensor-performance/
self.black_level_camera = 256 - inti_black_level
def capture_raw_image(self):
"""
Capture a raw image from the camera and extract the 12 most significant bits.
Expand All @@ -259,8 +272,10 @@ def capture_raw_image(self):
raw_array = self._picam2.capture_array('raw').view(np.uint16)

# Extract the 12 most significant bits
raw_array_12bit = (raw_array >> 4).astype(np.uint16)
return raw_array_12bit
raw_array_12bit = raw_array >> 4
# Apply the offset see https://www.strollswithmydog.com/pi-hq-cam-sensor-performance/
raw_array_12bit = raw_array_12bit - self.black_level_camera
return raw_array_12bit.astype(np.uint16)



Expand All @@ -277,6 +292,3 @@ def factory_imx477_camera_csi() -> BaseIMX477Camera_CSI:
else:
print("Create Camera object for Raspberry Pi 4")
return IMX477Camera_CSI_rpi4()



8 changes: 4 additions & 4 deletions app/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def extract_green_channel(self) -> np.ndarray:
Returns:
numpy.ndarray: Extracted green channel.
"""
return (self.array[0::2, 1::2] + self.array[1::2, 0::2]) / 2
return self.array[0::2, 1::2] + self.array[1::2, 0::2]

def extract_blue_channel(self) -> np.ndarray:
"""
Expand All @@ -112,15 +112,15 @@ def extract_red_channel(self) -> np.ndarray:
Returns:
numpy.ndarray: Extracted red channel.
"""
return self.array[0::2, 0::2]
return self.array[1::2, 1::2]
def extract_green_channel(self) -> np.ndarray:
"""
Extract the green channel from the Bayer pattern array.
Returns:
numpy.ndarray: Extracted green channel.
"""
return (self.array[0::2, 1::2] + self.array[1::2, 0::2]) / 2
return self.array[0::2, 1::2] + self.array[1::2, 0::2]

def extract_blue_channel(self) -> np.ndarray:
"""
Expand All @@ -129,7 +129,7 @@ def extract_blue_channel(self) -> np.ndarray:
Returns:
numpy.ndarray: Extracted blue channel.
"""
return self.array[1::2, 1::2]
return self.array[0::2, 0::2]

def factory_image_raw(array: np.ndarray) -> AbstractImageRaw12BitColor:
"""
Expand Down

0 comments on commit 5526d3e

Please sign in to comment.