Home c gaussian kernel for smoothing

gaussian kernel for smoothing

Author

Date

Category

Hello, I study kompyutornoe vision despite Course The Ancient Secrets of Computer Vision and doing homework. Faced with the smoothed image using gaussian kernel.
Teacher gives a formula for approximate calculation of the matrix:

At the same time his own video lectures at 49:21 on the slide can be seen that the peak function comes practically to 1. but by the same formula can be seen that she herself is in itself can not provide this, because when sigma = 1, the fraction will be approximately equal to 1/6, which means much more than ekspanenta dolzhnabyt one unit, but if x and y = 0 then ekspanenta will be equal to 1. This means the function is approximately equal to 1/6.

The text of the homework the teacher says that it is necessary to normalize the matrix. That is, make sure that the sum of the matrix is ​​equal to 1. However, when the amount of sigma = 1 and the size of the matrix 7 * 7 (Ramer matrix homework given by sigma = * 6 + 1 in x and y) is equal to 0.999459, that is, even normalization will not increase so much the peak function.

Well homework there is a test in which you create a filter with sigma = 7, which is the teacher looks like:

I also get absolutely black square, it is clear for what reasons.

In general, I obviously something I do not understand, help to understand that.

In any case Daubal normalizitsii code and creating a gaussian matrix:

Normalization:

// im - in fact just struktora that stores Number of channels, image width and height. And the image itself is stored as a one-dimensional array of float from 0 to 1.
void l1_normalize (image im)
{
  Double Sum = 0;
  for (int chanel = 0; chanel & lt; im.c; chanel ++)
    for (int row = 0; row & lt; im.h; row ++)
      for (int column = 0; column & lt; im.w; column ++)
      {
        sum + = get_pixel (im, column, row, chanel);
      }
  for (int chanel = 0; chanel & lt; im.c; chanel ++)
    for (int row = 0; row & lt; im.h; row ++)
      for (int column = 0; column & lt; im.w; column ++)
      {
        float pixel = get_pixel (im, column, row, chanel);
        set_pixel (im, column, row, chanel, pixel / sum);
      }
}

Creating a matrix:

image make_gaussian_filter (float sigma)
{
  image filter = make_image (6 * sigma + 1, 6 * sigma + 1, 1);
  for (int y = 0; y & lt; filter.h; y ++)
    for (int x = 0; x & lt; filter.w; x ++)
    {
      // find the exhibitor
      // in the matrix 0 0 - it verhiny left corner, so you need to shift the position half the height and width of the image
      float a = pow (x - ceil (filter.w / 2) 2) + pow (y - ceil (filter.h / 2) 2);
      float b = 2 * pow (sigma, 2);
      float ex = exp (- (a / b));
      // I think the main fraction
      a = 1;
      b = TWOPI * pow (sigma, 2); // TWOPI = 6.2831853
      float value = (a / b) * ex;
      set_pixel (filter, x, y, 0, value);
    }
  l1_normalize (filter);
  return filter;
}

Answer 1, Authority 100%

The maximum value of the Gaussian does not reach the unit. But underneath area (the sum of the discrete elements of the matrix) is equal to one – in fact, for this factor before the exponential function and entered

.

For a discrete amount of the matrix after the calculation may differ slightly from the unit, and you can perform normalization by dividing the sum of. However, the unit has reached the peak (it is smaller, the greater the sigma).

If you want to render this kernel so that the peak was white – scale it, the the value peak in the output, if the value of the real (image float format) in the range 0..1, and if the whole in the range 0..255, then more and multiply by 255.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions