implementation of the algorithm from the PAMI paper 1995