Skip to content

Instantly share code, notes, and snippets.

@kwinkunks
Created September 26, 2019 15:57
Show Gist options
  • Save kwinkunks/769e39e8314b5479842a77b18e4e3eda to your computer and use it in GitHub Desktop.
Save kwinkunks/769e39e8314b5479842a77b18e4e3eda to your computer and use it in GitHub Desktop.
Implementing a 3 x 3 boxcar filter over a 2D image in pure NumPy
import numpy as np
arr = np.random.randint(0, 256, (200, 200), dtype=np.uint8)
def func(arr1d):
kernel = np.ones(3) / 3
return np.convolve(arr1d, kernel, mode='same')
first_pass = np.apply_along_axis(func, axis=0, arr=arr)
final_result = np.apply_along_axis(func, axis=1, arr=first_pass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment