Image processing-grayscale processing (black/white effect)

Source: Internet
Author: User
Conversion from: Image Processing: black/white effect (grayscale processing)

1 .:

2. Implementation principle:
Grayscale is to make the three color components R, G, and B of the same color value. Because the color value range is [0,255 ],
There are only 256 levels, that is, grayscale images can only represent 256 gray colors. There are three common processing methods:
* Maximum Method (maximum): r = G = B = max (R, G, B). After processing, the brightness of the grayscale image is high.
* Mean Value Method (average): r = G = B = (R + G + B)/3. After processing, the brightness of the grayscale image is soft.
* Weighted average ):
R = G = B = WR * r + WG * g + WB * B. wr, WG, and WB are the weights of R, G, and B respectively.
When different values are obtained, gray images of different gray scales can be formed. Because human eyes have the highest sensitivity to green, red is the second,
The blue color is the lowest, so when WG> Wr> WB, The grayscale image is more consistent with the human visual experience.
Generally, wR = 30%, WG = 59%, WB = 11%, and the gray scale of the image is the most reasonable.
The following programs use wR = 70%, WG = 20%, WB = 10%, and the results are better.

3. Implementation Code:


1 Public Enum algorithmstype
2 {
3 maxvalue, // Maximum Value Method
4 averagevalue, // Average Method
5 weightaverage // weighted average method
6}

Public static image gray (image IMG, algorithmstype algo)
{
Int width = IMG. width;
Int Height = IMG. height;

Bitmap BMP = new Bitmap (IMG );

// Set instance bitmapdata Information
Rectangle rect = new rectangle (0, 0, width, height );
Imagelockmode mode = imagelockmode. readwrite;
Pixelformat format = pixelformat. format32bppargb;

// Lock BMP to system memory
Bitmapdata DATA = BMP. lockbits (rect, mode, format );

// Obtain the address of the first pixel in the bitmap.
Intptr = data. scan0;

Int numbytes = width * height * 4;
Byte [] rgbvalues = new byte [numbytes];

// Copy the BMP data to the declarative Array
Marshal. Copy (PTR, rgbvalues, 0, numbytes );

For (INT I = 0; I <rgbvalues. length; I + = 4)
{
Int value = 0;
Switch (algo)
{
// Maximum Value Method
Case algorithmstype. maxvalue:
Value = rgbvalues [I]> rgbvalues [I + 1]? Rgbvalues [I]: rgbvalues [I + 1];
Value = value> rgbvalues [I + 2]? Value: rgbvalues [I + 2];
Break;
// Average Method
Case algorithmstype. averagevalue:
Value = (INT) (rgbvalues [I] + rgbvalues [I + 1] + rgbvalues [I + 2])/3 );
Break;
// Weighted average method
Case algorithmstype. weightaverage:
Value = (INT) (rgbvalues [I] * 0.1 + rgbvalues [I + 1] * 0.2
+ Rgbvalues [I + 2] * 0.7 );
Break;
}

// Change the R, G, and B values in the array to the calculated values.
For (Int J = 0; j <3; j ++)
{
Rgbvalues [I + J] = (byte) value;
}
}

// Copy data to memory pointer
Marshal. Copy (rgbvalues, 0, PTR, numbytes );

// Unlock BMP from system memory
BMP. unlockbits (data );

Return (image) BMP;

}

 

4. Description:
For more information about how to use the getpixel method and setpixel method, see Soften Processing;
For the unsafe mode, see method 1 of salt and pepper noise (Miscellaneous;
The implementation method in this example is the same as that in method 2 of salt and pepper noise (Miscellaneous.

 

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.