Navigation

Related Articles

Back to Latest Articles

How To Make Boundary Extraction Work With C#

Boundary extraction in image processing is one of the basic morphological algorithms with which we extract only the outline of shown objects.


Andraz Krzisnik
How To Make Boundary Extraction Work With C#

Boundary extraction in image processing is one of the basic morphological algorithms. Furthermore, the purpose of this operation is, as its name might suggest, to extract the border of objects. In other words, this process results in an image that displays only the boundaries.

This process works best for binary images, which consist of only white and black pixels. In morphological processing we divide image into sets. Therefore, we’ll put pixels with different colors into separate sets. Furthermore, we will call white pixels foreground pixels and black pixels background pixels.

This technique is based on erosion, which is one of the fundamental morphological operations. I already made a post dedicated to erosion and if you’re not familiar with it, I suggest you check it out. All morphological operations stem from 2 fundamental operations, erosion and dilation.

How does boundary extraction work?

We can extract the boundary of foreground pixels or rather the objects they’re representing by getting the difference between the set of foreground pixel and its erosion. When we apply erosion to an image, we shrink the objects by removing foreground pixels that lay on the edges.

So, if we think about this logically, when we display only those removed pixels, we would end up with an outline of the objects. But there is also another thing to keep in mind, and that is that different structuring elements will produce different results.

For example, if we used bigger structuring elements, which will erode more pixels, we will get a thicker boundary. For the sake of simplicity, we’ll use a 3×3 structuring element filled with foreground pixel values. This structuring element is also the most frequently used in practice.

Code

I’ve already demonstrated the process of erosion in another post. However, the most important part of this operation is subtraction of eroded foreground pixel set from the original foreground pixel set. So I modified the code I used for erosion and added this subtraction.

public static Bitmap BoundaryExtraction(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);

         int o = (se_dim - 1) / 2;
         for (int i = o; i < w - o; i++)
         {
             for (int j = o; j < h - o; j++)
             {
                 int position = i * 3 + j * image_data.Stride;
                 byte val = 255;
                 for (int x = -o; x <= o; x++)
                 {
                     for (int y = -o; y <= o; y++)
                     {
                         int kposition = position + x * 3 + y * image_data.Stride;
                         val = Math.Min(val, buffer[kposition]);
                     }
                 }
                 for (int c = 0; c < 3; c++)
                 {
                     result[position + c] = val;
                 }
             }
         }

         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 guide on boundary extraction in image processing helped you.

You can also download the demo project and try it out yourself.

Show Comments (0)

Comments

Related Articles

C# Image Processing

How To Use Fourier Transform On Images – C# Guide

Fourier transform is an equation that turns normal pixel values into complex numbers. But to know what these complex numbers mean, we should give a little more context to them by...

Posted on by Andraz Krzisnik
Morphological Processes

How To Make Pruning In Image Processing Work With C#

Pruning in image processing is a morphological operation for removing spurs. It serves mainly as a post processing technique for cleaning up.

Posted on by Andraz Krzisnik