ENH Faster perimeter computation

Measured a 30% speedup on a 640x640 image. Fundamentally, this makes
fewer passes over the image; so it should never be worse that the
previous implementation.