I think this function can and should be made nd *and* take a connectivity parameter (so you can use 6 neighbors, 18, or 26; I prefer to refer to these as connectivity = 1, 2, 3), by using scipy.ndimage.generate_binary_structure. Then it might be useful as a public function.