### Article purpose

In we explore the concept of . implements as a means of achieving . All of the concepts explored are implemented by accessing  and manipulating the raw pixel data exposed by an , no GDI+ or conventional drawing code is required.

### Sample source code

is accompanied by a sample source code Visual Studio project which is available for download .

### Using the Sample Application

The concepts explored in 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 towards the bottom middle part of the screen relates the various methods discussed.

If desired a user can save the resulting 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?

, commonly abbreviated as DoG, is a method of implementing  . Central to the method of is the application of .

From we gain the following :

In , Difference of Gaussians is a 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 , the blurred images are obtained by the original with Gaussian kernels having differing standard deviations. Blurring an image using a suppresses only 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 that discards all but a handful of spatial frequencies that are present in the original grayscale image.

In simple terms can be implemented by applying two of different intensity levels to the same source . The resulting is then created by subtracting the two of different .

### Applying a Convolution Matrix filter

In the sample source code accompanying is applied by invoking the ConvolutionFilter method. This method accepts a two dimensional array of type double representing the convolution /. This method is also capable of first converting source to , which can be specified as a method parameter. Resulting 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:

``` private static Bitmap ConvolutionFilter(Bitmap sourceBitmap,
double[,] filterMatrix,
double factor = 1,
int bias = 0,
bool grayscale = false )
{
BitmapData sourceData = sourceBitmap.LockBits(new Rectangle (0, 0,
sourceBitmap.Width, sourceBitmap.Height),
PixelFormat.Format32bppArgb);

byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];

Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
sourceBitmap.UnlockBits(sourceData);

if (grayscale == true)
{
float rgb = 0;

for (int k = 0; k < pixelBuffer.Length; k += 4)
{
rgb = pixelBuffer[k] * 0.11f;
rgb += pixelBuffer[k + 1] * 0.59f;
rgb += pixelBuffer[k + 2] * 0.3f;

pixelBuffer[k] = (byte )rgb;
pixelBuffer[k + 1] = pixelBuffer[k];
pixelBuffer[k + 2] = pixelBuffer[k];
pixelBuffer[k + 3] = 255;
}
}

double blue = 0.0;
double green = 0.0;
double red = 0.0;

int filterWidth = filterMatrix.GetLength(1);
int filterHeight = filterMatrix.GetLength(0);

int filterOffset = (filterWidth-1) / 2;
int calcOffset = 0;

int byteOffset = 0;

for (int offsetY = filterOffset; offsetY <
sourceBitmap.Height - filterOffset; offsetY++)
{
for (int  offsetX = filterOffset; offsetX <
sourceBitmap.Width - filterOffset; offsetX++)
{
blue = 0;
green = 0;
red = 0;

byteOffset = offsetY *
sourceData.Stride +
offsetX * 4;

for (int  filterY = -filterOffset;
filterY <= filterOffset; filterY++)
{
for (int  filterX = -filterOffset;
filterX <= filterOffset; filterX++)
{

calcOffset = byteOffset +
(filterX * 4) +
(filterY * sourceData.Stride);

blue += (double)(pixelBuffer[calcOffset]) *
filterMatrix[filterY + filterOffset,
filterX + filterOffset];

green += (double)(pixelBuffer[calcOffset + 1]) *
filterMatrix[filterY + filterOffset,
filterX + filterOffset];

red += (double)(pixelBuffer[calcOffset + 2]) *
filterMatrix[filterY + filterOffset,
filterX + filterOffset];
}
}

blue = factor * blue + bias;
green = factor * green + bias;
red = factor * red + bias;

if (blue > 255)
{ blue = 255; }
else if (blue < 0)
{ blue = 0; }

if (green > 255)
{ green = 255; }
else if (green < 0)
{ green = 0; }

if (red > 255)
{ red = 255; }
else if (red < 0)
{ red = 0; }

resultBuffer[byteOffset] = (byte )(blue);
resultBuffer[byteOffset + 1] = (byte )(green);
resultBuffer[byteOffset + 2] = (byte )(red);
resultBuffer[byteOffset + 3] = 255;
}
}

Bitmap resultBitmap = new Bitmap(sourceBitmap.Width,
sourceBitmap.Height);

BitmapData resultData = resultBitmap.LockBits(new Rectangle (0, 0,
resultBitmap.Width, resultBitmap.Height),
ImageLockMode .WriteOnly,
PixelFormat.Format32bppArgb);

Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
resultBitmap.UnlockBits(resultData);

return resultBitmap;
} ```

### The Gaussian Matrix

The sample source code defines three / values, a 3×3 and two slightly different 5×5 matrices. The Gaussian3x3 requires a factor of 1 / 16, the Gaussian5x5Type1 a factor of 1 / 159 and the factor required by the Gaussian5x5Type2 equates to 1 / 256.

```public static class Matrix
{
public static double[,] Gaussian3x3
{
get
{
return new double[,]
{ { 1, 2, 1, },
{ 2, 4, 2, },
{ 1, 2, 1, }, };
}
}

public static double[,] Gaussian5x5Type1
{
get
{
return new double[,]
{ { 2, 04, 05, 04, 2  },
{ 4, 09, 12, 09, 4  },
{ 5, 12, 15, 12, 5  },
{ 4, 09, 12, 09, 4  },
{ 2, 04, 05, 04, 2  }, };
}
}

public static double[,] Gaussian5x5Type2
{
get
{
return new   double[,]
{ {  1,   4,  6,  4,  1  },
{  4,  16, 24, 16,  4  },
{  6,  24, 36, 24,  6  },
{  4,  16, 24, 16,  4  },
{  1,   4,  6,  4,  1  }, };
}
}
}```

### Subtracting Images

When implementing the method of after having applied two varying levels of the resulting need to be subtracted. The sample source code associated with implements the SubtractImage when subtracting .

The following code snippet details the implementation of the SubtractImage :

```private static void SubtractImage(this Bitmap subtractFrom,
Bitmap subtractValue,
bool invert = false,
int bias = 0)
{
BitmapData sourceData =
subtractFrom.LockBits(new Rectangle(0, 0,
subtractFrom.Width, subtractFrom.Height),
PixelFormat.Format32bppArgb);

byte[] resultBuffer = new byte[sourceData.Stride *
sourceData.Height];

Marshal.Copy(sourceData.Scan0, resultBuffer, 0,
resultBuffer.Length);

BitmapData subtractData =
subtractValue.LockBits(new Rectangle(0, 0,
subtractValue.Width, subtractValue.Height),
PixelFormat.Format32bppArgb);

byte[] subtractBuffer = new byte[subtractData.Stride *
subtractData.Height];

Marshal.Copy(subtractData.Scan0, subtractBuffer, 0,
subtractBuffer.Length);

subtractValue.UnlockBits(subtractData);

int blue = 0;
int green = 0;
int red = 0;

for (int k = 0; k < resultBuffer.Length &&
k < subtractBuffer.Length; k += 4)
{
if (invert == true )
{
blue = 255 - resultBuffer[k] -
subtractBuffer[k] + bias;

green = 255 - resultBuffer[k + 1] -
subtractBuffer[k + 1] + bias;

red = 255 - resultBuffer[k + 2] -
subtractBuffer[k + 2] + bias;
}
else
{
blue = resultBuffer[k] -
subtractBuffer[k] + bias;

green = resultBuffer[k + 1] -
subtractBuffer[k + 1] + bias;

red = resultBuffer[k + 2] -
subtractBuffer[k + 2] + bias;
}

blue = (blue < 0 ? 0 : (blue > 255 ? 255 : blue));
green = (green < 0 ? 0 : (green > 255 ? 255 : green));
red = (red < 0 ? 0 : (red > 255 ? 255 : red));

resultBuffer[k] = (byte )blue;
resultBuffer[k + 1] = (byte )green;
resultBuffer[k + 2] = (byte )red;
resultBuffer[k + 3] = 255;
}

Marshal.Copy(resultBuffer, 0, sourceData.Scan0,
resultBuffer.Length);

subtractFrom.UnlockBits(sourceData);
}```

### Difference of Gaussians Extension methods

The sample source code implements   by means of two : DifferenceOfGaussians3x5Type1 and DifferenceOfGaussians3x5Type2. Both methods are virtually identical, the only difference being the 5×5 being implemented.

Both methods create two new , each having a of different levels of intensity applied. The two new are subtracted in order to create a single resulting .

The following source code snippet provides the implementation of the DifferenceOfGaussians3x5Type1 and DifferenceOfGaussians3x5Type2 :

```public static Bitmap DifferenceOfGaussians3x5Type1(
this Bitmap sourceBitmap,
bool grayscale = false,
bool invert = false,
int bias = 0)
{
Bitmap bitmap3x3 = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian3x3, 1.0 / 16.0,
0, grayscale);

Bitmap bitmap5x5 = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian5x5Type1, 1.0 / 159.0,
0, grayscale);

bitmap3x3.SubtractImage(bitmap5x5, invert, bias);

return bitmap3x3;
}

public static Bitmap DifferenceOfGaussians3x5Type2(
this Bitmap sourceBitmap,
bool grayscale = false,
bool invert = false,
int bias = 0)
{
Bitmap bitmap3x3 = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian3x3, 1.0 / 16.0,
0, true );

Bitmap bitmap5x5 = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian5x5Type2, 1.0 / 256.0,
0, true );

bitmap3x3.SubtractImage(bitmap5x5, invert, bias);

return bitmap3x3;
}```

### Sample Images

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:

#### 31 Responses to “C# How to: Difference Of Gaussians”

1. 1 C# How to: Image Median Filter | Software by Default Trackback on May 18, 2013 at 4:16 AM
2. 2 C# How to: Image Unsharp Mask | Software by Default Trackback on May 18, 2013 at 12:18 PM
3. 3 C# How to: Image Colour Average | Software by Default Trackback on May 18, 2013 at 9:48 PM
4. 4 C# How to: Image Erosion and Dilation | Software by Default Trackback on May 19, 2013 at 10:24 AM
5. 5 C# How to: Morphological Edge Detection | Software by Default Trackback on May 25, 2013 at 8:23 AM
6. 6 C# How to: Boolean Edge Detection | Software by Default Trackback on June 1, 2013 at 2:09 AM
7. 7 C# How to: Gradient Based Edge Detection | Software by Default Trackback on June 1, 2013 at 4:46 PM
8. 8 C# How to: Image Cartoon Effect | Software by Default Trackback on June 2, 2013 at 4:13 PM
9. 9 C# How to: Sharpen Edge Detection | Software by Default Trackback on June 7, 2013 at 5:11 AM
10. 10 C# How to: Calculating Gaussian Kernels | Software by Default Trackback on June 8, 2013 at 10:59 AM
11. 11 C# How to: Image Blur | Software by Default Trackback on June 9, 2013 at 10:19 PM
12. 12 C# How to: Image Transform Rotate | Software by Default Trackback on June 16, 2013 at 10:40 AM
13. 13 C# How to: Image Transform Shear | Software by Default Trackback on June 16, 2013 at 5:45 PM
14. 14 C# How to: Compass Edge Detection | Software by Default Trackback on June 22, 2013 at 9:34 PM
15. 15 C# How to: Oil Painting and Cartoon Filter | Software by Default Trackback on June 30, 2013 at 10:47 AM
16. 16 C# How to: Stained Glass Image Filter | Software by Default Trackback on June 30, 2013 at 10:50 AM
17. 17 C# How to: Image Edge Detection | Software by Default Trackback on June 30, 2013 at 3:33 PM
18. 18 C# How to: Image Convolution | Software by Default Trackback on June 30, 2013 at 3:54 PM
19. 19 C# How to: Generate a Web Service from WSDL | Software by Default Trackback on June 30, 2013 at 4:08 PM
20. 20 C# How to: Decoding/Converting Base64 strings to Bitmap images | Software by Default Trackback on June 30, 2013 at 4:13 PM
21. 21 C# How to: Bitmap Colour Substitution implementing thresholds | Software by Default Trackback on July 6, 2013 at 4:33 PM
22. 22 C# How to: Swapping Bitmap ARGB Colour Channels | Software by Default Trackback on July 6, 2013 at 5:02 PM
23. 23 C# How to: Image filtering by directly manipulating Pixel ARGB values | Software by Default Trackback on July 8, 2013 at 2:57 AM
24. 24 C# How to: Image ASCII Art | Software by Default Trackback on July 14, 2013 at 7:22 AM
25. 25 C# How to: Weighted Difference of Gaussians | Software by Default Trackback on July 14, 2013 at 8:11 PM
26. 26 C# How to: Image Boundary Extraction | Software by Default Trackback on July 21, 2013 at 10:23 AM
27. 27 C# How to: Image Abstract Colours Filter | Software by Default Trackback on July 28, 2013 at 7:41 PM
28. 28 C# How to: Fuzzy Blur Filter | Software by Default Trackback on August 9, 2013 at 6:39 AM
29. 29 C# How to: Image Distortion Blur | Software by Default Trackback on August 9, 2013 at 10:13 PM
30. 30 C# How to: Standard Deviation Edge Detection | Software by Default Trackback on August 8, 2015 at 8:10 AM
31. 31 C# How to: Min/Max Edge Detection | Software by Default Trackback on August 9, 2015 at 11:30 AM

• 843,493 hits