TorchEFD is a python package to compute and apply elliptic fourier descriptors with PyTorch. Elliptic Fourier descriptors (EFDs) [1] represent a closed contour and are useful for shape analysis, matching, recognition and augmentation. This post gives a short overview and documentation of the package.
The code is adapted from PyEFD.
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)
Function | Description |
|
Compute EFDs, see example |
|
Reconstruct contour, see example |
|
Normalize the EFDs to have zero phase shift from the first major axis. |
|
Normalize the EFDs to be rotation invariant by aligning the semi-major axis with the x-axis. |
|
Normalize the scale of the EFDs. |
|
Normalize phase, rotation and scale of EFDs. |
Function | Description |
|
Compute EFDs for tangent or normal function |
|
Compute tangent/normal/tangrad vectors |
|
Compute (signed) curvature of a contour. |
|
Compute speed (magnitude of tangents) or arc lengths |
|
Compute total length of contour |
|
Compute area of polygon bounded by contour using the shoelace method |
Function | Description |
|
Scale or rotate contour by transforming EFDs |
|
Apply gaussian smoothing with standard deviation sigma in fourier space |
Function | Description |
|
Draw image of mask/perimeter of the polygon bounded by the contour.
Requires scikit-image . |
|
Compute signed distance function for a contour.
Requires scipy . |
|
Plot a contour. Extra args/kwargs will be passed to Axes.plot .
Requires matplotlib . |
[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