A Flexible Algorithm for Image Matching