Skip to content

Instantly share code, notes, and snippets.

@vpoughon
Last active November 2, 2017 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpoughon/b4afc76ce5dc681fda9d0550d41359d3 to your computer and use it in GitHub Desktop.
Save vpoughon/b4afc76ce5dc681fda9d0550d41359d3 to your computer and use it in GitHub Desktop.
skimage.filters.rank.mean and scipy.signal.convolve2d
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import convolve2d
from skimage import data
from skimage.filters import rank
image = data.coins()
K = np.ones((11, 11))
rank_mean = rank.mean(image, selem=K)
naive_convolve = np.array(convolve2d(image, K, mode="same"), dtype=float) / float(K.sum())
plots = [
("rank.mean", rank_mean, (0, 255), plt.cm.gray),
("convolve2d", naive_convolve, (0, 255), plt.cm.gray),
("Input Image", image, (0, 255), plt.cm.gray),
("abs(convolve2d - rank.mean)", abs(naive_convolve - rank_mean), (0, 1), plt.cm.jet)
]
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8),
sharex=True, sharey=True)
for ax, (title, img, (vmin, vmax), cmap) in zip(axes.ravel(), plots):
im = ax.imshow(img, cmap=cmap, vmin=vmin, vmax=vmax)
ax.set_title(title)
ax.set_adjustable('box-forced')
ax.axis('off')
plt.tight_layout()
plt.savefig("rank_mean.png")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment