Navigation

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.

Code

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),
             ImageLockMode.ReadOnly,
             PixelFormat.Format24bppRgb);

         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);
         image.UnlockBits(image_data);

         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),
             ImageLockMode.WriteOnly,
             PixelFormat.Format24bppRgb);
         Marshal.Copy(result, 0, res_data.Scan0, bytes);
         res_img.UnlockBits(res_data);
         return res_img;
     }

Conclusion

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

C# Tutorial: How To Create An Image Negative

A negative image is a complete inversion of an image, we would say to be normal. In other words, dark areas will appear light, and light areas will become dark. More in detail, a...

Posted on by Andraz Krzisnik
Morphological Processes

How To Make Hit Or Miss Transform Work With C#

Hit or miss transform is a morphological process for shape detection. We need to use two different structuring elements to find the shapes.

Posted on by Andraz Krzisnik