Learning Similarities for Rigid and Non-Rigid Object Detection