Related Articles

Back to Latest Articles

How To Make Morphological Gradient Work With C#

Morphological gradient is a grayscale morphological operation in image processing, which emphasized boundaries and supresses homogenous areas

Andraz Krzisnik
How To Make Morphological Gradient Work With...

Morphological gradient is an operation in image processing, with which we can emphasize edges in grayscale images. Furthermore, it utilizes grayscale dilation and erosion to calculate the difference between the two.

When we described dilation and erosion processes, we were working with binary images. Therefore, we we’re talking about how we were taking away and adding foreground pixels.

But, in our case here, we work with grayscale images, which have more than two possible intensities. For this reason, grayscale dilation and erosion work a little differently. But we still have a structuring element to slide over the image, while comparing values at each step.

However, the difference in grayscale morphology is that we choose the resulting pixel value by taking all of the values in the neighborhood into account. For example, with erosion, we pick minimum intensity in neighborhood overlapped by structuring element.

How does morphological gradient work?

As we mentioned before, we’re going to use grayscale dilation and erosion to get the result. Firstly, we need to get the resulting images from each of these two operations. And secondly, we need to subtract erosion image values from dilation image values.

In essence, dilation thickens regions and generally makes images a bit brighter. While on the other hand, erosion has the opposite effect. And if we get the difference between these two, we’ll get an image with emphasized boundaries and suppressed homogenous areas.

However, results do depend on the size of the structuring element we want to use.


We already talked about grayscale erosion and dilation in another post, where I already posted the code for these operations. Therefore, I won’t copy it again for this guide, but I will post the function for applying morphological gradient.

public static Bitmap MorphologicalGradient(this Bitmap image)
         int w = image.Width;
         int h = image.Height;

         BitmapData image_data = image.LockBits(
             new Rectangle(0, 0, w, h),

         int bytes = image_data.Stride * image_data.Height;
         byte[] buffer = new byte[bytes];
         byte[] result = new byte[bytes];

         Marshal.Copy(image_data.Scan0, buffer, 0, bytes);

         result = buffer.Dilate(image_data);
         buffer = buffer.Erode(image_data);

         for (int i = 0; i < bytes; i++)
             result[i] = (byte)(result[i] - buffer[i]);

         Bitmap res_img = new Bitmap(w, h);
         BitmapData res_data = res_img.LockBits(
             new Rectangle(0, 0, w, h),
         Marshal.Copy(result, 0, res_data.Scan0, bytes);
         return res_img;


I hope this tutorial was helpful in giving you a better understanding on how morphological gradient and grayscale morphological operations work in general.

In case you want to check out the demo project, I’m leaving a download link below.

Related Articles

C# Tutorial

How To Use Butterworth Highpass Filter – C# Guide

Butterworth highpass filter is used to filter images in frequency domain. We can control how smooth transition beyond cut off frequency.

Posted on by Andraz Krzisnik
Morphological Processes

How To Make Convex Hull In Image Processing With C#

Convex hull in image processing is a morphological operation, where we encapsulate a shape or and object in an image into a convex shape.

Posted on by Andraz Krzisnik