# Does OpenCV support Gradient Magnitude

**URL:** <https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943>\
**Category:** Uncategorized\
**Created:** [April 23, 2021, 12:03pm UTC](https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943 "2021-04-23T12:03:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krakadoom](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@krakadoom](https://forum.opencv.org/u/krakadoom)\
**Post date:** [April 23, 2021, 12:03pm UTC](https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943/1 "2021-04-23T12:03:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [April 23, 2021, 12:49pm UTC](https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943/2 "2021-04-23T12:49:02Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![krakadoom](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@krakadoom](https://forum.opencv.org/u/krakadoom)\
**Post date:** [April 23, 2021, 12:54pm UTC](https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943/3 "2021-04-23T12:54:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [April 23, 2021, 1:00pm UTC](https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943/4 "2021-04-23T13:00:15Z")

</div>

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](https://docs.opencv.org/3.4/d2/d2c/tutorial_sobel_derivatives.html)

---

<div class="post-metadata">

**Author:** ![krakadoom](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@krakadoom](https://forum.opencv.org/u/krakadoom)\
**Post date:** [April 23, 2021, 3:37pm UTC](https://forum.opencv.org/t/does-opencv-support-gradient-magnitude/2943/5 "2021-04-23T15:37:47Z")

</div>

Thank you very much !!

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