C# Tutorial - Convert a Color Image to Grayscale

Skill

C# Tutorial - Convert a Color Image to Grayscale

Posted in:

We've done a few tutorials about image editing with C#. We're going to continue the series with a tutorial on how to convert a color image to black and white. This tutorial will show three different ways to convert an image to grayscale - starting with the easiest and slowest.

You're probably wondering why you would want to see multiple ways to accomplish the same thing. It's hard to predict what technique will work best for everyone's application, so I've decided to outline several different possibilities.

Below is the image I used to test each algorithm and to benchmark their performance. The image can be downloaded free from Gaming Textures. The conversion times listed were obtained using my computer so your times might be different.

Flower in Color

Original color image

Flower in Black & White

Image converted to black and white

1. Slow and Simple

Convert Time: 1,135ms

The first method I'm going to show you is by far the easiest to understand and implement. Unfortunately, it's also the slowest.

public static Bitmap MakeGrayscale(Bitmap original)
{
   //make an empty bitmap the same size as original
   Bitmap newBitmap = new Bitmap(original.Width, original.Height);

   for (int i = 0; i < original.Width; i++)
   {
      for (int j = 0; j < original.Height; j++)
      {
         //get the pixel from the original image
         Color originalColor = original.GetPixel(i, j);

         //create the grayscale version of the pixel
         int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
             + (originalColor.B * .11));

         //create the color object
         Color newColor =  Color.FromArgb(grayScale, grayScale, grayScale);
         
         //set the new image's pixel to the grayscale version
         newBitmap.SetPixel(i, j, newColor);
        }
    }

    return newBitmap;
}

This code looks at every pixel in the original image and sets the same pixel in the new bitmap to a grayscale version. You can probably figure out why this is so slow. If the image is 2048x2048, this code will call GetPixel and SetPixel over 4 million times. Those functions aren't the most efficient way to get pixel data from the image.

You might be wondering where the numbers .3, .59, and .11 came from. In reality, you could just take the average color by adding up R, G, B and dividing by three. In fact, you'll get a pretty good black and white image by doing that. However, at some point, someone a lot smarter than me figured out that these numbers better approximate the human eye's sensitivity to each of those colors. The Wikipedia article on grayscale has some pretty good information about that.

This method will work fine for small images or when you don't really care about how long it takes to convert it to black and white. If you need it done quickly, I would recommend one of the next two methods.

2. Faster and Complicated

Convert Time: 188ms

The next technique I'm going to show you is based on the previous one, but much faster. It still iterates through every pixel, but we're going to utilize C#'s unsafe keyword to make getting the pixel data much more efficient.

public static Bitmap MakeGrayscale2(Bitmap original)
{
   unsafe
   {
      //create an empty bitmap the same size as original
      Bitmap newBitmap = new Bitmap(original.Width, original.Height);

      //lock the original bitmap in memory
      BitmapData originalData = original.LockBits(
         new Rectangle(0, 0, original.Width, original.Height),
         ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

      //lock the new bitmap in memory
      BitmapData newData = newBitmap.LockBits(
         new Rectangle(0, 0, original.Width, original.Height),
         ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
 
      //set the number of bytes per pixel
      int pixelSize = 3;

      for (int y = 0; y < original.Height; y++)
      {
         //get the data from the original image
         byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

         //get the data from the new image
         byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

         for (int x = 0; x < original.Width; x++)
         {
            //create the grayscale version
            byte grayScale =
               (byte)((oRow[x * pixelSize] * .11) + //B
               (oRow[x * pixelSize + 1] * .59) +  //G
               (oRow[x * pixelSize + 2] * .3)); //R

            //set the new image's pixel to the grayscale version
            nRow[x * pixelSize] = grayScale; //B
            nRow[x * pixelSize + 1] = grayScale; //G
            nRow[x * pixelSize + 2] = grayScale; //R
         }
      }

      //unlock the bitmaps
      newBitmap.UnlockBits(newData);
      original.UnlockBits(originalData);

      return newBitmap;
   }
}

There's a lot of code here so let's go through it piece by piece. The first thing we need to do is lock the bits in the Bitmap objects. Locking the bits keeps the .NET runtime from moving them around in memory. This is important because we're going to use a pointer, and if the data is moving around the pointer won't point to the correct thing anymore. You'll need to know the pixel format of the image you're trying to convert. I'm using jpeg's, which are 24 bits per pixel. There is a way to get the pixel format from an image, but that's outside the scope of this tutorial. The integer, pixelSize, is the number of bytes per pixel in your original image. Since my images were 24 bits per pixel, that translates to 3 bytes per pixel.

To get pixel data, I start by getting the address to the first pixel in each row of the image. Scan0 returns the address of the first pixel in the image. So in order to get the address of the first pixel in the row, we have to add the number of bytes in the row, Stride, multiplied by the row number, y. Below is a diagram that might help you understand this a little better.

Pixel Data Diagram

Now we can get color data straight from memory by accessing it like an array. The byte at x * pixelSize will be the blue, x * pixelSize + 1 is green, and x * pixelSize + 2 is red. This is why pixelSize is very important. If the image you provided is not 3 bytes per pixel, you'll be pulling color data from the wrong location in memory.

Next, make the grayscale version using the same process as the previous method and set the exact same pixel in the new image. All that's left to do is to unlock the bitmaps and return the new image.

3. Short and Sweet

Convert Time: 62ms

The last method is probably the best way. It's faster than the previous two methods and uses much less code. This technique uses a ColorMatrix to perform the conversion. A ColorMatrix is a 5x5 matrix that can make just about any modifications to the color of an image. A ColorMatrix is pretty complicated and deserves a whole tutorial to itself. For now, if you want more information about it, you'll have to do the research yourself.

public static Bitmap MakeGrayscale3(Bitmap original)
{
   //create a blank bitmap the same size as original
   Bitmap newBitmap = new Bitmap(original.Width, original.Height);
   
   //get a graphics object from the new image
   Graphics g = Graphics.FromImage(newBitmap);

   //create the grayscale ColorMatrix
   ColorMatrix colorMatrix = new ColorMatrix(
      new float[][]
      {
         new float[] {.3f, .3f, .3f, 0, 0},
         new float[] {.59f, .59f, .59f, 0, 0},
         new float[] {.11f, .11f, .11f, 0, 0},
         new float[] {0, 0, 0, 1, 0},
         new float[] {0, 0, 0, 0, 1}
      });

   //create some image attributes
   ImageAttributes attributes = new ImageAttributes();

   //set the color matrix attribute
   attributes.SetColorMatrix(colorMatrix);

   //draw the original image on the new image
   //using the grayscale color matrix
   g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
      0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

   //dispose the Graphics object
   g.Dispose();
   return newBitmap;
}

This time we're going to use GDI to draw the new black and white image. The benefit of this technique over the last one is that we don't need to know any information about the image's pixel format. The code here is pretty straight forward. First create the blank image and get a Graphics object from it. Next, create the ColorMatrix that will convert the original image to grayscale. Declare an ImageAttributes object that will use the ColorMatrix and use it to draw the new image using DrawImage.

That's it for converting an image to grayscale using C# and .NET. Hopefully one of the above techniques will work for your application. Leave us a comment if you've got questions or anything else to say.

Anil Madamala
11/14/2007 - 16:57

Hi this is pretty useful, in my case I am processing a large image, the conversion from color to black and white itself taking much time.

Thanks

reply

Hnminh
12/03/2007 - 21:43

So cool! Your article help me so much with my project at school. Thanks!

But, how about images which are 32bpp?

reply

Rodrigo Silva
12/28/2007 - 13:20

Very good article. Your research about human eye's sensitivity to build the entire algorithm was very usefull for future implementations!

I'm looking forward to tutorials about croping and rotating images as well!

Cheers!

reply

Nishith
03/05/2008 - 07:37

Great, can we also convert 32/24 bpp to 8bpp in similar fashion?

reply

Mehboob
03/10/2008 - 01:51

Thanx alot.

reply

Dennis
03/12/2008 - 10:23

Can this be made to work in reverse? I have a black and white line art image saved as a grayscale TIFF that I need to take and apply preset CMYK values to the black area only. The CMYK values would be assigned dynamically.

reply

Coolgeek1998
04/11/2008 - 10:03

Hi Interesting tutorial!
Glad to c tutorial set dedicated to image processing in C#.
Right now a group of my friends and I are taking the very first steps in creating a rather wacky new face recognition technique we got in our heads...... But first things first... c how we can manipulate and extract info on images in the computer... so this is great.. thanks.

reply

Tom
05/13/2008 - 20:40

I'm new to C#. I come from JAVA where I do command-line image processing. This is a nice tutorial to observe the C# way.

How is Bitmap type setup? I can't get the algorithm to work with C# console application. I will need to put a "using" at the top, but the IDE helper does not offer System.Drawing, it has System.(other stuff) but not Drawing. So I assume I can't do these things in console app environment in C#? I'm using C#2008 express.

Thanks for your time.

reply

The Reddest
05/14/2008 - 09:27

Before you can add a 'using' statement, you'll first have to make sure your project has a reference to the System.Drawing assembly. Using the Solution Explorer, expand the 'References' folder under your project. If System.Drawing doesn't exist there, right-mouse click on the folder and selected 'Add Reference'. When the dialog appears, select the .NET tab, scroll down to System.Drawing, and add it. Now you should be able to include it into your source.

reply

Anonymous
03/11/2011 - 16:55

Thank you MAN!

reply

Usman
05/21/2008 - 07:21

it is very good.as it is easy to understand

reply

Maninder
09/24/2008 - 11:12

i want to identify a circel in a image

reply

Gustav
09/27/2008 - 15:59

Nice piece of code (#3) but you forgot to listen the needed references, even though they are quite easy to figure out:

using System.Drawing;
using System.Drawing.Imaging;

reply

Ihteshamq
10/27/2008 - 04:43

How did you find the matrix values.

It looks good though

reply

The Reddest
10/27/2008 - 07:08

I explain that a little up in the "Slow and Simple" portion of this tutorial, however those values came from the Wikipedia article on Grayscale.

reply

Ihteshamq
10/27/2008 - 07:26

Thanks for letting me know. How /where can i find the matrix values for CMYK.

reply

The Reddest
10/27/2008 - 08:09

The ColorMatrix class only works in the RGBA space. You'd have to find something that converts CMYK to RGB.

reply

David
11/23/2008 - 13:23

Nice thanks! :)

reply

David
11/24/2008 - 08:45

How would one handle .gifs and .png with transpareny?

reply

David
11/25/2008 - 00:45

Me again Answered my own Q? :)

If you want to grayscale a .gif or .png with transparency colors....

Just save the grayscaled image as Format.Png and not as Format.Gif.

ps.Also change the file extension to .png.

Hope this saves somebody some time.

reply

Angel Jimenez
12/01/2008 - 09:14

i tried to compile your routine in compaq visual fortran, that must compile c# programs, but i had a lot of errors.

reply

The Reddest
12/01/2008 - 10:49

I’ve compiled the code using Microsoft’s C# compiler and Mono’s compiler without issues. I’ve never used the Visual Fortran compiler.

reply

Angel Jimenez
12/01/2008 - 09:37

i have sea surface temperature jpg images and i want to get the (lat,lon,temp) data of each pixel.
have you some software to do this?

reply

The Reddest
12/01/2008 - 10:49

We don’t have any software to read the data you’re looking for. The code in this post does allow you to look at the information inside each pixel (RGB values), and I’m sure you can expand it into something you want.

reply

Tomas Matejka
12/19/2008 - 04:30

Thanks a lot. This is one of the best :-)

reply

Gercios
01/08/2009 - 10:18

Hi

I have a problem. I cannot find information, that if we can use "if" statemant somehow when using color matrix, in the way, that for example color matrix will not affect yellow colors. I suppose i can use a method with locking bits, but color matrix is much more nice. Please help me, if you can, give a sample of code, or refer me to a website. I was googling, and couldn't find nothing :/

reply

s.a.pishvaie
03/21/2009 - 10:17

it is so great and so sweet
with best regards

reply

Anonymous
03/22/2009 - 21:19

HELP!. how do i input the image i want converted for method 3?

how do i set 'original'?. THANKS A LOT

reply

chithu
04/15/2009 - 00:21

how can i merge one image to another thanks in adv.

reply

Andrei
05/12/2009 - 08:20

Hello!
Could you give me some help and advice please?
I would like to know how the grayscale code can be applyed to a button in a menu like in the other projects, near rotate,crop,etc.
Thank you!
andrei

reply

aman sandha
06/28/2009 - 05:50

you can do it on any event just on button click also

thnx

reply

madd
05/20/2009 - 06:13

hi, thanks for the code, it helped me a lot.

reply

OtherworldBob
05/20/2009 - 11:57

Thank you very much - works a treat!

reply

aman sandha
06/28/2009 - 05:49

its very nice solution

reply

GHolamreza
07/18/2009 - 12:16

Thanks alot

reply

Fernando
09/16/2009 - 20:31

Hello!

How to check image is RGB?

reply

Anonymous
11/03/2009 - 07:33

You can increase the performance of the "Fast and Complicated" method much by not multiplying so much and by caching values as the original. Width in local variables.

Much time to salvage there.

reply

Irfan
11/16/2009 - 11:54

I am talking about 3rd way.
It is awesome.
But we only work 1 byte and we can do calculation between that byte red blue green integer values.Can i do calculation between 2 byte using third way.

Thank you

reply

Dan Dan
11/17/2009 - 19:03

Hi There

Wonderful article.

I was owndering that when i save the new grayscale image to disk the size actually increases.

I am using the bmp.Save() method.

Why would that be?

I would expect that the file size would decrese.

Am i missing something?

Cheers
Dan

reply

Dimple
11/19/2009 - 00:44

Can any one tell me how to convert a grayscale or a multi color image to mono color gif image... selection of color is done by user dynamically at run time

reply

Prashanth
02/23/2010 - 22:20

Hi,

Very nice article.. Really helpful.. However I ve one doubt.. When using the byte* pointers logic, the pixel format of the grayscale image is still 24bpprgb right? But in general, grayscale images are in 8bbpindexed format. How can this pixelformat conversion be obtained?

reply

Abdulellah
03/06/2010 - 13:27

Thanks for this explained codes ,but I want know how merge an image with digtil signature in E-card.

reply

JALAL
03/10/2010 - 16:21

Thank you very much

reply

Irene
04/15/2010 - 03:46

I am new to C# image processing.

Is it possible to change the portion of original image into grayscale value and save it back to the original image itself.

reply

FasterThanYou
05/10/2010 - 19:31

You missed the fastest method of all:

Doing floating-point arithmetic is sloooow, computationally speaking. Doing it for an array of pixels is doubly slow.

Solution: Use your "2. Faster and Complicated", but use pre-calculated lookup tables. I have 3 arrays (R, G, B), each with values from 0 - 254, precalculated with the values you are multiplying above.

I processed the same image using that, and it took me 16ms. MUCH faster. Even your third method, while cool, still is performing many floating-point calculations (=slow).

The arrays I am using:

private static int[] RGBR = new int[] { 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54 };

private static int[] RGBG = new int[] { 0, 1, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23, 24, 24, 25, 26, 26, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 36, 37, 38, 39, 39, 40, 41, 41, 42, 43, 44, 44, 45, 46, 47, 47, 48, 49, 49, 50, 51, 52, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 59, 60, 61, 62, 62, 63, 64, 64, 65, 66, 67, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 74, 75, 76, 77, 77, 78, 79, 79, 80, 81, 82, 82, 83, 84, 84, 85, 86, 87, 87, 88, 89, 89, 90, 91, 92, 92, 93, 94, 94, 95, 96, 97, 97, 98, 99, 99, 100, 101, 102, 102, 103, 104, 104, 105, 106, 107, 107, 108, 109, 109, 110, 111, 112, 112, 113, 114, 114, 115, 116, 117, 117, 118, 119, 119, 120, 121, 122, 122, 123, 124, 124, 125, 126, 127, 127, 128, 129, 129, 130, 131, 132, 132, 133, 134, 134, 135, 136, 137, 137, 138, 139, 140, 140, 141, 142, 142, 143, 144, 145, 145, 146, 147, 147, 148, 149, 150, 150, 151, 152, 152, 153, 154, 155, 155, 156, 157, 157, 158, 159, 160, 160, 161, 162, 162, 163, 164, 165, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 172, 173, 174, 175, 175, 176, 177, 177, 178, 179, 180, 180, 181, 182, 182 };

private static int[] RGBB = new int[] { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };

reply

yosra
10/16/2010 - 00:29

Hi,can you pls, tell me is that array data's is for a real image and how you do it.

Cheers,

reply

MarkG
07/29/2010 - 11:17

Hi, I am trying to use your last method to change the brightness of a VERY large image ( 14770x22392 ~ 350MB), and it's not working, I think because the NewBitmaps needs the width and height as a int.

What can I do?

reply

mpistare
01/30/2011 - 19:51

I can help you, the answer is CUDA. I have experience in the field. mpistare@fceia.unr.edu.ar

reply

yosra
10/16/2010 - 00:31

very importnt information it help me mo,thanks alot

reply

Anonymous
11/12/2010 - 00:51

thank you!

reply

Genious
12/24/2010 - 15:50

Hello @FasterThankyou please you explaine your method...

reply

Bernard
01/12/2011 - 03:39

thank you so much!

reply

Chuck
02/15/2011 - 21:53

Thank you. Help me out.

reply

Hungry
03/11/2011 - 03:54

Is is possible to convert a tif to gray and ccitt 4 by 1 bit level?
I just want to resize a 32bit color image to a small size image.

reply

Hungry
03/11/2011 - 04:01

Bitmap bmpCanvas = new Bitmap(maxSize.Width, maxSize.Height);
Graphics g = Graphics.FromImage(bmpCanvas);

System.Drawing.Image img = System.Drawing.Image.FromFile("test.tif", true);
g.DrawImage(img, 0, 0);

sFileName = string.Format("{0}\\{1}.tif", path, Guid.NewGuid().ToString());

ImageCodecInfo codecInfo = GetEncoderInfo("image/tiff");
EncoderParameters iparams = new EncoderParameters(1);
Encoder iparam = Encoder.Compression;
EncoderParameter iparamPara = new EncoderParameter(iparam, (long)(EncoderValue.CompressionCCITT4));
iparams.Param[0] = iparamPara;
bmpCanvas = MakeGrayscale3(bmpCanvas);
bmpCanvas.Save(sFileName, codecInfo, iparams);
// CompressionCCITT4 is case parameter error, but CompressionLZW is success.
What's wrong?

reply

Seyi
03/16/2011 - 03:50

@FasterThanYou, can you please post the full source code for this conversion and perhaps also explain?

reply

Gihan Perera
04/10/2011 - 07:05

public void Convert2GrayScaleFast(Bitmap bmp)
{
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
int stopAddress = (int)p + bmData.Stride * bmData.Height;
while ((int)p != stopAddress)
{
p[0] = (byte)(.299 * p[2] + .587 * p[1] + .114 * p[0]);
p[1] = p[0];
p[2] = p[0];
p += 3;
}
}
bmp.UnlockBits(bmData);
}

im using this code for gray scale ..but it gives "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Can somes one please help me to get through this...
thanks.

reply

Anonymous
05/11/2011 - 14:53

Stride is rounded up to 4 bytes.
For example a 330 pixel width at 24 bits has a stride of 992, not 990. Your loop needs to skip those bytes and not overflow the bitmap memory space

reply

js
04/15/2011 - 22:11

thanks.. thanks a lot...

reply

Anonymous
05/24/2011 - 16:46

Include the fucking part of the code wehre it says "Using ...", so we can see what the fuck to include to be able to do this shit, since the microsoft documentation is the most retarded documentation in the world, and the language is so retarded, that it fucking doesn't even find the fucking shit that you fucking need to include, because of some fucking fucked up setting that fucking is something I as a programmer shouldn't fucking have to fucking ever fucking worry about.

reply

The Reddest
05/25/2011 - 10:05

Pro tip: put your cursor at the end of the type and hit Shift+Alt+F10. Visual Studio will popup the include you need.

P.S. I think your "fuck" key is stuck.

reply

rc
06/15/2011 - 15:21

Just wanted to say thanks for your article; it was a nice starting off point for getting into image manipulation.

There's one improvement, though, that will easily make your second method faster than your third:
Create a local variable to hold the width and height of the original image. Surprisingly, .Width and .Height are relatively expensive GDI calls that end up being evaluated for every pixel in the condition of the for loops. Changing these to a local variable reduces the run time from ~80ms to ~4ms on my machine with the flower test image you used(the matrix method takes ~20ms in comparison).

You can further optimize by creating local variables to hold the stride width, the starting byte address of the images, and changing how the current rows are computed, and use a precomputed lookup array, but obviously you won't see the same sort of performance gain as removing .Width and .Height calls. If you need to process thousands of larger images though, it can definitely be worth it.

Lastly, I started using FasterThanYou's lookup table but it looks like the values used to compute it were slightly different than the standards I've seen, so I rebuilt a table using R * .299, G * .587, and B *.114. TO use the table, use the original value of the rgb elements as indexes in the tables and add the returned values, ie:

byte grayByte = (byte) (RGBB[oRow[x * pixelSize]]
+ RGBG[oRow[x * pixelSize + 1]]
+ RGBR[oRow[x * pixelSize + 2]]);

And here are my modified tables using the above values if anyone is interested:

private static int[] RGBR = new int[] { 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 59, 59, 59, 60, 60, 60, 60, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63, 64, 64, 64, 65, 65, 65, 65, 66, 66, 66, 67, 67, 67, 68, 68, 68, 68, 69, 69, 69, 70, 70, 70, 71, 71, 71, 71, 72, 72, 72, 73, 73, 73, 74, 74, 74, 74, 75, 75, 75, 76, 76, 76 };

private static int[] RGBG = new int[] { 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12, 13, 14, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 29, 30, 31, 31, 32, 32, 33, 33, 34, 35, 35, 36, 36, 37, 38, 38, 39, 39, 40, 41, 41, 42, 42, 43, 43, 44, 45, 45, 46, 46, 47, 48, 48, 49, 49, 50, 50, 51, 52, 52, 53, 53, 54, 55, 55, 56, 56, 57, 58, 58, 59, 59, 60, 60, 61, 62, 62, 63, 63, 64, 65, 65, 66, 66, 67, 68, 68, 69, 69, 70, 70, 71, 72, 72, 73, 73, 74, 75, 75, 76, 76, 77, 77, 78, 79, 79, 80, 80, 81, 82, 82, 83, 83, 84, 85, 85, 86, 86, 87, 87, 88, 89, 89, 90, 90, 91, 92, 92, 93, 93, 94, 95, 95, 96, 96, 97, 97, 98, 99, 99, 100, 100, 101, 102, 102, 103, 103, 104, 104, 105, 106, 106, 107, 107, 108, 109, 109, 110, 110, 111, 112, 112, 113, 113, 114, 114, 115, 116, 116, 117, 117, 118, 119, 119, 120, 120, 121, 122, 122, 123, 123, 124, 124, 125, 126, 126, 127, 127, 128, 129, 129, 130, 130, 131, 131, 132, 133, 133, 134, 134, 135, 136, 136, 137, 137, 138, 139, 139, 140, 140, 141, 141, 142, 143, 143, 144, 144, 145, 146, 146, 147, 147, 148, 149, 149, 150 };

private static int[] RGBB = new int[] { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29 };

reply

Jaffer Mumtaz
12/05/2011 - 02:40

Hi,

Can you guide me on how to do the same function but from grayscale image to color. My input will be a raw colorless image and as output I want colored image.

Any pointers will be helpful.

Thanks,

Jaffer Mumtaz

reply

mustafa
07/05/2011 - 04:27

hi, can anybody help me about plate recognition C# algorithm

reply

umer draz
07/28/2011 - 13:50

I am getting GDI+ error using the algorithm3. can anyone help. i am using it in a windows service.

reply

Anonymous
08/22/2011 - 06:15

I just modified the function, so that it can convert to 8-bit format. Use this color pallette:

Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
ColorPalette monoPalette = bmp.Palette;
for (int i = 0; i < 256; i++) {
monoPalette.Entries[i] = Color.FromArgb(i, i, i);
}

b8.Palette = monoPalette;

private static Bitmap Make8bitGrayscale(Bitmap original)
{
unsafe
{
//create an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format8bppIndexed);

//lock the original bitmap in memory
BitmapData originalData = original.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

//lock the new bitmap in memory
BitmapData newData = newBitmap.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

//number of bytes per pixel in original image
int pixelSize = 3;

for (int y = 0; y < original.Height; y++)
{
//get the data from the original image
byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

//get the data from the new image
byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

for (int x = 0; x < original.Width; x++)
{
//create the grayscale version
/*byte grayScale = // Color image as source
(byte)((oRow[x * pixelSize] * .11) + //B
(oRow[x * pixelSize + 1] * .59) + //G
(oRow[x * pixelSize + 2] * .3)); //R*/
byte grayScale = oRow[x * pixelSize]; // Greyscale

//set the new image's pixel to the grayscale version
nRow[x] = grayScale; // only one byte per pixel in greyscale format
//nRow[x * pixelSize] = grayScale; //B
//nRow[x * pixelSize + 1] = grayScale; //G
//nRow[x * pixelSize + 2] = grayScale; //R
}
}

//unlock the bitmaps
newBitmap.UnlockBits(newData);
original.UnlockBits(originalData);

return newBitmap;
}
}

reply

Anonymous
10/09/2011 - 14:58

can you send me the project with .sln ??

reply

smoli
10/11/2011 - 10:32

Hello,

i have a problem with displaying the grayescle image...I do:
d.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

do i need in de form designer some event?

reply

Photo Editor
01/08/2012 - 04:46

Awesome photo editing tutorial collection. Great resources for me as a photo editing professional. Thanks for share these very useful links & this post. Fotoclipping Love this post. Visit and take service.

reply

Anonymous
01/16/2012 - 06:09

i want morphology codes like dilation erosion for processing RGB images can anyone help me.

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.