Use skimage.filter for the median filter. Much faster.