# Hi, how to convert a grayscale image into 8 bit image?

• 03-15-2005
Alexanderbinich
Hi, how to convert a grayscale image into 8 bit image?
Hi,

anyone can tell me how to convert a grayscale image into 8 bit image? I am trying to use CvFindContour function base on OpenCV, the CvFindContour only supports 8 bit unsigned image. Thanks for your help!

Regards,
Alexander
• 03-15-2005
pianorain
If you're using Windows, good ole MSPaint will do it for you.
• 03-15-2005
VirtualAce
Um....greyscale is an 8-bit image.

Even though the pixels might be RGB's greyscale means the red=green=blue. So if you just iterate through the data and extract the red's from the DWORD you have your image.

To create the palette just iterate through and count how many diff colors there are. Just in case you were wondering, there will be exactly 256 colors. It's your job to find them, write the values into the palette data, and then write the file.

This will extract the values from a DWORD on a 32-bit ARGB.
#define RED_FROM_ARGB32(color) (BYTE) ( ( (color) & (0xFF0000) )>>(16) )
#define GREEN_FROM_ARGB32(color) (BYTE) ( ( (color) & (0xFF00) )>> (8) )
#define BLUE_FROM_ARGB(color) (BYTE) ( (color) & (0xFF) )

And this will make an ARGB
#define ARGB(r,g,b,alpha) (DWORD) ( ( (alpha)<<(24)) + ((r)<<(16)) + ((g)<<(8)) + (b) )

This will make an ARGB into a greyscale value.

int value=(int)((red+green+blue)/3);
ARGB(value,value,value,0);
• 03-15-2005
Alexanderbinich
Sorry, friends, I made a mistake. My picture is opened from my E:\pictures file and it is .bmp format. After using threshod base on OpenCV , I try to use CvFindContour but it says it only support " 8uc1 image ". Do not know why.

Regards,
Alexander