MRI inhomogeneity correction based on image gradient