Skip to main content

Python implementation of "Elliptic Fourier Features of a Closed Contour"

Project description

PyEFD

Build and Test Documentation Status image image image

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

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

1.6.0 (2021-12-09)

Added

  • Added a demo for 3D surfaces with cylindrical symmetries. (examples/example1.py)

Fixes

  • Fixes incorrectly plotted curves when no imshow has been called.
  • Fixes ugly coefficient calculation code.

1.5.1 (2021-01-22)

Added

  • return_transformation keyword on elliptic_fourier_descriptors method. Merged #11. Fixes #5.

Fixes

  • Documentation correction. Merged #12.

Removed

  • Deleted broken example script scikit_image.py.

1.4.1 (2020-09-28)

Added

  • Added CHANGELOG.md

Changed

  • Change CI from Azure Devops to Github Actions

1.4.0 (2019-07-27)

Changed

  • Merged PR #4: Vectorized contour reconstruction function

1.3.0 (2019-06-18)

Changed

  • Merged PR #2: Numpy vectorized efd
  • Moved from Travis CI to Azure Pipelines
  • Replaced rst with markdown

1.2.0 (2018-06-14)

Changed

  • Updated setup.py
  • Updated numpy requirement

Added

  • Added Pipfile
  • Ran Black on code
  • Testing on 3.6

1.1.0 (2018-06-13)

Added

  • New example for OpenCV
  • Updated documentation

1.0.0 (2016-04-19)

Changed

  • Deemed stable enough for version 1.0 release

Added

  • Created documentation.

0.1.2 (2016-02-29)

Changed

  • Testing with pytest instead of nosetests.

Added

  • Added Coveralls use.

0.1.1 (2016-02-17)

Fixed

  • Fixed MANIFEST

Added

  • Added LICENSE file that was missing.

0.1.0 (2016-02-09)

Added

  • Initial release

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

pyefd-1.6.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

pyefd-1.6.0-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyefd-1.6.0.tar.gz.

File metadata

  • Download URL: pyefd-1.6.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pyefd-1.6.0.tar.gz
Algorithm Hash digest
SHA256 628f50f395d41c82db9625834674975584fa8f829fc60cfc127937dec8f22980
MD5 d69a00269fbc33b68b877e758a66c265
BLAKE2b-256 7ffcd62c68520252715b1b06bfebf45dbbb696f06535bab9210259eb6987f9fc

See more details on using hashes here.

File details

Details for the file pyefd-1.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pyefd-1.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pyefd-1.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3c0bff21bdd00a8f1da69618ed61ea5b8a38d94c7ff5556073716aae0f66c8a2
MD5 4a5d736221941453242a97c4eaabd295
BLAKE2b-256 d26cabb8f9b6b30fa4e69addd8e6e3c384fd4ce4ad23e38bb79e6a82cd3e74a9

See more details on using hashes here.

Supported by

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