Home > Backend Development > C++ > How Can I Convert an Image to Grayscale Using a 16-Bit per Pixel Format?

How Can I Convert an Image to Grayscale Using a 16-Bit per Pixel Format?

Susan Sarandon
Release: 2025-01-17 13:06:10
Original
639 people have browsed it

How Can I Convert an Image to Grayscale Using a 16-Bit per Pixel Format?

Grayscale conversion method for 16-bit pixel images

In image processing, grayscale conversion typically sets the red, green, and blue (RGB) components of each pixel to a brightness value. However, there is a way to convert an image to grayscale using a 16-bit-per-pixel format.

Use the PixelFormat constructor

To convert an image to grayscale using this method, you can use the PixelFormat constructor of the Bitmap class. An example is as follows:

<code class="language-c#">Bitmap grayScaleBP = new System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);</code>
Copy after login

This will create a new bitmap with a size of 2x2 pixels and a pixel format of 16 bits per pixel grayscale.

Convert existing image to grayscale

To convert an existing image to grayscale, you can use a loop to loop through the pixels and set them to a grayscale color. An example is as follows:

<code class="language-c#">// 代码片段略,此处需要补充完整代码以实现灰度转换</code>
Copy after login

This code goes through each pixel in the bitmap c and sets the red component to the brightness value and the green and blue components to zero, effectively converting it to grayscale. The resulting bitmap d contains a grayscale version of the original image.

Quick way to use ColorMatrix

Convert grayscale images faster using ColorMatrix:

<code class="language-c#">public static Bitmap MakeGrayscale3(Bitmap original)
{
    // 创建一个与原始图像大小相同的空白位图
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);

    // 从新图像获取图形对象
    using (Graphics g = Graphics.FromImage(newBitmap)) {
        // 创建灰度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}
            });

        // 创建一些图像属性
        using (ImageAttributes attributes = new ImageAttributes()) {
            // 设置颜色矩阵属性
            attributes.SetColorMatrix(colorMatrix);

            // 使用灰度颜色矩阵在新图像上绘制原始图像
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                        0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
        }
    }

    // 返回新的位图
    return newBitmap;
}</code>
Copy after login

This method uses a ColorMatrix to perform a grayscale conversion, which is much faster than manually iterating through the pixels.

The above is the detailed content of How Can I Convert an Image to Grayscale Using a 16-Bit per Pixel Format?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template