Calculate Image PCA
SUMMARY
Calculate Image PCA computes principal component analysis on a binary mask and returns a visualization.
PCA finds the centroid, eigenvectors, eigenvalues, and principal angle of the mask. The output image shows the mask with the centroid and principal axis overlaid, useful for shape orientation analysis, alignment, and morphological characterization of binary regions.
Use this Skill when you want to analyze the principal orientation and centroid of a binary mask.
The Skill
from telekinesis import pupil
result_image = pupil.calculate_image_pca(image=image)Example
Input Image

Original binary mask
Output

Mask with centroid and principal axis overlaid
The Code
from telekinesis import pupil
from datatypes import io
import pathlib
from loguru import logger
DATA_DIR = pathlib.Path("path/to/telekinesis-data")
# Load image
filepath = str(DATA_DIR / "images" / "gear_with_texture.jpg")
image = io.load_image(filepath=filepath, as_binary=True, binary_method="fixed")
logger.success(f"Loaded image from {filepath}")
# Compute PCA on binary mask
binary_mask = image.to_numpy()
centroid, eigenvectors, eigenvalues, angle = pupil.calculate_image_pca(image=image)
centroid = centroid.to_numpy()
eigenvectors = eigenvectors.to_numpy()
eigenvalues = eigenvalues.to_numpy()
angle = angle.valueThe Explanation of the Code
The code begins by importing the necessary modules: pupil for image processing operations, io for data handling, pathlib for path management, and loguru for logging.
from telekinesis import pupil
from datatypes import io
import pathlib
from loguru import loggerNext, a binary mask is loaded from a .jpg file using the io.load_image function. The image is loaded as binary using as_binary=True, which is required for PCA computation on foreground pixels.
DATA_DIR = pathlib.Path("path/to/telekinesis-data")
# Load binary image
filepath = str(DATA_DIR / "images" / "gear_with_texture.jpg")
image = io.load_image(filepath=filepath, as_binary=True, binary_method="fixed")The main operation uses the calculate_image_pca Skill from the pupil module. This Skill computes principal component analysis on a binary mask, returning centroid, eigenvectors, eigenvalues, and principal angle for shape orientation analysis.
centroid, eigenvectors, eigenvalues, angle = pupil.calculate_image_pca(image=image)Finally, the results are converted to NumPy arrays using to_numpy() for further processing, visualization, or downstream tasks.
centroid = centroid.to_numpy()
eigenvectors = eigenvectors.to_numpy()
eigenvalues = eigenvalues.to_numpy()
angle = angle.valueThis operation is particularly useful in robotics and vision pipelines for shape orientation analysis, alignment, morphological characterization, and quality inspection, where analyzing the principal orientation and centroid of a binary mask is required.
Running the Example
Runnable examples are available in the Telekinesis examples repository. Follow the README in that repository to set up the environment. Once set up, you can run this specific example with:
cd telekinesis-examples
python examples/pupil_examples.py --example calculate_image_pcaHow to Tune the Parameters
The calculate_image_pca Skill has no tunable parameters. It operates on a binary mask and returns a visualization image with the centroid and principal axis overlaid. Ensure the input is binary (foreground/background) for correct PCA computation.
Where to Use the Skill in a Pipeline
Calculate Image PCA is commonly used in the following pipelines:
- Shape orientation analysis - Determine the principal axis of binary regions
- Alignment - Align objects by their principal direction
- Morphological characterization - Extract centroid and orientation for shape descriptors
- Quality inspection - Verify part orientation in manufacturing
Related skills to build such a pipeline:
calculate_image_centroid: Get centroid only (no visualization)filter_image_using_morphological_erode: Clean binary masks before PCAalign_image_circle_center: Alternative alignment method using circles
When Not to Use the Skill
Do not use Calculate Image PCA when:
- You're working with grayscale or color images (Convert to binary first)
- You need 3D orientation (Use point cloud PCA instead)
- The mask has multiple disconnected regions (PCA analyzes the combined region)

