Skip to content

Instantly share code, notes, and snippets.

@iskandr
Created June 3, 2013 22:11
Show Gist options
  • Save iskandr/5701919 to your computer and use it in GitHub Desktop.
Save iskandr/5701919 to your computer and use it in GitHub Desktop.
Image dilation in Python as two separate 1-D passes
def dilate_two_pass(x, window_size):
m,n = x.shape
k,l = window_size
y = np.empty_like(x)
for i in xrange(m):
for j in xrange(n):
left_idx = max(0, i-k/2)
right_idx = min(m, i+k/2+1)
currmax = x[left_idx, j]
for ii in xrange(left_idx+1, right_idx):
elt = x[ii, j]
if elt > currmax:
currmax = elt
y[i, j] = currmax
z = np.empty_like(x)
for i in xrange(m):
for j in xrange(n):
left_idx = max(0, j-l/2)
right_idx = min(n, j+l/2+1)
currmax = y[i,left_idx]
for jj in xrange(left_idx+1, right_idx):
elt = y[i,jj]
if elt > currmax:
currmax = elt
z[i,j] = currmax
return z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment