Method and apparatus for aligning images