Example Notebook for Loading and Visualizing the Ensenso X36 Stereo + 3D XYZ Wooden Battens Dataset¶

Dataset structure:
DATASET/
LR_Image/
LR_0001.tif
LR_0002.tif
...
XYZ_Image/
XYZ_0001.tif
XYZ_0002.tif
...

Notes:¶

  • LR_Image TIFFs contain 2 channels: Left (0) and Right (1)
  • XYZ_Image TIFFs contain 3 channels: X, Y, Z coordinates in millimeters (mm)
  • Adjust DATASET_DIR if the folder is in a different location
  • This notebook shows how to load and visualize both stereo and XYZ images
In [1]:
# 1️⃣ Import libraries

import numpy as np
import tifffile as tiff
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
In [2]:
# 2️⃣ Set dataset paths
DATASET_DIR = './DATASET'  # adjust if needed
LR_DIR = os.path.join(DATASET_DIR, 'LR_Image')
XYZ_DIR = os.path.join(DATASET_DIR, 'XYZ_Image')

# 3️⃣ List example files
lr_files = sorted([f for f in os.listdir(LR_DIR) if f.endswith('.tif')])
xyz_files = sorted([f for f in os.listdir(XYZ_DIR) if f.endswith('.tif')])

print("Example LR files:", lr_files[:5])
print("Example XYZ files:", xyz_files[:5])
Example LR files: ['LR_0001.tif', 'LR_0002.tif', 'LR_0003.tif']
Example XYZ files: ['XYZ_0001.tif', 'XYZ_0002.tif', 'XYZ_0003.tif']
In [3]:
# 4️⃣ Load a stereo image
example_lr = tiff.imread(os.path.join(LR_DIR, lr_files[0]))
left_img = example_lr[..., 0]
right_img = example_lr[..., 1]

# 5️⃣ Display stereo pair
plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.imshow(left_img, cmap='gray')
plt.title('Left Image')
plt.axis('off')

plt.subplot(1,2,2)
plt.imshow(right_img, cmap='gray')
plt.title('Right Image')
plt.axis('off')

plt.show()

# 6️⃣ Load an XYZ map
example_xyz = tiff.imread(os.path.join(XYZ_DIR, xyz_files[0]))
X, Y, Z = example_xyz[...,0], example_xyz[...,1], example_xyz[...,2]

# 7️⃣ Display depth map (Z channel)
plt.figure(figsize=(6,5))
plt.imshow(Z, cmap='viridis')
plt.colorbar(label='Z (mm)')
plt.title('Depth Map (Z)')
plt.axis('off')
plt.show()

# 8️⃣ Display X, Y channels
fig, axes = plt.subplots(1,2, figsize=(10,4))
axes[0].imshow(X, cmap='plasma')
axes[0].set_title('X Coordinate')
axes[0].axis('off')

axes[1].imshow(Y, cmap='plasma')
axes[1].set_title('Y Coordinate')
axes[1].axis('off')

plt.show()

# 9️⃣ Notes:
# - LR_Image TIFFs contain 2 channels: Left (0) and Right (1)
# - XYZ_Image TIFFs contain 3 channels: X, Y, Z coordinates in mm
# - Adjust DATASET_DIR if the folder is in a different location
In [4]:
# Limit to first 5 samples
n_show = min(5, len(lr_files), len(xyz_files))
print(f"Displaying {n_show} samples from dataset...")

# === Visualization loop ===
for i in range(n_show):
    lr_path = os.path.join(LR_DIR, lr_files[i])
    xyz_path = os.path.join(XYZ_DIR, xyz_files[i])
    
    # Load TIFFs
    lr_img = tiff.imread(lr_path)   # (H, W, 2)
    xyz_img = tiff.imread(xyz_path) # (H, W, 3)
    
    # Split channels
    left_img  = lr_img[..., 0]
    right_img = lr_img[..., 1]
    x_map, y_map, z_map = xyz_img[..., 0], xyz_img[..., 1], xyz_img[..., 2]
    
    # Normalize XYZ for better visualization
    def normalize(img):
        img = img.astype(np.float32)
        return (img - np.nanmin(img)) / (np.nanmax(img) - np.nanmin(img) + 1e-8)
    
    x_norm = normalize(x_map)
    y_norm = normalize(y_map)
    z_norm = normalize(z_map)
    
    # Common colormap for XYZ maps
    cmap_xyz = "inferno"
    
    # Plot Stereo + XYZ
    fig, axes = plt.subplots(2, 3, figsize=(16, 10))  # larger size
    fig.suptitle(f"Sample {i+1}: {lr_files[i]}", fontsize=16, weight="bold")
    
    # Stereo images
    axes[0, 0].imshow(left_img, cmap="gray")
    axes[0, 0].set_title("Left Image", fontsize=13)
    axes[0, 0].axis("off")
    
    axes[0, 1].imshow(right_img, cmap="gray")
    axes[0, 1].set_title("Right Image", fontsize=13)
    axes[0, 1].axis("off")
    
    axes[0, 2].axis("off")
    
    # XYZ maps — same colormap
    im0 = axes[1, 0].imshow(x_norm, cmap=cmap_xyz)
    axes[1, 0].set_title("X Map (mm)", fontsize=13)
    plt.colorbar(im0, ax=axes[1, 0], fraction=0.046, pad=0.04)
    
    im1 = axes[1, 1].imshow(y_norm, cmap=cmap_xyz)
    axes[1, 1].set_title("Y Map (mm)", fontsize=13)
    plt.colorbar(im1, ax=axes[1, 1], fraction=0.046, pad=0.04)
    
    im2 = axes[1, 2].imshow(z_norm, cmap=cmap_xyz)
    axes[1, 2].set_title("Z Map (mm)", fontsize=13)
    plt.colorbar(im2, ax=axes[1, 2], fraction=0.046, pad=0.04)
    
    plt.tight_layout()
    plt.show()
Displaying 3 samples from dataset...
In [ ]: