Optimal sigma for Gaussian filtering of an image?
Asked Answered
H

4

17

When applying a Gaussian blur to an image, typically the sigma is a parameter (examples include Matlab and ImageJ).

How does one know what sigma should be? Is there a mathematical way to figure out an optimal sigma? In my case, i have some objects in images that are bright compared to the background, and I need to find them computationally. I am going to apply a Gaussian filter to make the center of these objects even brighter, which hopefully facilitates finding them. How can I determine the optimal sigma for this?

Hollywood answered 30/6, 2010 at 12:36 Comment(0)
G
22

There's no formula to determine it for you; the optimal sigma will depend on image factors - primarily the resolution of the image and the size of your objects in it (in pixels).

Also, note that Gaussian filters aren't actually meant to brighten anything; you might want to look into contrast maximization techniques - sounds like something as simple as histogram stretching could work well for you.

edit: More explanation - sigma basically controls how "fat" your kernel function is going to be; higher sigma values blur over a wider radius. Since you're working with images, bigger sigma also forces you to use a larger kernel matrix to capture enough of the function's energy. For your specific case, you want your kernel to be big enough to cover most of the object (so that it's blurred enough), but not so large that it starts overlapping multiple neighboring objects at a time - so actually, object separation is also a factor along with size.

Since you mentioned MATLAB - you can take a look at various gaussian kernels with different parameters using the fspecial('gaussian', hsize, sigma) function, where hsize is the size of the kernel and sigma is, well, sigma. Try varying the parameters to see how it changes.

Gordongordy answered 30/6, 2010 at 13:9 Comment(6)
@tzaman: can you expand on how the optimal sigma depends on the resolution of the image and the size of my objects in pixels? or point me in the direction of readings if you can. that's exactly what i'm looking for. also, i misspoke about brightening; i meant to say i want the center of my objects to be relatively bright to everything else.Hollywood
@hatorade: I've expanded my answer; hope it clarifies things.Gordongordy
@tzaman: on a website (imaging.mrc-cbu.cam.ac.uk/imaging/PrinciplesSmoothing) I read that the Full Width at Half Maximum (FWHM) measure has an equation. Does this concept hold for the kernel? As in, in ImageJ or Matlab, does the "FWHM" of the kernel relate to sigma in the same way? The reason I ask is because usually the parameters are the kernel dimensions, and sigma, but i keep reading that sigma effects the kernel size...Hollywood
@hatorade: Yes - essentially, FWHM is one way to measure the "spread" of the function itself; so it'll be larger for higher sigma. You could, for example, use it to help determine how big the matrix size for a given kernel should be. While in some sense you can pick dimension and sigma separately, in reality the dimension has to be tied to the sigma for it to be meaningful - it needs to be big enough to preserve the shape of the curve; if you truncate it too much, it stops being a Gaussian blur and more or less turns into a simple average-filter.Gordongordy
@tzaman: right. do you know if there is an equation for the width of the base of the normal curve, and not at half max? essentially, i know how big i want the kernel to be, but I don't know how to calculate sigma from that (so going from kernel size -> sigma instead of sigma -> kernel size)Hollywood
@hatorade: Gaussians are essentially infinite, although they drop down to negligible values as you go further out, so there's no "limit" on the width of the base. Twice the FWHM or something like that sounds reasonable. Also, even at a particular kernel size, you can vary the sigma meaningfully (there's no "one best sigma" for any given kernel) - think of it as multiplying your Gaussian filter with a box filter of those dimensions.Gordongordy
H
10

I use this convention as a rule of thumb. If k is the size of kernel than sigma=(k-1)/6 . This is because the length for 99 percentile of gaussian pdf is 6sigma.

Hockett answered 25/5, 2020 at 13:2 Comment(2)
This is right, but I would suggest the opposite approach: first determine the best sigma, then find the size of the kernel such that it contains the 6 sigma. This is because the kernel size is a discrete quantity (incremented in steps of 2, whereas sigma is a continuous variable and can therefore be adjusted much more finely. k=2*ceil(3*sigma)+1.Skylar
@CrisLuengo Makes perfect senseHockett
U
1

You have to find a min/max of a function G such that G(X,sigma) where X is a set of your observations (in your case, your image grayscale values) , This function can be anything that maintain the "order" of the intensities of the iamge, for example, this can be done with the 1st derivative of the image (as G),

fil = fspecial('sobel');
im = imfilter(I,fil);
imagesc(im);
colormap = gray;

this gives you the result of first derivative of an image, now you want to find max sigma by maximzing G(X,sigma), that means that you are trying a few sigmas (let say, in increasing order) until you reach a sigma that makes G maximal. This can also be done with second derivative.

Ungual answered 30/1, 2012 at 18:3 Comment(0)
P
0

Given the central value of the kernel equals 1 the dimension that guarantees to have the outermost value less than a limit (e.g 1/100) is as follows:

double limit = 1.0 / 100.0;
size = static_cast<int>(2 * std::ceil(sqrt(-2.0 * sigma * sigma * log(limit))));
if (size % 2 == 0)
{
    size++;
}       
Peaked answered 27/11, 2022 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.