MATLAB Answers

Marcus
0

How do you perform a difference of Gaussian filter on an image,

Asked by Marcus
on 20 Dec 2012
Latest activity Commented on by Shahryar Ahmad on 28 Feb 2016

Hi guys

How do you perform a 3x3 difference of Gaussian filter on an image, where sigma1 = 5 and sigma2 = 2 and retain the positive values?

Your help is really appreciated!!!!!!!!!!!!

Thanks

Marcus

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by Image Analyst
on 20 Dec 2012
 Accepted answer

You can't. Try to visualize: what kind of Gaussian shape can you have when you just have one sample point on either side of the middle? Is that really a Gaussian? Now think of two Gaussians - so basically there's two numbers for that location (one pixel away from the center pixel). Now you subtract them and you still have one number. How do you know if that one number is the result of subtracting two Gaussians or is just one single Gaussian? You don't.

That said, you can do DOG filters with larger window sizes. See fspecial() for examples. See this demo I wrote just for you:

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins');
if button == 1
	baseFileName = 'cameraman.tif';
elseif button == 2
	baseFileName = 'moon.tif';
elseif button == 3
	baseFileName = 'eight.tif';
else
	baseFileName = 'coins.png';
end
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
	% File doesn't exist -- didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
		uiwait(warndlg(errorMessage));
		return;
	end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.  
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') 
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2); 
bar(pixelCount);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
gaussian1 = fspecial('Gaussian', 21, 15);
gaussian2 = fspecial('Gaussian', 21, 20);
dog = gaussian1 - gaussian2;
dogFilterImage = conv2(double(grayImage), dog, 'same');
subplot(2, 2, 3); 
imshow(dogFilterImage, []);
title('DOG Filtered Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(dogFilterImage(:));
subplot(2, 2, 4); 
bar(grayLevels, pixelCount);
grid on;
title('Histogram of DOG Filtered Image', 'FontSize', fontSize);

  4 Comments

Sure, go ahead. It will have truncation/quantization error compared to what you'd get if you did it at a higher resolution, but whatever... Try it and see - it will look a lot more like a Laplacian than a difference of Gaussians - pretty harsh and thin edge detection. Again, try it and see.

sir is this 2D - derivative of Gaussian?? please do reply sir....

difference of Gaussian


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!