I'm looking for an image filter that does the following operation:

In my first image, I have two spheres. After applying the filter, I'd like to have them "glued together" which something that kind of resembles surface tension.


  • The image will be 2D/3D, with the signed distance to the surface of the spheres encoded as grayscale.
  • It would be great if it is something that's already in Numpy / Scipy.
  • I know that people do similiar stuff on meshes, but I'd like to have it as an image filter.

Example images (sorry for the different formats):

enter image description here Image 2 - after filtering

  • $\begingroup$ morphological dilation might appeal. $\endgroup$ Commented Sep 16, 2017 at 6:48

2 Answers 2


Morphological Closing.

A morphological closing is a combination of dilation followed by erosion; typical image processing operations available in most image processing libraries.

SciPy has a "simple" binary closing method which does this on binary images. By experimenting with different structuring elements you can close smaller or bigger gaps.


I think that you could use a Gaussian Filter.

The following Python code does something similar to what you show in your images.

from __future__ import division
import numpy as np
from scipy.ndimage.filters import gaussian_filter, laplace
import matplotlib.pyplot as plt

y, x = np.mgrid[-2:2:101j, -2:2:101j]
z = np.maximum(0, 0.2 - (x + 0.5)**2 - y**2) +\
    np.maximum(0, 0.2 - (x - 0.5)**2 - y**2)
z2 = gaussian_filter(z, sigma=7)

plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.contourf(x, y, z)
plt.subplot(1, 2, 2)
plt.contourf(x, y, z2)

The results is the following

enter image description here

The caveat is the selection of sigma. That parameter would determine how spread is the Gaussian filter.

  • $\begingroup$ It does agglomerate the nearby bodies, but it does look more like a diffusion than a surface tension phenomena. Surface tension would in a way "conserve the volume" of the original spheres while maintaining a sharp interface. Still a good starting point. $\endgroup$
    – BlaB
    Commented Sep 15, 2017 at 12:38
  • $\begingroup$ @BlaB, that's true, but to generate a new surface you would need to move material from one side to the other. $\endgroup$
    – nicoguaro
    Commented Sep 15, 2017 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.