Gaussian kernel in image processing

AI Thread Summary
The discussion focuses on understanding the Gaussian kernel in image processing, specifically the origin and calculation of the X and Y matrices used in the Gaussian filter. Participants clarify that X and Y represent distances from the center pixel and provide examples for both 3x3 and 5x5 matrices. There is also a debate about the accuracy of calculated kernel values, emphasizing the importance of normalization to ensure the kernel sums to 1.0 for proper signal processing. The conversation highlights the need for clear interpretation of Gaussian approximations and the impact of matrix dimensions on the resulting values.
PainterGuy
Messages
938
Reaction score
73
I was reading the following webpage on Gaussian kernel but couldn't understand few details: https://www.imageeprocessing.com/2014/04/gaussian-filter-without-using-matlab.html . Would really appreciate it if you could guide me. Thanks in advance!

Here you can find the high-res screenshot of the webpage: https://imagizer.imageshack.com/img922/8473/QIx8L3.jpg

Question 1:
Where do the matrices X and Y come from?

Question 2:
I could see how the value "94.9296" is calculated but I cannot see how other values in the matrix are calculated?
 

Attachments

  • gaussian_kernel.jpg
    gaussian_kernel.jpg
    31.3 KB · Views: 204
Engineering news on Phys.org
PainterGuy said:
Question 1:
Where do the matrices X and Y come from?
I had no clear idea either, but it is really easy nowadays to find some explanation
https://setosa.io/ev/image-kernels/
If still unclear:
Consult a textbook​
If still unclear:​
Ask a specific question on PF​

Still thinking on answer 2 (but I'm at least as lazy as I think you are :smile: )

----

Re first link: it's actually more an example to show why you shouldn't filter like that !
(other comment: for a club that faetures image processing, they do rather a poor job ! And the web page isn't much of a confidence building advert either !)
Re second link: not that high res !
1613138171597.png

PainterGuy said:
Here you can find the high-res screenshot of the webpage:
I can't and after a few months probably no one can. No loss.

No need to think long about
PainterGuy said:
Question 2:
I could see how the value "94.9296" is calculated but I cannot see how other values in the matrix are calculated?
You didn't even try to move the red box one column to the left and repeat the matrix multiplication ? Or one row down ? Or one column to the left and one row down ?

1613138311056.png

That should give you three more values. For more, you need more
1613138569486.png
:wink:
##\ ##
 
Last edited:
  • Like
Likes PainterGuy
Thanks a lot for the reply! :smile:

I'd say my Question 1 was quite specific. Where do X and Y come from?

Suppose both X and Y have 5x5 dimensions instead of 3x3. I don't think I can get the kernel below. I've looked up around and can't see how the following kernel is derived using the Gaussian equation
1613222385646.png
.

1613222151892.png

Source: https://en.wikipedia.org/wiki/Kernel_(image_processing)

Thanks for the help, in advance!
 
The X and Y matrices are just the distances in the x and y directions from the center pixel:
If they were 5x5 matrices, X and Y would be <br /> X = \begin{matrix} -2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\ \end{matrix}<br /> \,\,\,\,Y = \begin{matrix} -2&amp;-2&amp;-2&amp;-2&amp;-2\\-1&amp;-1&amp;-1&amp;-1&amp;-1\\0&amp;0&amp;0&amp;0&amp;0\\1&amp;1&amp;1&amp;1&amp;1\\2&amp;2&amp;2&amp;2&amp;2\\ \end{matrix} Do you see the pattern?
 
  • Like
Likes PainterGuy
PainterGuy said:
I'd say my Question 1 was quite specific. Where do X and Y come from?
It was. I just didn't interpret it the way you intended.

x is the step in the x-direction
[edit]never mind ! Thanks @phyzguy !
 
  • Like
Likes PainterGuy
PainterGuy said:
Suppose both X and Y have 5x5 dimensions instead of 3x3. I don't think I can get the kernel below. I've looked up around and can't see how the following kernel is derived using the Gaussian equation View attachment 277892.

View attachment 277891
Source: https://en.wikipedia.org/wiki/Kernel_(image_processing)

phyzguy said:
The X and Y matrices are just the distances in the x and y directions from the center pixel:
If they were 5x5 matrices, X and Y would be <br /> X = \begin{matrix} -2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\ \end{matrix}<br /> \,\,\,\,Y = \begin{matrix} -2&amp;-2&amp;-2&amp;-2&amp;-2\\-1&amp;-1&amp;-1&amp;-1&amp;-1\\0&amp;0&amp;0&amp;0&amp;0\\1&amp;1&amp;1&amp;1&amp;1\\2&amp;2&amp;2&amp;2&amp;2\\ \end{matrix} Do you see the pattern?

Thank you!

In my last message I made a mistake. I wanted to ask about 5x5 Gaussian kernel as shown below instead of 3x3.

1613399124233.png
I did the calculation but my values for 5x5 kernel are different. Where am I going wrong? Could you please help me?

1613399527187.png

1613399659386.png
 
PainterGuy said:
Where am I going wrong?
Perhaps in the interpretation of the word 'approximation'?
 
  • Like
Likes PainterGuy
BvU said:
Perhaps in the interpretation of the word 'approximation'?

But my middle number is 40.742 and in the standard 5x5 kernel it's 36. Isn't the difference too much for an approximation? Thank you.
 
Last edited:
You'll be hard pressed to see any difference between the two !
 
  • #10
Ideally you want the kernel to sum to 1.0, so you are only moving signal around, not gaining or losing signal. The approximation in Post #4 does that, but your calculation doesn't. If you continue the Gaussian out far enough, it will sum to 1.0, but since you truncated it, it doesn't.
 
  • Like
Likes PainterGuy and BvU
  • #11
phyzguy said:
The X and Y matrices are just the distances in the x and y directions from the center pixel:
If they were 5x5 matrices, X and Y would be <br /> X = \begin{matrix} -2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\-2&amp;-1&amp;0&amp;1&amp;2\\ \end{matrix}<br /> \,\,\,\,Y = \begin{matrix} -2&amp;-2&amp;-2&amp;-2&amp;-2\\-1&amp;-1&amp;-1&amp;-1&amp;-1\\0&amp;0&amp;0&amp;0&amp;0\\1&amp;1&amp;1&amp;1&amp;1\\2&amp;2&amp;2&amp;2&amp;2\\ \end{matrix} Do you see the pattern?

This is a minor point but I wanted to clarify.

Normally the values increases toward the top of y-axis and become more negative toward the bottom. In the shown case, the values become negative toward the top and increase toward the bottom, as shown below.

1613467174240.png


I don't think it affects the end result but for consistency, shouldn't the X matrix be written as shown below?

1613467199984.png


phyzguy said:
Ideally you want the kernel to sum to 1.0, so you are only moving signal around, not gaining or losing signal. The approximation in Post #4 does that, but your calculation doesn't. If you continue the Gaussian out far enough, it will sum to 1.0, but since you truncated it, it doesn't.

I'm sorry but I didn't exactly get your point. Do you mean that if the dimensions of X and Y were, say, 1000x1000 then it would sum to 1.0?

Thanks for the help, in advance!
 
  • #12
Which direction is + or - is just a convention, so you can do it either way. But since the function squares x and y, it is symmetric in x and y, so it makes no difference which way you define it. On the other point, the \frac {1}{2 \pi \sigma^2} normalizes the Gaussian function so that it integrates to 1. To do it properly, instead of each pixel (for example x=1, y=2) having the value \frac {1}{2 \pi \sigma^2} \exp (-(1^2+2^2)/2 \sigma^2), it should have the value \frac {1}{2 \pi \sigma^2}\int_{1.5}^{2.5} \int_{0.5}^{1.5} \exp (-(x^2+y^2)/2 \sigma^2) dx dy. Then if you did that and the matrices are large enough (even 10x10 should be enough) then the matrix values should sum to 1.0. You might try evaluating these and see if it comes closer to the integer values in your earlier post.
 
  • Like
Likes PainterGuy
Back
Top