次の方法で共有


カラー行列の使用による単一色の変換

GDI+ は、イメージを格納したり操作したりするために、Image クラスおよび Bitmap クラスを提供しています。Image オブジェクトおよび Bitmap オブジェクトは、各ピクセルの色として、赤、緑、青、およびアルファそれぞれに 8 ビットずつ割り当てた 32 ビットの数値を格納します。4 つの各要素は、0 から 255 までの数値で表され、0 は輝度がないことを表し、255 は最大の輝度を表します。アルファ要素は、色の透明度を指定します。0 は完全な透明を表し、255 は完全な不透明を表します。

カラー ベクタは、4 つの要素 (赤、緑、青、アルファ) から成る集合です。たとえば、カラー ベクタ (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 の 5 行目および 3 列目のエントリは、M [4][2] と表記されます。

次の図に示す 5 × 5 の単位行列には、対角線上に 1、それ以外のすべての位置に 0 が格納されています。カラー ベクタとこの単位行列を掛け合わせても、そのカラー ベクタは変化しません。カラー変換の行列を形成する簡単な方法として、単位行列から着手し、必要な変換を生成できるように微調整していく方法があります。

行列と変換の詳細については、「座標系と変換」を参照してください。

全体が 1 つの色 (0.2, 0.0, 0.4, 1.0) であるイメージに、上の段落で説明した変換を適用する例を次に示します。

Dim image = New Bitmap("InputColor.bmp")
Dim imageAttributes As New ImageAttributes()
Dim width As Integer = image.Width
Dim height As Integer = image.Height
      
' The following matrix consists of the following transformations:
' red scaling factor of 2
' green scaling factor of 1
' blue scaling factor of 1
' alpha scaling factor of 1
' three translations of 0.2
Dim colorMatrixElements As Single()() = { _
   New Single() {2, 0, 0, 0, 0}, _
   New Single() {0, 1, 0, 0, 0}, _
   New Single() {0, 0, 1, 0, 0}, _
   New Single() {0, 0, 0, 1, 0}, _
   New Single() {0.2F, 0.2F, 0.2F, 0, 1}}

Dim colorMatrix As 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), _
   0, _
   0, _
   width, _
   height, _
   GraphicsUnit.Pixel, _
   imageAttributes)
[C#]
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.2

ColorMatrix 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);

次の図は、左側に元のイメージ、右側に変換後のイメージを示しています。

上の例の中のコードでは、次のような手順で色の変更が行われます。

  1. ColorMatrix オブジェクトを初期化します。
  2. ImageAttributes オブジェクトを作成し、ColorMatrix オブジェクトをその ImageAttributes オブジェクトの SetColorMatrix メソッドに渡します。
  3. ImageAttributes オブジェクトを Graphics オブジェクトの DrawImage メソッドに渡します。