Principle and source code for converting a color image to a grayscale image

Source: Internet
Author: User
Tags color representation

In image processing, most of the processing methods need to convert a color image into a grayscale image in advance for calculation and recognition.
The principle of converting a grayscale image from a color chart is as follows:
We know that the color bitmap consists of three components: R/G/B, and its file storage format is
Bitmapfileheader + bitmapinfoheader:
If it is a 24-bit true color chart, each point is represented by three bytes respectively R/G/B, so here we follow the color information of the image directly;
For an 8-bit (256-color), 4-bit (16-color), and 1-bit (monochrome) graph, the following is the color palette data, an array of the rgbquad type, its length is determined by bitmapinfoheader. this parameter is determined by biclrused.
Then the image data is followed closely by the image data (24 bitmap is the real image data, and others are the index data of the palette ).
A grayscale image is an image that only contains brightness information and does not contain color information. It is like a black-and-white image we usually see: The brightness changes continuously from dark to bright. Therefore, to represent a grayscale image, you need to quantify the brightness value. Usually divided into 0 to 255, a total of 256 levels, of which 0 is the lowest (black), 255 is the brightest (white ). In addition to RGB, YUV is also used to represent colors. TV signals use a color representation method similar to YUV. In this representation method, the physical meaning of the Y component is the brightness. The Y component contains all the information of the grayscale image and can represent a grayscale image with only the y fraction.
The y conversion formula from RGB to YUV space is as follows:
Y = 0.299r + 0.587G + 0.114b
In Windows, a 16-bit or higher graph is a little different from the following graph. A 16-bit or lower graph uses a color palette to select a specific color, each cell in the palette contains four bytes, one of which is transparency. The specific pixel values store indexes, which are 1, 2, 4, and 8 bits. A 16-bit or more image uses pixels to represent the color.
How can we convert a color image to a grayscale image?
There is a palette in a grayscale image. First, you must determine the specific color values of the palette. As we mentioned earlier, the three components of the grayscale image are equal.
When converted to 8 bits, the color palette contains 256 colors, each of which ranges from 0 to 255, and the three components are equal.
When it is converted to 4 bits, the color palette contains 16 colors, and 255 color values are evenly divided at an equal interval. All three components are equal.
When the color is converted to 2 bits, the four colors in the color palette are evenly divided into 255 colors at an equal interval, and the three components are equal.
When the color is converted to one digit, the two colors in the color palette are 0 and 255, indicating black and white.
When the color is converted to a gray scale, the corresponding value is calculated according to the formula. The value is actually the brightness level. The brightness ranges from 0 to 255. Because different bits have different brightness levels, the specific values of Y are as follows:
Y = y/(1 <(8-number of digits converted ));
Therefore, to convert the image into a grayscale image and store it as a visible image, you need to convert it as follows:
For an image with more than 16 digits without a color palette, you only need to convert the image data to the same gray value based on the digits of each vertex.
For images with less than 16 bits, you need to modify the value of the color palette and modify the gray index based on the number of digits occupied by each vertex.

Modify the color in the color palette (two different algorithms for calculating the gray value)

R = G = B = (R + G + B)/3 or R = G = B = (0.30r + 0.59G + 0.11b)

---------------------------------------------------------------

Lpbyte lpgraybit = (lpbyte) lpgray;
If (is_win30_dib (lpbit ))
{
Lpgraybit + = pheader-> bisize + 256 * sizeof (rgbquad); // move the pointer to the data area of the newly created grayscale image
Lpbit + = sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad); // This is moved to the data area of the original image
}
Else
{
Lpgraybit + = pheader-> bisize + 256 * sizeof (rgbtriple );
Lpbit + = sizeof (bitmapinfoheader) + 256 * sizeof (rgbtriple );
}
Int ncountbit = pheader-> bibitcount;

If (ncountbit> 8) // If bibitcount great eight then translate
{
Int nrgb = (nwide * ncountbit + 31)/32 * 4-nwide * ncountbit/8;
Int ngrey = (nwide * 8 + 31)/32 * 4-nwide;
For (I = 0; I <nheight; I ++)
{
For (j = 0; j <nwide; j ++)
* Lpgraybit ++ = (* lpbit ++) * 0.299 + (* lpbit ++) * 0.587 + (* lpbit ++) * 0.114;
Lpgraybit + = ngrey;
Lpbit + = nrgb;
}
: Globalunlock (hglobal) hdib );
: Globalfree (hglobal) hdib );
Hdib = hgreydib;
: Globalunlock (hglobal) hgreydib );
}

Note the color palette
---------------------------------------------------------------

// (14) convert bitmap into an 8-bit grayscale image $ $
Void deflianbiao: turntogrey (lpstr lpdib)
{

Lpstr lpnewdibbits; // pointer to the pixel at the beginning of the dib grayscale image
Unsigned char * IRED;
Unsigned char * igreen;
Unsigned char * iblack;
Long I, J; // cyclic variable
Unsigned char * lpdest;
// Lpdest = (unsigned char *): malloc (lheight * lwidth );
Lpdest = new unsigned char [lheight * lwidth];
Int n = 0;
Int picturebits;
Picturebits = dibbits (lpdib)/8;
If (picturebits = 0) // | picturebits <= 1) // The image format of non-integer bytes is not processed.
{
Afxmessagebox ("Data Format of the image is not supported ");
Return;
}
Rgbquad * lprgbquad;
Lprgbquad = (rgbquad *) & lpdib [sizeof (bitmapinfoheader)]; // the color palette is followed by the Bitmap header.
Unsigned char * lpsrc;
N = 0;
If (picturebits = 1) // 256 color bitmap
For (j = 0; j <lheight; j ++)
For (I = 0; I <lwidth; I ++)
{
Lpsrc = (unsigned char *) lpdibbits + llinebytes * j + I;
IRED = & lprgbquad [* lpsrc]. rgbred;
Igreen = & lprgbquad [* lpsrc]. rgbgreen;
Iblack = & lprgbquad [* lpsrc]. rgbblue;
Lpdest [N] = (unsigned char) (0.299 * (* IRED) + 0.587 * (* igreen) + 0.114 * (* iblack ));
N ++;
}
Else // 24/32-bit color bitmap
{

Llinebytes = widthbytes (lwidth * 8 * picturebits );
For (j = 0; j <lheight; j ++)
For (I = 0, n = 0; I <picturebits * lwidth; I + = picturebits, N ++)
{

IRED = (unsigned char *) lpdibbits + llinebytes * j + I + 2;
Igreen = (unsigned char *) lpdibbits + llinebytes * j + I + 1;
Iblack = (unsigned char *) lpdibbits + llinebytes * j + I;
Lpdest [J * lwidth + N] = (unsigned char) (0.299 * (* IRED) + 0.587 * (* igreen) + 0.114 * (* iblack ));

}
}
Lpbitmapinfoheader lpbi; // bitmap information Header
// Read the bitmapinfo structure and initialize the pointer
Lpbi = (lpbitmapinfoheader) lpdib; // [sizeof (bitmapfileheader)];
Lpbi-> bibitcount = 8;
// Set the 256-color grayscale palette
For (I = 0; I <256; I ++)
{
Lprgbquad [I]. rgbred = (unsigned char) I; // read the red component
Lprgbquad [I]. rgbgreen = (unsigned char) I; // read the green component
Lprgbquad [I]. rgbblue = (unsigned char) I; // read the red component
Lprgbquad [I]. rgbreserved = 0; // Reserved Bit
}
Lpnewdibbits =: finddibbits (lpdib); // locate the starting position of the DIB image pixel
Llinebytes = widthbytes (lwidth * 8 );
For (j = 0; j <lheight; j ++)
For (I = 0; I <lwidth; I ++)
{

Lpsrc = (unsigned char *) lpnewdibbits + llinebytes * j + I;
* Lpsrc = lpdest [J * lwidth + I];
}
//: Free (void *) lpdest );
Delete lpdest;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.