# How to calculate gradients with a gaussian kernel

**URL:** <https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107>\
**Category:** C++\
**Tags:** programming\
**Created:** [September 8, 2022, 6:23am UTC](https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107 "2022-09-08T06:23:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [September 8, 2022, 6:23am UTC](https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107/1 "2022-09-08T06:23:39Z")

</div>

Hello,

As told by the title, I want to calculate image gradients with a gaussian kernel. It would be convoluting the image with the kernel in general. However, I also want to get gx, gy and the magnitude with sqrt(gx.^2+gy.^2).

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 8, 2022, 10:43am UTC](https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107/2 "2022-09-08T10:43:26Z")

</div>

ok, so you _want_ to do something. assuming you want to turn that desire into action, did you _try_ to do it?

---

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [September 9, 2022, 2:07am UTC](https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107/3 "2022-09-09T02:07:03Z")

</div>

I tried it in Matlab getting simply a gaussian kernel of 3\*3 for example. It is simply a symmetric matrix leading to the same response in both x and y direction. This is definitely a mistake so that I posted this to see if there is a function in OpenCV doing this.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 9, 2022, 9:41am UTC](https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107/4 "2022-09-09T09:41:26Z")

</div>

there’s `cv::filter2D`… but you probably already know about that function.

for direction, you need to apply two filters, one for gradients in X and one for gradients in Y direction. commonly, sobel kernels are used for that.

---

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [September 10, 2022, 3:09pm UTC](https://forum.opencv.org/t/how-to-calculate-gradients-with-a-gaussian-kernel/10107/5 "2022-09-10T15:09:17Z")

</div>

Thanks, @crackwitz. I have got that. I found some codes in git using differential to replace derivative, that 's why I always get a symmetric matrix.
