Load TPS Files#
TPS is a standard format for landmark data, commonly used with tpsDig software.
Read landmarks from a TPS file#
import tempfile
from ktch.io import read_tps, write_tps
# Create a sample TPS file for demonstration
tps_content = """LM=4
0.0 0.0
1.0 0.0
1.0 1.0
0.0 1.0
ID=specimen_001
LM=4
0.1 0.0
1.1 0.0
1.0 1.1
0.0 1.0
ID=specimen_002
"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.tps', delete=False) as f:
f.write(tps_content)
tps_path = f.name
# Read the TPS file
landmarks = read_tps(tps_path)
print(f"Shape: {landmarks.shape}") # (n_specimens, n_landmarks, n_dim)
Shape: (2, 4, 2)
Write landmarks to a TPS file#
import numpy as np
# Prepare data
landmarks = np.array([
[[0, 0], [1, 0], [1, 1], [0, 1]],
[[0.1, 0], [1.1, 0], [1, 1.1], [0, 1]],
], dtype=float)
# Write to TPS file
with tempfile.NamedTemporaryFile(mode='w', suffix='.tps', delete=False) as f:
output_path = f.name
write_tps(output_path, landmarks)
# Verify
coords = read_tps(output_path)
print(f"Written and read back: {coords.shape}")
Written and read back: (2, 4, 2)
See also
Use Built-in Datasets for built-in example datasets
Generalized Procrustes analysis from TPS file for a complete TPS workflow