1

They have asked me to implement a 2D Gaussian smoothing using a separable filter in Python. I don't know how to do that... In fact i don't know the difference from 1D and 2D gaussian smoothing. Where could I find more information about it?

Thanks a lot

synack
  • 1,699
  • 3
  • 24
  • 50
  • 1
    I'm not sure what your definition of basic is, but this is basically it. [An Introduction to Smoothing](http://imaging.mrc-cbu.cam.ac.uk/imaging/PrinciplesSmoothing) – Thomas Feb 16 '13 at 22:04

2 Answers2

3

About 2D filtering:

The Gaussian smoothing operator is a 2-D convolution operator that is used to `blur' images and remove detail and noise.

When working with images - convolution is an operation that calculates the new values ​​of a given pixel, which takes into account the value of the surrounding neighboring pixels. The main element is convolution kernel.

Сonvolution kernel - a matrix (of arbitrary size, most often used a square matrix (by default, 3x3)

[ ][ ][ ]
[ ][k][ ]
[ ][ ][ ]

Convolution works very simply: When calculating the new value of the selected pixel, the convolution kernel is applied to it by its center pixel. Neighboring pixels are covered with the same kernel. Next, calculate the sum of the product of the pixels in the image to the values ​​of the convolution kernel, which covered a given pixel. The resulting sum is the new value of the selected pixel. Now, if we apply the convolution to each pixel in the image, you get a certain effect, which depends on the chosen convolution kernel.

For example we have the following image:

[47][48][49][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[47][50][42][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[47][48][42][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

And you have convolution kernel:

[0][1][0]
[0][0][0]
[0][0][0]

Result is calculated in the followinf way:

result = 47*0 + 48*1 + 49*0 + 47*0 + 50*0 + 42*0 + 47*0 + 48*0 + 42*0 = 48

The result of applying our kernel to pixel with a value of 50:

[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][48][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

Here is a good explanation of Gaussian Smoothing. About 1D and 2D gaussian smoothing:

"The convolution can in fact be performed fairly quickly since the equation for the 2-D isotropic Gaussian shown above is separable into x and y components. Thus the 2-D convolution can be performed by first convolving with a 1-D Gaussian in the x direction, and then convolving with another 1-D Gaussian in the y direction. "

You can try appling convolution filter in this site.

Hope this will be helpful for you.

Ann Orlova
  • 1,338
  • 15
  • 24
1

You can look at the Python Imaging Library.

The basic idea is this. You have an image and you have something called a kernel. You then process the image with the kernel/filter. This creates a new image that was created by applying the kernel on each pixel in the source image and adding the result to the destination image.

From Image Filter with some changes to make it clearer how to create a custom kernel an process an image with it. I have not tried this, so it should be viewed as pseudocode:

import ImageFilter

customKernelData = ( 0.0, 0.0, 0.0,
                     0.0, 1.0, 0.0,
                     0.0, 0.0, 0.0 )    

customKernel = ImageFilter.Kernel( (3,3), customKernelData )
im = im.filter( customKernel )

In your case you would have to use a Kernel where the data is really 2D gauss data.

Thomas
  • 4,980
  • 2
  • 15
  • 30
  • 1
    Thomas, you do not answer the question of the separablity of gaussian 2D kernel. Separable here means that instead of applying a 2D kernel by convolution, you can apply a vector in horizontal then vertical direction independently, resulting in the very same effect on image. – CTZStef Feb 17 '13 at 01:35
  • 1
    I had forgotten about that. Then the answers to [Fastest 2D convolution or image filter in Python](http://stackoverflow.com/questions/5710842/fastest-2d-convolution-or-image-filter-in-python) are probably better. – Thomas Feb 17 '13 at 07:35