如何:使用颜色矩阵对单色进行变换

来源:互联网 发布:php 二维数组过滤相同 编辑:程序博客网 时间:2024/05/17 22:45

GDI+ 提供用于存储和操作图像的 Image 和 Bitmap 类。 Image 和 Bitmap 对象用一个 32 位数字存储每个像素的颜色:红、绿、蓝和 Alpha 各 8 位。 这四个分量的值都是 0 到 255,其中 0 表示没有亮度,255 表示最大亮度。 alpha 分量指定颜色的透明度:0 表示完全透明,255 表示完全不透明。

颜色矢量采用 4 元组形式(红色、绿色、蓝色、alpha)。 例如,颜色矢量 (0, 255, 0, 255) 表示一种没有红色和蓝色但绿色达到最大亮度的不透明颜色。

表示颜色的另一种惯例是用数字 1 表示亮度达到最大。 通过使用这种约定,上一段中描述的颜色将可以由矢量 (0, 1, 0, 1) 表示。 在执行颜色变换时,GDI+ 遵循使用 1 为最大亮度的惯例。

可通过用 4×4 矩阵乘以这些颜色矢量将线性变换(旋转和缩放等)应用到颜色矢量中。 但是,您不能使用 4×4 矩阵进行平移(非线性)。 如果在每个颜色矢量中再添加一个虚拟的第 5 坐标(例如,数字 1),则可使用 5×5 矩阵应用任何组合形式的线性变换和平移。 由线性变换组成的后跟平移的变换称为仿射变换。

例如,假设您希望从颜色 (0.2, 0.0, 0.4, 1.0) 开始并应用下面的变换:

  1. 将红色分量乘以 2。

  2. 将 0.2 添加到红色、绿色和蓝色分量中。

下面的矩阵乘法将按照列出的顺序进行这对变换。

重新着色

颜色矩阵的元素按照先行后列(从 0 开始)的顺序进行索引。 例如,矩阵 M 的第五行第三列由 M[4][2] 表示。

5×5 单位矩阵(在下面的插图中显示)在对角线上为 1,在其他任何地方为 0。 如果用单位矩阵乘以颜色矢量,则颜色矢量不会发生改变。 形成颜色变换矩阵的一种简便方法是从单位矩阵开始,然后进行较小的改动以产生所需的变换。

重新着色

有关矩阵和变换的更详细的讨论,请参见坐标系统和变形。

示例

下面的示例采用一个使用一种颜色 (0.2, 0.0, 0.4, 1.0) 的图像,并应用上一段中描述的变换。

下面的插图在左侧显示原来的图像,在右侧显示变换后的图像。

颜色

下面示例中的代码使用以下步骤进行重新着色:

  1. 初始化 ColorMatrix 对象。

  2. 创建一个 ImageAttributes 对象,并将 ColorMatrix 对象传递给 ImageAttributes 对象的 SetColorMatrix 方法。

  3. 将 ImageAttributes 对象传递给 Graphics 对象的 DrawImage 方法。

C#
VB
Image image = new Bitmap("InputColor.bmp");ImageAttributes imageAttributes = new ImageAttributes();int width = image.Width;int height = image.Height;float[][] colorMatrixElements = {    new float[] {2,  0,  0,  0, 0},        // red scaling factor of 2   new float[] {0,  1,  0,  0, 0},        // green scaling factor of 1   new float[] {0,  0,  1,  0, 0},        // blue scaling factor of 1   new float[] {0,  0,  0,  1, 0},        // alpha scaling factor of 1   new float[] {.2f, .2f, .2f, 0, 1}};    // three translations of 0.2ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);imageAttributes.SetColorMatrix(   colorMatrix,   ColorMatrixFlag.Default,   ColorAdjustType.Bitmap);e.Graphics.DrawImage(image, 10, 10);e.Graphics.DrawImage(   image,   new Rectangle(120, 10, width, height),  // destination rectangle    0, 0,        // upper-left corner of source rectangle    width,       // width of source rectangle   height,      // height of source rectangle   GraphicsUnit.Pixel,   imageAttributes);
原创粉丝点击