Navigation

Related Articles

Back to Latest Articles

Intensity Level Slicing With C# – Explore Image Processing


Andraz Krzisnik
Intensity Level Slicing With C# –...

In this tutorial we will be talking about intensity level slicing in image processing.

What is Intensity Level Slicing

It’s a process that highlights pixels in an arbitrary range of intensities. I created an example project that demonstrates this functionality.

There are two different themes this process can be applied. We could highlight our desired range of intensities and set those pixels to white and everything else to black. This way we would produce a binary image.

Another option is we highlight our range of intensities and leave everything else unchanged.

Intensity Level Slicing

Graph above shows intensities, horizontal axis show for input and vertical for output.

Left graph shows the first method we described, where we produce a binary image. As for the right graph we can see that only a certain range of intensities is changed in the output, while other intensity levels stay unchanged.

“a” and “b” represent the lowest and the highest intensity levels to be highlighted. Or in other words, they define the position and width of the intensity level band to be highlighted. We also need to define the highlighting intensity, to which our pixels will be changed to.

This process is applied in various different fields, such as satellite imagery and x-ray imagery.


Intensity Level Slicing Function

public static Bitmap IntensityLevel(Bitmap bmp, int min, int max, int intensity)
        {
            int w = bmp.Width;
            int h = bmp.Height;

            BitmapData sd = bmp.LockBits(new Rectangle(0, 0, w, h), 
                ImageLockMode.ReadOnly, 
                PixelFormat.Format32bppArgb);
            int bytes = sd.Stride * sd.Height;
            byte[] buffer = new byte[bytes];
            byte[] result = new byte[bytes];

            Marshal.Copy(sd.Scan0, buffer, 0, bytes);
            bmp.UnlockBits(sd);
            int current = 0;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    current = y * sd.Stride + x * 4;
                    for (int i = 0; i < 3; i++)
                    {
                        if (buffer[current + i] >= (byte)min && buffer[current + i] <= (byte)max)
                        {
                            result[current + i] = (byte)intensity;
                        }
                        else
                        {
                            result[current + i] = buffer[current + i];
                        }
                        result[current + 3] = 255;
                    }
                }
            }
            Bitmap resimg = new Bitmap(w, h);
            BitmapData rd = resimg.LockBits(new Rectangle(0, 0, w, h), 
                ImageLockMode.WriteOnly, 
                PixelFormat.Format32bppArgb);
            Marshal.Copy(result, 0, rd.Scan0, bytes);
            resimg.UnlockBits(rd);
            return resimg;
        }

There are some things we need to provide for our function to work. We need to set intensity range by providing the lowest and the highest intensity to be highlighted. And lastly we need to set the highlighting intensity.

This code works for colored images too. Intensities are highlighted across all channels. Project also includes a function to convert color images into grayscale.

Entire project is available for download

Download Project

If you liked this project, let me know in the comments.

Is there anything you might add?

I'm always open for your suggestions.

 

Show Comments (0)

Comments

Related Articles

C# Basics

C# Basics – Functions – Part 10

Functions We’ve been writing simple programs so far. But when things get more complicated, writing it all into one block of code could become hard to read. We can use functions...

Posted on by Andraz Krzisnik
C# Tutorial

Histogram Equalization And Magic Behind It

This tutorial is meant to demonstrate how histogram equalization works. This is a continuation on contrast stretch articles I’ve made in the past. An article on histogram...

Posted on by Andraz Krzisnik