How To Make Top Hat Transformation Work With C#

Top hat transformation is a grayscale morphological operation in image processing, we can use for extraction of certain objects in the image.


Andraz Krzisnik
How To Make Top Hat Transformation Work With...

Top hat transformation is a morphological operation in image processing for extracting objects from background. There are also two versions of this transformation, because operations we’ll use in it will depend on input image data.

Before I explain, how it depends on the image, I want to mention that this process includes grayscale opening or closing. In case you’re not familiar with these two processes, they are basically sequences of grayscale dilation and erosion.

We already covered these processes in a post where I described the process of morphological smoothing.

Whether we use opening or closing, we can call white or black top hat transformation, respectively. Therefore, if we’re extracting light objects from dark background we will use opening with it. On the other hand if we have the opposite situation, we use closing with it.

How does top hat transformation work?

Basically, we just need to subtract opening or closing of input image from the input image. However, we need to use structuring element of appropriate size to isolate objects completely. In other words, structuring elements need to be larger than the objects we want to extract.

One important use for these operations is to correct non-uniform illumination in the image. Furthermore, uniformity of illumination is important for accurate object extraction. This process is also a complement to thresholding processes, which result in binary images.

It’s very useful for automated image analysis, where we usually need reliable object extraction algorithms.

Code

I slightly modified the code for grayscale erosion and dilation, which I already used in a post of morphological smoothing. The only difference with the one we use here the option to set custom size structuring element.

I won’t include these functions here in this post since it’s so similar to the one already on the other post. But if you want to see what and how I changed exactly, you’ll be able to download the whole demo project and see for yourself.

public static Bitmap WhiteTopHatTransform(this Bitmap image, int se_dim)
     {
         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);

         //opening
         result = buffer.Erode(image_data, se_dim);
         result = result.Dilate(image_data, se_dim);
         //top hat transform
         for (int i = 0; i < bytes; i++)
         {
             result[i] = (byte)(buffer[i] - result[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 how top hat transformation works. As I mentioned before, I’m including a link to the demo project I’ve made for the purpose of this post.

Related Articles

Image Noise

How To Make Gaussian Noise On Image – C# Guide

Gaussian noise on images is generated with Gaussian probability distribution function. It simulates noise that appears in practice.

Posted on by Andraz Krzisnik
Morphological Processes

How To Make Image Dilation Work With C#

Image dilation is one of the fundamental morphological processes, which we demonstrate it here in C# programming language.

Posted on by Andraz Krzisnik