Does OpenCV support Gradient Magnitude

Hello,

I’m trying to segment all bunched-up objects and I think watershed is the way to go, so I can findCoutours properly, but I am beginning to think the
Gradient Magnitude can give a properly defined Contour and highlight their external edges instead of the Watershed which will only create sub-Contours.

Does OpenCV support the Gradient Magnitude technique? I can’t find it on the website.

AFAIK there’s no function for this, but you can use

magnitude,angles = cartToPolar(gradx,grady)

to get the magnitude (and orientations). Or using the default formula:

magn = sqrt(gradx*gradx+grady*grady)

or simply add the horizontal and vertical gradients:

magn = gx+gy

(not as precise, but much faster and good enough for most applications)

But how would this work exactly given we have, does it recompute the row/col gradient of each element in a Mat ?.:

for example,

Mat m // let’s say 512 by 512 image of objects and we want contours
so in MATLAB,
grad = imgradient( m ), gives gradient magnitude of Mat m.

Gradient operators compute the horizontal and vertical derivative of an image. So you get two images, with the horizontal and vertical edges.
To get the magnitude, you have to combine these two images using one of the formulas above.
For a more detailed description follow this tutorial: OpenCV: Sobel Derivatives

Thank you very much !!

The Sobel operator does what I want. It gives identical results to the Gradient magnitude.