Gaussian kernel in image processing

Click For Summary

Discussion Overview

The discussion revolves around the Gaussian kernel in image processing, specifically focusing on the derivation and calculation of the matrices X and Y used in the Gaussian filter. Participants seek clarification on the origins of these matrices and the computation of kernel values, exploring both theoretical and practical aspects of Gaussian filtering.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants inquire about the origin of the matrices X and Y, with one suggesting they represent distances from the center pixel.
  • There is a discussion about the calculation of kernel values, with one participant noting a specific value (94.9296) but struggling to understand how other values are derived.
  • One participant proposes that if X and Y were 5x5 matrices, they would follow a specific pattern of values, while another participant questions the derivation of the Gaussian kernel from the Gaussian equation.
  • Concerns are raised about the approximation of kernel values, with one participant noting discrepancies between their calculated values and standard values.
  • Another participant emphasizes the importance of the kernel summing to 1.0 to avoid signal gain or loss, suggesting that truncation of the Gaussian function affects this property.
  • There is a clarification regarding the convention of positive and negative values in the matrices, with a suggestion that symmetry in the Gaussian function makes the direction arbitrary.
  • One participant mentions that proper normalization of the Gaussian function requires integration over a defined range, which could lead to values summing to 1.0 if matrices are sufficiently large.

Areas of Agreement / Disagreement

Participants express differing views on the interpretation of the Gaussian kernel and its approximation, with no consensus reached on the exact calculations or derivations. The discussion remains unresolved regarding the discrepancies in calculated values and the implications of truncation.

Contextual Notes

Some participants highlight limitations in understanding due to missing assumptions or unclear definitions related to the Gaussian kernel and its application in image processing.

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: 227
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: PainterGuy

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 152 ·
6
Replies
152
Views
11K