In this article we explore the concept of Difference of Gaussians edge detection. This article implements image convolution as a means of achieving Gaussian blurring. All of the concepts explored are implemented by accessing and manipulating the raw pixel data exposed by an image, no GDI+ or conventional drawing code is required.
Sample source code
Using the Sample Application
The concepts explored in this article can be easily replicated by making use of the Sample Application, which forms part of the associated sample source code accompanying this article.
When using the Difference Of Gaussians sample application you can specify a input/source image by clicking the Load Image button. The dropdown combobox towards the bottom middle part of the screen relates the various edge detection methods discussed.
If desired a user can save the resulting edge detection image to the local file system by clicking the Save Image button.
The following image is screenshot of the Difference Of Gaussians sample application in action:
What is Difference of Gaussians?
Difference of Gaussians, commonly abbreviated as DoG, is a method of implementing image edge detection. Central to the Difference of Gaussians method of edge detection is the application of Gaussian image blur.
In imaging science, Difference of Gaussians is a feature enhancement algorithm that involves the subtraction of one blurred version of an original image from another, less blurred version of the original. In the simple case of grayscale images, the blurred images are obtained by convolving the original grayscale images with Gaussian kernels having differing standard deviations. Blurring an image using a Gaussian kernel suppresses only high-frequency spatial information. Subtracting one image from the other preserves spatial information that lies between the range of frequencies that are preserved in the two blurred images. Thus, the difference of Gaussians is a band-pass filter that discards all but a handful of spatial frequencies that are present in the original grayscale image.
In simple terms Difference of Gaussians can be implemented by applying two Gaussian blurs of different intensity levels to the same source image. The resulting image is then created by subtracting the two images of different Gaussian blurring.
Applying a Convolution Matrix filter
In the sample source code accompanying this article Gaussian image blurring is applied by invoking the ConvolutionFilter method. This method accepts a two dimensional array of type double representing the convolution matrix/kernel. This method is also capable of first converting source images to grayscale, which can be specified as a method parameter. Resulting images sometimes tend to be very dark, which can be corrected by specifying a suitable bias value.
The following code snippet provides the implementation of the ConvolutionFilter method:
The Gaussian Matrix
The sample source code defines three Gaussian matrix/kernel values, a 3×3 matrix and two slightly different 5×5 matrices. The Gaussian3x3 matrix requires a factor of 1 / 16, the Gaussian5x5Type1 matrix a factor of 1 / 159 and the factor required by the Gaussian5x5Type2 equates to 1 / 256.
When implementing the Difference of Gaussians method of edge detection after having applied two varying levels of Gaussian blurring the resulting images need to be subtracted. The sample source code associated with this article implements the SubtractImage extension method when subtracting images.
The following code snippet details the implementation of the SubtractImage extension method:
Difference of Gaussians Extension methods
The sample source code implements Difference of Gaussians edge detection by means of two extension methods: DifferenceOfGaussians3x5Type1 and DifferenceOfGaussians3x5Type2. Both methods are virtually identical, the only difference being the 5×5 Gaussian matrix being implemented.
The following source code snippet provides the implementation of the DifferenceOfGaussians3x5Type1 and DifferenceOfGaussians3x5Type2 extension methods:
The original sample image used in this article is licensed under the Creative Commons Attribution-Share Alike 2.0 Generic license. The original author is attributed as Andrew Dunn – http://www.andrewdunnphoto.com/
The Original Image
Difference Of Gaussians 3×5 Type1
Difference Of Gaussians 3×5 Type2
Difference Of Gaussians 3×5 Type1 Bias 128
Difference Of Gaussians 3×5 Type 2 Bias 96
Related Articles and Feedback
Feedback and questions are always encouraged. If you know of an alternative implementation or have ideas on a more efficient implementation please share in the comments section.
I’ve published a number of articles related to imaging and images of which you can find URL links here:
- C# How to: Image filtering by directly manipulating Pixel ARGB values
- C# How to: Image filtering implemented using a ColorMatrix
- C# How to: Blending Bitmap images using colour filters
- C# How to: Bitmap Colour Substitution implementing thresholds
- C# How to: Generating Icons from Images
- C# How to: Swapping Bitmap ARGB Colour Channels
- C# How to: Bitmap Pixel manipulation using LINQ Queries
- C# How to: Linq to Bitmaps – Partial Colour Inversion
- C# How to: Bitmap Colour Balance
- C# How to: Bi-tonal Bitmaps
- C# How to: Bitmap Colour Tint
- C# How to: Bitmap Colour Shading
- C# How to: Image Solarise
- C# How to: Image Contrast
- C# How to: Bitwise Bitmap Blending
- C# How to: Image Arithmetic
- C# How to: Image Convolution
- C# How to: Image Edge Detection
- C# How to: Image Median Filter
- C# How to: Image Unsharp Mask
- C# How to: Image Colour Average
- C# How to: Image Erosion and Dilation
- C# How to: Morphological Edge Detection
- C# How to: Boolean Edge Detection
- C# How to: Gradient Based Edge Detection
- C# How to: Sharpen Edge Detection
- C# How to: Image Cartoon Effect
- C# How to: Calculating Gaussian Kernels
- C# How to: Image Blur
- C# How to: Image Transform Rotate
- C# How to: Image Transform Shear
- C# How to: Compass Edge Detection
- C# How to: Oil Painting and Cartoon Filter
- C# How to: Stained Glass Image Filter