Skip to main content

Elliptic Fourier Descriptors in PyTorch

Project description

Elliptic Fourier Descriptors in PyTorch

Elliptic Fourier descriptors (EFDs) [1] represent a closed contour and are useful for shape analysis, matching, recognition and augmentation.

Adapted from PyEFD.

Install

pip install torch-efd

Example

import torch
from torch_efd import (
    compute_efds,
    integrate_total_length,
    compute_curvature,
    rotate_efds,
    reconstruct_efds
)

# 2d contour of a square
contour = torch.tensor(
    [[1, -1], [1, 1], [-1, 1], [-1, -1], [1, -1]],
    dtype=torch.float32,
)

# compute EFD coefficents
# higher order increases precision
# set normalize=True for scale/phase/rotation-invariant descriptors
efds = compute_efds(contour, order=20)

# compute length and curvature of shape
length = integrate_total_length(efds)
curvature = compute_curvature(efds, ts=100)

# transform shape
efds = rotate_efds(efds, angle=torch.pi / 4)

# back to coordinates
reconstructed = reconstruct_efds(efds, ts=100)

API

Basics

Function Description
compute_efds(
    contour: Tensor,
    order: int,
    normalize: bool = False,
) -> Tensor
Compute EFDs, see example
reconstruct_efds(
    efds: Tensor,
    ts: Tensor | int
) -> Tensor
Reconstruct contour, see example
normalize_phase(efds: Tensor) -> Tensor
Normalize the EFDs to have zero phase shift from the first major axis.
normalize_rotation(efds: Tensor) -> Tensor
Normalize the EFDs to be rotation invariant by aligning the semi-major axis with the x-axis.
normalize_scale(efds: Tensor) -> Tensor
Normalize the scale of the EFDs.
normalize_efds(efds: Tensor) -> Tensor
Normalize phase, rotation and scale of EFDs.

Geometry

Function Description
derive_tangent_efds(efds: Tensor) -> Tensor
derive_normal_efds(efds: Tensor) -> Tensor
Compute EFDs for tangent or normal function
reconstruct_tangents(
    efds: Tensor,
    ts: Tensor | int,
    normed: bool = False,
) -> Tensor

reconstruct_normals(
    efds: Tensor,
    ts: Tensor | int,
    normed: bool = False,
) -> Tensor

reconstruct_tangrads(
    efds: Tensor,
    ts: Tensor | int,
    normed: bool = False,
) -> Tensor
Compute tangent/normal/tangrad vectors
compute_curvature(
    efds: Tensor,
    ts: Tensor | int,
    signed: bool = True,
) -> Tensor
Compute (signed) curvature of a contour.
compute_speed(
    efds: Tensor,
    ts: Tensor | int
) -> Tensor

compute_arclens(
    efds: Tensor,
    ts: Tensor | int
) -> Tensor
Compute speed (magnitude of tangents) or arc lengths
integrate_total_length(
    efds: Tensor,
    ts: Tensor | int = 100
) -> Tensor
Compute total length of contour
compute_area(
    efds: Tensor,
    ts: Tensor | int = 100
) -> Tensor
Compute area of polygon bounded by contour using the shoelace method

Transform

Function Description
scale_efds(
    efds: Tensor,
    scale: float | Tensor
) -> Tensor

rotate_efds(
    efds: Tensor,
    angle: float | Tensor
) -> Tensor
Scale or rotate contour by transforming EFDs
smooth_efds(
    efds: Tensor,
    sigma: float,
    normalize: bool = False
) -> Tensor
Apply gaussian smoothing with standard deviation sigma in fourier space

Render

Function Description
draw_mask(
    efds: Tensor,
    shape: tuple[int, int],
    n_points: int
) -> Tensor

draw_perimeter(
    efds: Tensor,
    shape: tuple[int, int],
    n_points: int
) -> Tensor
Draw image of mask/perimeter of the polygon bounded by the contour. Requires scikit-image.
compute_sdf(
    efds: Tensor,
    shape: tuple[int, int],
    n_points: int
) -> Tensor
Compute signed distance function for a contour. Requires scipy.
plot_efds(
    efds: Tensor,
    ts: int | Tensor = 100,
    ax: Axes | None = None,
    *args,
    **kwargs,
)
Plot a contour. Extra args/kwargs will be passed to Axes.plot. Requires matplotlib.

References

[1] F. Kuhl and C. P. Giardina. Elliptic Fourier Features of a Closed Contour. Computer Graphics and Image Processing 18, 1982, 236-258. https://www.sci.utah.edu/~gerig/CS7960-S2010/handouts/Kuhl-Giardina-CGIP1982.pdf

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_efd-1.0.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_efd-1.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file torch_efd-1.0.0.tar.gz.

File metadata

  • Download URL: torch_efd-1.0.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.10.6-10-MANJARO

File hashes

Hashes for torch_efd-1.0.0.tar.gz
Algorithm Hash digest
SHA256 435fd6a21a29857e848df51f2c938176cadda844aff00c722b6648a97aa8d4c8
MD5 c71480dcd56c97e9ea81457230934eb8
BLAKE2b-256 62d3392588fdef9a95baaf33805b1a53c5d8c44b58f8bf3cb3932e5534b2d43b

See more details on using hashes here.

File details

Details for the file torch_efd-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: torch_efd-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.10.6-10-MANJARO

File hashes

Hashes for torch_efd-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ec19de9f312fddd1b4f7fca5ffd416bc09c5222f63cdb1697b6544792998f45
MD5 44e4ba024745252f6676ce378ea17e3d
BLAKE2b-256 23d8e6d3c5d075216ec539855572a6a9604f497ff31c54817066cfc763ad28fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page