Which Ranking Metric Is Optimal? - With Applications in Image Retrieval and Stereo Matching