Question

In image processing, specifically in fingerprint recognition, I have to apply a two-dimensional low pass filter with a unit integral.

What does this unit integral mean? Also, if I choose a Gaussian filter, what sigma to use?

Was it helpful?

Solution

Unit integral means that the total area of the mask or kernel should be 1. For example, a 3 x 3 averaging filter means that every coefficient in your mask should be 1/9. When you sum up all of the elements in the mask it adds to 1.

The Gaussian filter inherently has a unit integral / unit area of 1. If you use MATLAB, the fspecial command with the gaussian flag has its mask normalized.

However, if you want to create the Gaussian mask yourself, you can use the following equation:

Gaussian 2D Equation

Bear in mind that (x,y) are the locations inside the mask with respect to the centre. As such, if you have a 5 x 5 mask, then at row = 2, col = 2, x = 0 and y = 0. However, the above equation does not generate a unit area of 1. It is theoretically equal to 1 if you integrate over the entire 2D plane. Because we are truncating the Gaussian function, the area is not 1. As such, once you generate all of your coefficients, you need to make sure that the total area is 1 by summing up every single element in the mask. Then, you take this number and divide every single element in your mask by this number. In fact when you generate the Gaussian mask, it's not important to multiply the exponential term by the scale factor in the equation. By ensuring that the sum of the mask is equal to 1, the scale is effectively removed. You can just use the exponential term instead to shave off some calculations.

In terms of the sigma that is completely up to you. Usually people go with the half width of 3*sigma rule, so the total width spanning from left to right in 1D is 6*sigma + 1 (including the centre). In order to figure out what sigma you want specifically, people figure out how wide the smallest feature is in the image, set that as the width then figure out the sigma from there. For example, if the biggest width is 13, then rearranging for sigma in the equation gives you 2. In other words:

13 = 6*sigma + 1
12 = 6*sigma
sigma = 2

As such, you'd set your sigma to 2 and make the mask 13 x 13. For more information about the 3*sigma rule, check out my post on the topic here: By which measures should I set the size of my Gaussian filter in MATLAB?

Once you create that mask, use any convolution method you wish to Gaussian filter your image.

Here's another post that may help you if you can use MATLAB.

How to make a Gaussian filter in Matlab


If you need to use another language like C or Java, then you could create a Gaussian mask in the following way:

C / C++

#define WIDTH 13

float sigma = ((float)WIDTH - 1.0f) / 6.0f;
int half_width = (int)(WIDTH / 2.0);
float mask[WIDTH][WIDTH];
float scale = 0.0f;
for (int i = -half_width; i <= half_width; i++) {
    for(int j = -half_width; j <= half_width; j++) {
         mask[i+half_width][j+half_width] = expf( -((float)(i*i + j*j) / (2.0*sigma*sigma)) );
         scale += mask[i+half_width][j+half_width];
    }
}

 for (int i = 0; i < WIDTH; i++)
      for (int j = 0; j < WIDTH; j++)
            mask[i][j] /= scale;

Java

 int WIDTH = 13;     
 float sigma = ((float)WIDTH - 1.0f) / 6.0f);
 int half_width = Math.floor((float)WIDTH / 2.0f);
 float[][] mask = new float[WIDTH][WIDTH];
 float scale = 0.0f;

 for (int i = -half_width; i <= half_width; i++) {
     for (int j = -half_width; j <= half_width; j++) {
          mask[i+half_width][j+half_width] = (float) Math.exp( -((double)(i*i + j*j) /  (2.0*sigma*sigma)) );
          scale += mask[i+half_width][j+half_width];
     }
 }

 for (int i = 0; i < WIDTH; i++)
      for (int j = 0; j < WIDTH; j++)
            mask[i][j] /= scale;

As I noted before, notice that in the code I didn't have to divide by 2*pi*sigma^2. Again, the reason why is because when you normalize the kernel, this constant factor gets cancelled out anyway, so there's no need to add any additional overhead when computing the mask coefficients.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top