Sobel filter does exactly the same (here: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html#sobel-derivatives you can find more on that) I'd suggest replacing getGradientX and getGradientY with Sobel.