65.9K
CodeProject is changing. Read more.
Home

Image (colour) manipulation with ColorMatrix

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2 votes)

May 4, 2010

CPOL
viewsIcon

22717

Below is a code snippet derived from a few of the ideas in the article at Matrix Operations for Image Manipulation[^].Whilst this code is C# it should be easily convertible to VB or Managed C++.These three simple Methods make use of the ColorMatrix to achieve variations in the colour...

Below is a code snippet derived from a few of the ideas in the article at Matrix Operations for Image Manipulation[^].

Whilst this code is C# it should be easily convertible to VB or Managed C++.

These three simple Methods make use of the ColorMatrix to achieve variations in the colour saturation.

This is the Method that does all the work.

		private void ApplySaturation(float saturation)
		{
			float rWeight = 0.3086f;
			float gWeight = 0.6094f;
			float bWeight = 0.0820f;

			float a = (1.0f - saturation) * rWeight + saturation;
			float b = (1.0f - saturation) * rWeight;
			float c = (1.0f - saturation) * rWeight;
			float d = (1.0f - saturation) * gWeight;
			float e = (1.0f - saturation) * gWeight + saturation;
			float f = (1.0f - saturation) * gWeight;
			float g = (1.0f - saturation) * bWeight;
			float h = (1.0f - saturation) * bWeight;
			float i = (1.0f - saturation) * bWeight + saturation;

			// Create a Graphics
			using (Graphics gr = this.CreateGraphics())
			{
				gr.Clear(this.BackColor);
				// Create a Bitmap
				using (Bitmap curBitmap = new Bitmap("roses.jpg"))
				{
					// ColorMatrix elements
					float[][] ptsArray = {
											 new float[] {a,  b,  c,  0, 0},
											 new float[] {d,  e,  f,  0, 0},
											 new float[] {g,  h,  i,  0, 0},
											 new float[] {0,  0,  0,  1, 0},
											 new float[] {0, 0, 0, 0, 1}
										 };
					// Create ColorMatrix
					ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
					// Create ImageAttributes
					ImageAttributes imgAttribs = new ImageAttributes();
					// Set color matrix
					imgAttribs.SetColorMatrix(clrMatrix,
						ColorMatrixFlag.Default,
						ColorAdjustType.Default);
					// Draw Image with no effects
					gr.DrawImage(curBitmap, 0, 0, 200, 200);
					// Draw Image with image attributes
					gr.DrawImage(curBitmap,
						new Rectangle(205, 0, 200, 200),
						0, 0, curBitmap.Width, curBitmap.Height,
						GraphicsUnit.Pixel, imgAttribs);
				}
			}
		}

This will convert an image to GreyScale. Use a saturation code of zero.

    public void GreyScale()
    {
        ApplySaturation(0.0f);
    }

This will alter the saturation of an image. Use a code saturation of 0.5f.

    public void Fade()
    {
        ApplySaturation(0.5f);
    }

This will give the complementary colours for an image. Use a saturation of -1.0f.

    public void Complement()
    {
        ApplySaturation(-1.0f);
    }

Obviously you should substitute one of your own images for "roses.jpg", the more colourful the better.