Code for fast morphological filtering