PyEFD
An Python/NumPy implementation of a method for approximating a contour with a Fourier series, as described in [1].
Installation
pip install pyefd
Usage
Given a closed contour of a shape, generated by e.g. scikit-image or OpenCV, this package can fit a Fourier series approximating the shape of the contour.
General usage examples
This section describes the general usage patterns of pyefd.
from pyefd import elliptic_fourier_descriptors
coeffs = elliptic_fourier_descriptors(contour, order=10)
The coefficients returned are the a_n, b_n, c_n and d_n of the following Fourier series
representation of the shape.
The coefficients returned are by default normalized so that they are rotation and size-invariant. This can be overridden by calling:
from pyefd import elliptic_fourier_descriptors
coeffs = elliptic_fourier_descriptors(contour, order=10, normalize=False)
Normalization can also be done afterwards:
from pyefd import normalize_efd
coeffs = normalize_efd(coeffs)
OpenCV example
If you are using OpenCV to generate contours, this example shows how to
connect it to pyefd.
import cv2
import numpy
from pyefd import elliptic_fourier_descriptors
# Find the contours of a binary image using OpenCV.
contours, hierarchy = cv2.findContours(
im, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Iterate through all contours found and store each contour's
# elliptical Fourier descriptor's coefficients.
coeffs = []
for cnt in contours:
# Find the coefficients of all contours
coeffs.append(elliptic_fourier_descriptors(
numpy.squeeze(cnt), order=10))
Using EFD as features
To use these as features, one can write a small wrapper function:
from pyefd import elliptic_fourier_descriptors
def efd_feature(contour):
coeffs = elliptic_fourier_descriptors(contour, order=10, normalize=True)
return coeffs.flatten()[3:]
If the coefficients are normalized, then coeffs[0, 0] = 1.0, coeffs[0, 1] = 0.0 and
coeffs[0, 2] = 0.0, so they can be disregarded when using the elliptic Fourier descriptors as features.
See [1] for more technical details.
Testing
Run tests with with Pytest:
py.test tests.py
The tests include a single image from the MNIST dataset of handwritten digits ([2]) as a contour to use for testing.
Documentation
See ReadTheDocs.
References
[1]: Frank P Kuhl, Charles R Giardina, Elliptic Fourier features of a closed contour, Computer Graphics and Image Processing, Volume 18, Issue 3, 1982, Pages 236-258, ISSN 0146-664X, http://dx.doi.org/10.1016/0146-664X(82)90034-X.
[2]: LeCun et al. (1999): The MNIST Dataset Of Handwritten Digits
Metadata
Release files for pyefd 1.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyefd-1.8.0.tar.gz | 9.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyefd-1.8.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.3 kB
Release files / pyefd-1.8.0.tar.gz
| Download URL | pyefd-1.8.0.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
af44c926fa28611b45d25085c00eac51e3611504da0da3548c732275dd0e021f
|
|
BLAKE2b-256 checksum How to use checksums |
5501d75fc87546e4fc96bcae065cf142e7c6d8608238f4f9c8466d0f62f1cd31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Release files / pyefd-1.8.0-py3-none-any.whl
| Download URL | pyefd-1.8.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b76ffa42b1cb9de89adabd534b8567af7f20a53a042b8106c050879a9abdc0c
|
|
BLAKE2b-256 checksum How to use checksums |
09a34d16dff57bb831310fef51d33472e9975c79ce262fcd83eb582045fa93a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|