Skip to content

Instantly share code, notes, and snippets.

@william-silversmith
Last active July 10, 2018 19:18
Show Gist options
  • Save william-silversmith/a409cf47674c5dc670c224f8109e9c2e to your computer and use it in GitHub Desktop.
Save william-silversmith/a409cf47674c5dc670c224f8109e9c2e to your computer and use it in GitHub Desktop.
COUNTLESS variant that treats zero as "background" that doesn't count for the purposes of choosing the mode.
def countless_stippled(data):
"""
Vectorized implementation of downsampling a 2D
image by 2 on each side using the COUNTLESS algorithm
that treats zero as "background" that doesn't count
for the purposes of choosing the mode.
data is a 2D numpy array with even dimensions.
"""
sections = []
# This loop splits the 2D array apart into four arrays that are
# all the result of striding by 2 and offset by (0,0), (0,1), (1,0),
# and (1,1) representing the A, B, C, and D positions from Figure 1.
factor = (2,2)
for offset in np.ndindex(factor):
part = data[tuple(np.s_[o::f] for o, f in zip(offset, factor))]
sections.append(part)
a, b, c, d = sections
ab_ac = a * ((a == b) | (a == c)) # PICK(A,B) or PICK(A,C)
ab_ac |= b * (b == c) # (PICK(A,B) or PICK(A,C)) or PICK(B,C)
nonzero = a + (a == 0) * (b + (b == 0) * c)
return ab_ac + (ab_ac == 0) * (d + (d == 0) * nonzero) # AB or AC or BC or D or A or B or C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment