Skip to content

Commit

Permalink
helper notebooks and unpack tiff
Browse files Browse the repository at this point in the history
  • Loading branch information
mkutu committed Nov 15, 2024
1 parent 68ca173 commit 5efaeb0
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 17 deletions.
88 changes: 78 additions & 10 deletions notebooks/create_colorchecker_mask.ipynb

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions src/copy_from_lockers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ def copy_from_lockers_in_parallel(src_dir, dest_dir, max_workers=12, raw_extensi
log.info(f"Copying raw image files with extension {raw_extension}")
# Collect all ARW file paths
raw_files = list(Path(src_dir).glob(f"*{raw_extension}"))

checked_raw_files = []
for raw_file in raw_files:
if os.path.getsize(raw_file) > 0:
checked_raw_files.append(raw_file)
else:
log.warning(f"Skipping empty file: {raw_file}")

log.info(f"Copying {len(raw_files)} raw image files from {src_dir} to {dest_dir}")

# Use ThreadPoolExecutor for parallel copying
Expand Down
81 changes: 81 additions & 0 deletions src/unpack_tiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import numpy as np
import cv2
from pathlib import Path
from colour_demosaicing import demosaicing_CFA_Bayer_Malvar2004, demosaicing_CFA_Bayer_bilinear

class TIFFProcessor:
def __init__(self, file_path, im_height, im_width):
self.file_path = Path(file_path)
self.im_height = im_height
self.im_width = im_width
self.packed_img = None
self.unpacked_img = None
self.demosaiced_img = None

def load_image(self):
"""Loads the packed TIFF image."""
self.packed_img = cv2.imread(str(self.file_path))
if self.packed_img is None:
raise FileNotFoundError(f"Could not load image from {self.file_path}")
print(f"Loaded image with shape: {self.packed_img.shape}")

def unpack_image(self):
"""Unpacks a 12-bit packed image to 16-bit format."""
if self.packed_img is None:
raise ValueError("Image not loaded. Please load the image first.")

layer1 = self.packed_img[:, :, 0]
nparray = layer1.reshape((int(self.im_height * self.im_width * 1.5))).astype(np.uint16)
nparray_16bit = np.empty(self.im_width * self.im_height, dtype=np.uint16)

# Unpack 12-bit packed data into 16-bit
nparray_16bit[::2] = (nparray[::3] << 4) | (nparray[1::3] & 0b00001111)
nparray_16bit[1::2] = nparray[2::3] | ((nparray[1::3] & 0b11110000) << 4)

self.unpacked_img = nparray_16bit.reshape((self.im_height, self.im_width)) << 4
print(f"Unpacked image to shape: {self.unpacked_img.shape}, dtype: {self.unpacked_img.dtype}")

def demosaic_image(self):
"""Applies demosaicing to the unpacked 16-bit image."""
if self.unpacked_img is None:
raise ValueError("Image not unpacked. Please unpack the image first.")

image_data = (self.unpacked_img).astype(np.float32) / 65535.0
# colour_image = demosaicing_CFA_Bayer_Malvar2004(image_data, "RGGB")
colour_image = demosaicing_CFA_Bayer_bilinear(image_data, pattern="RGGB")
# Clamp values to the [0, 1] range
colour_image = np.clip(colour_image, 0, 1)

gamma = 2.2
colour_image = np.power(colour_image, 1.0 / gamma)
# Convert to BGR format for OpenCV compatibility
self.demosaiced_img = cv2.cvtColor(colour_image.astype(np.float32), cv2.COLOR_RGB2BGR)
print(f"Demosaiced image shape: {self.demosaiced_img.shape}, dtype: {self.demosaiced_img.dtype}")

def save_image(self, output_format="16bit"):
"""Saves the demosaiced image in either 8-bit or 16-bit format."""
if self.demosaiced_img is None:
raise ValueError("Image not demosaiced. Please demosaic the image first.")

if output_format == "8bit":
# Scale to 8-bit and save
save_img = (self.demosaiced_img * 255).astype(np.uint8)
output_file = self.file_path.with_name(f"{self.file_path.stem}_unpacked_8bit.png")
cv2.imwrite(str(output_file), save_img, [cv2.IMWRITE_PNG_COMPRESSION, 0])
elif output_format == "16bit":
# Scale to 16-bit and save
save_img = (self.demosaiced_img * 65535).astype(np.uint16)
output_file = self.file_path.with_name(f"{self.file_path.stem}_unpacked_16bit.png")
cv2.imwrite(str(output_file), save_img, [cv2.IMWRITE_PNG_COMPRESSION, 0])
else:
raise ValueError("Invalid output format. Choose '8bit' or '16bit'.")

print(f"Image saved as {output_format} at {output_file}")

# Example usage:
file_path = "data/results/NC_2024-11-12/NC_1731423628.tiff"
processor = TIFFProcessor(file_path=file_path, im_height=9528, im_width=13376)
processor.load_image()
processor.unpack_image()
processor.demosaic_image()
processor.save_image(output_format="8bit") # Change to "8bit" to save as 8-bit

0 comments on commit 5efaeb0

Please sign in to comment.