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), 
            int bytes = sd.Stride * sd.Height;
            byte[] buffer = new byte[bytes];
            byte[] result = new byte[bytes];

            Marshal.Copy(sd.Scan0, buffer, 0, bytes);
            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;
                            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), 
            Marshal.Copy(result, 0, rd.Scan0, bytes);
            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

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

Is there anything you might add?

I’m always open for your suggestions.

Related Articles

C# Tutorial

How To Use Gaussian Lowpass Filter – C# Guide

We’ve talked about ideal lowpass filter and Butterworth lowpass filter already. And now, we shall get to know the last type of lowpass filters, which is Gaussian lowpass...

Posted on by Andraz Krzisnik
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