Learning Affinity Functions for Image Segmentation: Combining Patch-based and Gradient-based Approaches