# Question about Difference of Gaussian (DoG) algorithm

I am recently learning about Computer Vision and I am having a trouble understanding Difference of Gaussian (DoG) algorithm. I get how the algorithm works in high level idea, but I am trying to implement my own and I am confused about some steps.

For instance, I am trying to create 5 blur level for each octave, and I am confused about which filter and sigma value applying to which image. Using Matlab, for the first octave, I created a filter and applied:

sigma = 0.5;
gauss = fspecial('gaussian', [5 5], sigma);
blur1 = imfilter(img, gauss, 'replicate');
dog1 = img - blur1;
%Next level
blur2 = imfilter(blur1, gauss, 'replicate');
dog2 = blur1 - blur2;


I am not so sure if this is how I need to apply? Do I apply gaussian filter to previously applied image? I also saw code using k*sigma. I am not sure what k means and how to apply? Oh and what value should I used for sigma? Is it in [0, 1] range or can be bigger than that? Could someone help me on this? Thank you very much.
Thank you so much!

• If you have MATLAB version R2015a or later, there is an imgaussfilt function, that will save you the trouble of creating the filter. – Dima Nov 5 '15 at 23:45

Difference of gaussian is the difference in the output of two Gaussian filters with different blur amounts (sigma).

Sigma is the size of the Gaussian filter. A bigger sigma gives you a bigger amount of blurring. A good way to think about it is a Gaussian filter with variance sigma is very roughly like averaging 3 x sigma samples wide (or 3 x 3 in an image)

e.g. from wikipedia:

Very Important when making a Gaussian filter in MATLAB make sure the size of the filter is at least 6 x sigma. In your above code you have 5 x 5 which is fine for sigma = 0.5, but for sigma = 1 you would want 6 x 6 or bigger.

The k is simply a multiplier for sigma.

e.g.

sigma = 0.5;
gauss1 = fspecial('gaussian', round([10*sigma 10*sigma]), sigma);
sigma = 1;
gauss2 = fspecial('gaussian', round([10*sigma 10*sigma]), sigma);
blur1 = imfilter(img, gauss1, 'replicate', 'same');
blur2 = imfilter(img, gauss2, 'replicate', 'same');
dog2 = blur1 - blur2;


A more complete code example that allows you to set the number of octaves and the steps per octave:

%% Filter using DoG
stepsPerOctave = 5;
octaves = 4;
mult = nthroot(2,stepsPerOctave);

% Create blurry images
sigma = 0.5;
kernelSize = [10*sigma*2^(octaves),10*sigma*2^(octaves)]
for k = 1:octaves*stepsPerOctave+1
disp(['Sigma is ' num2str(sigma)]);
gauss = fspecial('gaussian', kernelSize, sigma);
blur(:,:,k) = imfilter(I, gauss, 'replicate', 'same');
imagesc(blur(:,:,k)); colorbar; title(['Gaussian ' num2str(k)]); pause;
sigma = sigma * mult;
end

% Create DoG
for k = 1:octaves*stepsPerOctave
dog(:,:,k) = blur(:,:,k+1) - blur(:,:,k);
imagesc(dog(:,:,k)); colorbar; title(['DoG ' num2str(k)]); pause;
end

• Thank you very much for your explanation and code! That definitely helps me understanding and clear all the confusion! Thank you very very much! – user2168 Jul 6 '15 at 0:24