Gaussian kernel in image processing

In summary, the X and Y matrices come from the distances in the x and y directions from the center pixel. The values in the matrix increase toward the top of the y-axis and become more negative toward the bottom.
  • #1
PainterGuy
940
69
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: 129
Engineering news on Phys.org
  • #2
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
  • #3
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!
 
  • #4
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}
\,\,\,\,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 PainterGuy
  • #5
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
  • #6
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}
\,\,\,\,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.

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
 
  • #7
PainterGuy said:
Where am I going wrong?
Perhaps in the interpretation of the word 'approximation'?
 
  • Like
Likes PainterGuy
  • #8
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:
  • #9
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 [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}
\,\,\,\,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.

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 [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 PainterGuy

1. What is a Gaussian kernel in image processing?

A Gaussian kernel is a type of filter used in image processing to blur or smooth an image. It is based on the Gaussian distribution, which is a mathematical function that describes the probability of a random variable taking on a certain value. In image processing, the Gaussian kernel is used to reduce noise and improve the overall appearance of an image.

2. How does a Gaussian kernel work?

A Gaussian kernel works by convolving a matrix of Gaussian values with the pixel values of an image. This means that each pixel in the image is multiplied by a corresponding value in the Gaussian matrix, and then the resulting values are summed to produce the new pixel value. The Gaussian values in the matrix are weighted such that pixels closer to the center of the matrix have a higher weight, resulting in a blur or smoothing effect.

3. What are the advantages of using a Gaussian kernel?

One of the main advantages of using a Gaussian kernel is its ability to reduce noise in an image. This is because the Gaussian distribution has a natural smoothing effect, making it ideal for blurring out small variations in pixel values. Additionally, the Gaussian kernel is a non-linear filter, meaning it can preserve edges and details in an image while still smoothing out the overall appearance.

4. Are there any downsides to using a Gaussian kernel?

One potential downside of using a Gaussian kernel is that it can result in a loss of sharpness in an image. This is because the blurring effect of the Gaussian kernel can also blur out fine details and edges. Additionally, the size of the Gaussian kernel can greatly impact the processing time, with larger kernels taking longer to compute.

5. How do I choose the right size for a Gaussian kernel?

The size of a Gaussian kernel is determined by its standard deviation, which controls the amount of blur in the resulting image. A larger standard deviation will result in a larger kernel and more blur. The best way to choose the right size for a Gaussian kernel is to experiment with different standard deviation values and see which produces the desired level of smoothing without sacrificing too much detail in the image.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
831
  • Programming and Computer Science
Replies
2
Views
723
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • STEM Academic Advising
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
8K
Replies
1
Views
6K
Back
Top