views:

280

answers:

2

I am having trouble implementing a LoG kernel. I am trying to implement 9x9 kernal with theta = 1.4 as shown in this link http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm.

However, I am having difficulty with the formula itself. If someone could tell me how to calculate the center ie what x and y values to use in order to get -40 in the 9x9 kernel it'd be greatly appreciated.

A: 

You don't need to worry about the formula - that is just used to generate the coefficients. You just need to apply those 9x9 coefficients to your image.

Example (untested code !):

const int K = 9;
const int K2 = K / 2;
const int NORM = 500; // constant for normalising filter gain
const int coeffs[K][K] = { ... };
int in_image[M][N];
int out_image[M][N];

for (i = K2; i < M - K2; ++i)
{
    for (j = K2; j < N - K2; ++j)
    {
        int term = 0;
        for (di = -K2; di <= K2; ++di)
        {
            for (dj = -K2; dj <= K2; ++dj)
            {
                term += in_image[i + di][j + dj] * coeff[K2 + ii][K2 + jj];
            }
        }
        out_image = term / NORM;
    }
}
Paul R
Ahhh. But I'm curious though, and obsessive, how was the 9x9 kernel with sigma = 1.4 generated using that formula? For the life of me I can't get any one of those values applying it.
Don
Easiest way to check it is just look at the case where y = 0 and look at the values for x. There seems to be a scaling factor of around 483. So if you evaluate 483 * LoG(x, y) for y = 0, x = -4..+4 you should find that you get the correct values. (I just checked this using a spreadsheet and it looks right.) If you want to go further you can of course evaluate the kernel for y = -4..+4, x = -4..+4, but I leave that as an exercise for the reader... ;-)
Paul R
Yes. You're right. I just figured that out 5 min ago. The scaling factor that is. It was bothering like no other. Thank you very much Paul, your help was greatly appreciated.
Don
So why the scaling factor is used and how can it effect the resulting image? (if I am using float data type image structure)
maximus
@maximus: most image processing is does with integer (fixed point) arithmetic. Images are usually integer values and we choose integer coefficients and an implicit scaling factor to keep all the arithmetic in the integer domain. If your application is not performance-critical though, or if you are already stuck with floating point due to the image format, then you can just use floating point coefficients and drop the scaling factor.
Paul R
A: 

Recently I've implemented the LoG filter, the only thing you need is that formula and sigma as a parameter. If you need a fixed size mask, you can store the filter mask values in the matrix and use it, or recalculate it each time and create the needed matrix. The size of the filter depends on sigma value, if more than that size is used - it doesn't make a sence, because the rest part that is out of some maximum size are calculated to zeros using that formula. So, for example you got a filter size = 9X9 Then in order to calculate the filter itself as a matrix, you need to run the formula through this values :

int halfsize = size / 2;    
for (int x = -halfsize; x < halfsize; ++x)
   for (int y = -halfsize; y < halfsize; ++y)
       mask[x][y] = LoGFunction(x, y);

Something like that. It also means that filter size must be an odd value. Hope this helps. In your case, size = 9 sigma = 1.4 x and y changes through -4 to 4. using formula at point (0, 0) (it is the center of the filter) you get something near to -12

But if you put the sigma to 0.2986 You will get the needed answer near to -40. I also don't understand why it is written that sigma value is equal to 1.4 May ba I'm missing something.. Correct me if I made a mistake please

maximus