Gaussian kernel in image processing

11 replies · 3K views · last reply
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
  • Worked example of building a 3x3 Gaussian kernel: the 2D Gaussian formula with sigma 0.6, a 3x3 kernel of values 0.0275, 0.1102 and 0.4421, and a matrix of 3x3 image pixel values multiplied element-wise by the kernel, with a 94.9269 result.
    gaussian_kernel.jpg
    31.3 KB · Views: 260
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 !
Small inline text reading: standard deviation, sigma = 0.6

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 ?

4x4 grid of pixel values: row 1: 72, 68, 88, 159; row 2: 69, 66, 87, 162; row 3: 70, 66, 83, 161; row 4: 70, 66, 78, 154. A red box outlines the inner 3x3 block of values 68, 88, 159, 66, 87, 162, 66, 83, 161.

That should give you three more values. For more, you need more
Small greyscale image excerpt with the caption text 'local region from an image' printed above it.
: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
Equation for a 2D Gaussian function: G(x, y) = 1/(2πσ²) times e to the power of minus (x² + y²) divided by 2σ².
.

Gaussian blur 3 × 3 (approximation) kernel: 1/16 times a 3 by 3 matrix with rows 1 2 1, 2 4 2, and 1 2 1.

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 [tex] X = \begin{matrix} -2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\ \end{matrix}<br /> \,\,\,\,Y = \begin{matrix} -2&-2&-2&-2&-2\\-1&-1&-1&-1&-1\\0&0&0&0&0\\1&1&1&1&1\\2&2&2&2&2\\ \end{matrix}[/tex] 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 [tex] X = \begin{matrix} -2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\ \end{matrix}<br /> \,\,\,\,Y = \begin{matrix} -2&-2&-2&-2&-2\\-1&-1&-1&-1&-1\\0&0&0&0&0\\1&1&1&1&1\\2&2&2&2&2\\ \end{matrix}[/tex] 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.

Gaussian blur 5 × 5 approximation kernel: the factor 1/256 multiplies a 5 by 5 integer matrix with rows 1 4 6 4 1, 4 16 24 16 4, 6 24 36 24 6, 4 16 24 16 4, 1 4 6 4 1.
I did the calculation but my values for 5x5 kernel are different. Where am I going wrong? Could you please help me?

Worked 5x5 Gaussian kernel calculation: matrices X and Y of coordinates, the formula G(x,y) with sigma = 1, and the resulting 5x5 grid of values, with centre value 0.15915.

Two 5x5 grids of numbers with a multiplication step: a grid of values in scientific notation multiplied by 256, equalling a grid of decimal values scaled by 1/256, with the top-left-to-bottom-right symmetric layout typical of a Gaussian kernel.
 
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 !
 
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
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 [tex] X = \begin{matrix} -2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\-2&-1&0&1&2\\ \end{matrix}<br /> \,\,\,\,Y = \begin{matrix} -2&-2&-2&-2&-2\\-1&-1&-1&-1&-1\\0&0&0&0&0\\1&1&1&1&1\\2&2&2&2&2\\ \end{matrix}[/tex] 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.

Grid matrix Y with rows of values -2, -1, 0, 1, 2 repeated across five columns; the last column is highlighted green, with arrows labelled decreasing pointing up and increasing pointing down.


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

Matrix Y, a 5 by 5 grid where each row is constant: the rows read 2, 1, 0, -1, -2 from top to bottom.


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!
 
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 [itex]\frac {1}{2 \pi \sigma^2}[/itex] 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 [itex]\frac {1}{2 \pi \sigma^2} \exp (-(1^2+2^2)/2 \sigma^2)[/itex], it should have the value [itex]\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[/itex]. 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