zebra zebra - 11 months ago 62
C++ Question

Is there a quick and easy way in OpenCV to compute the gradient of an image?

Using the newest OpenCV, is there an easy way to compute the gradient image of a specific cv::Mat?

Answer Source

Assuming you are referring to the typical image gradient; you can compute these quite easily with the Sobel operator as mentioned by Chris. Have a look at the Sobel Derivatives tutorial here. You may also be interested in the Laplace operator, and its tutorial.

Here is a short snippet of computing the X and Y gradients using Sobel:

cv::Mat src = ...; // Fill the input somehow.

cv::Mat Dx;
cv::Sobel(src, Dx, CV_64F, 1, 0, 3);

cv::Mat Dy;
cv::Sobel(src, Dy, CV_64F, 0, 1, 3);