How To Use False Color Coding And Intensity Slicing – C#

False color or pseudocolor image processing coupled with intensity slicing is useful for emphasizing shapes that might be hidden to our eyes.


Andraz Krzisnik
How To Use False Color Coding And Intensity...

False color image processing coupled with intensity slicing is a process, where we assign colors to different shades of gray. We assign different colors to the shades in order to make the shapes in the image stand out.

In other words, we use this process to emphasize details which are represented by different intensities. We also do this because we’re unable to distinguish 255 shades of gray that our modern computers are able to show us. Interestingly enough, we can discern thousands of different colors, while only less than two dozen shades of gray.

False color image processing and intensity slicing

First of all, let’s talk about what intensity slicing is. It’s a process of setting an intensity threshold and define intensities for pixels below and above that threshold.

For example, let’s say we want to set these intensities to be pure black and white. After we set which intensity will act as a threshold, our image will appear of only black and white pixels.

Now, we can take this process to another level, where we create many of these thresholds and color code pixels between each of them. This way we’ll be doing false color coding and intensity slicing.

This process is useful for images that were captured by infrared cameras or taken by X-ray imaging devices.

Coding a demo in C#

I’ve written a program that demonstrates intensity slicing and color coding. So basically what it does, it takes all of the different pixel values – intensities across all color channels and color codes them with colors on a scale from blue to green to red.

It takes the amount of different shades and divides the total of all possible colors on that scale with it. As a result, we get a step between different colors on that scale. This way we’ll get color distribution across the entire scale, no matter how many different shades of gray the original image will have.

Let’s take a look at the function that does all the work.

public static Bitmap Pseudocolorize(this Bitmap image, int colors = 0)
     {
         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];
         Marshal.Copy(image_data.Scan0, buffer, 0, bytes);
         image.UnlockBits(image_data);

         byte[] unique = buffer.Distinct().ToArray();
         Array.Sort(unique);

         int blue = 255;
         int green = 0;
         int red = 0;

         byte[][] color_scale = new byte[unique.Length][];
         for (int i = 0; i < unique.Length; i++)
         {
             color_scale[i] = new byte[3];
             byte step = (byte)Math.Round(512f / unique.Length);

             if (blue > 0 && green < 255)
             {
                 color_scale[i][0] = (byte)blue;
                 color_scale[i][1] = (byte)green;
                 color_scale[i][2] = 0;

                 blue -= step;
                 green += step;

                 if (blue < 0)
                 {
                     blue = 0;
                 }

                 if (green > 255)
                 {
                     green = 255;
                 }
             }

             else
             {
                 color_scale[i][0] = 0;
                 color_scale[i][1] = (byte)green;
                 color_scale[i][2] = (byte)red;

                 green -= step;
                 red += step;

                 if (green < 0)
                 {
                     green = 0;
                 }

                 if (red > 255)
                 {
                     red = 255;
                 }
             }
         }

         byte[] result = new byte[bytes];
         for (int i = 0; i < bytes; i+=3)
         {
             for (int j = 0; j < 3; j++)
             {
                 result[i + j] = color_scale[Array.IndexOf(unique, buffer[i + j])][j];
             }
         }
         Bitmap result_image = new Bitmap(w, h);
         BitmapData result_data = result_image.LockBits(
             new Rectangle(0, 0, w, h),
             ImageLockMode.WriteOnly,
             PixelFormat.Format24bppRgb);
         Marshal.Copy(result, 0, result_data.Scan0, bytes);
         result_image.UnlockBits(result_data);
         return result_image;
     }

Conclusion

This kind of false color processing is one of the simplest and earliest there was. I hope this demonstration helped you understand how it works.

In case you want to play with or modify the code, you can download the entire project here for free.

Related Articles

Frequency Domain Filtering

How To Use Notch Filters – C# Guide

We can use notch filters for attenuating frequencies on custom locations across the frequency map. But for that we will need to utilize all of the knowledge we’ve acquired...

Posted on by Andraz Krzisnik
Image Noise

How To Make Uniform Noise On Images – C# Guide

Uniform noise is one of the noise models we can use to simulate real life data corruption. This guide shows how to make in on images.

Posted on by Andraz Krzisnik