Implemented function to compute gradient of image density.