Gradient Based Mrf Learning For Image Restoration And Segmentation