Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use faster implementation of binary dilation using FFT #1638

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ General
New Features
^^^^^^^^^^^^

- ``photutils.segmentation``

- The ``SegmentationImage`` ``make_source_mask`` method now uses a
much faster implementation of binary dilation. [#1638]

Bug Fixes
^^^^^^^^^

Expand Down
14 changes: 11 additions & 3 deletions photutils/segmentation/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,8 @@ def make_source_mask(self, *, size=None, footprint=None):
return mask
else:
size = as_pair('size', size, check_odd=False)
footprint = np.ones(size)
footprint = np.ones(size, dtype=bool)
footprint = footprint.astype(bool)

if np.all(footprint):
# With a rectangular footprint, scipy grey_dilation is
Expand All @@ -1157,8 +1158,15 @@ def make_source_mask(self, *, size=None, footprint=None):
from scipy.ndimage import grey_dilation
return grey_dilation(mask, footprint=footprint)
else:
from scipy.ndimage import binary_dilation
return binary_dilation(mask, structure=footprint)
# Binary dilation is very slow, especially for large
# footprints. The following is a faster implementation
# using fast Fourier transforms (FFTs) that gives identical
# results to binary_dilation. Based on the following paper:
# "Dilation and Erosion of Gray Images with Spherical
# Masks", J. Kukal, D. Majerova, A. Prochazka (Jan 2007).
# https://www.researchgate.net/publication/238778666_DILATION_AND_EROSION_OF_GRAY_IMAGES_WITH_SPHERICAL_MASKS
from scipy.signal import fftconvolve
return fftconvolve(mask, footprint, 'same') > 0.5

@lazyproperty
def _geo_polygons(self):
Expand Down