Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Red, Blue, and Green Filters in C#

4/9/2009

I remember when the Matrix came out, I was really curious how they ended up getting that weird greenish look on everything. I was young, I knew nothing about image processing but in my head I figured it had to be the lighting. I kept thinking "Wow, that must have been really annoying having a greenish light on everything"... Like I said, I was young... Anyway, eventually I found out about green filters (as well as blue and red filters). For those of you that don't know what that is, it's simply taking the green (or red or blue or any color really) component of the image, overlaying it over the original image with a certain opacity to give the image a specific tint.

Now after updating my utility library, I noticed that I had never added that functionality. In fact I never added anything to help you accomplish this if you wanted to create the effect. In order to help, I've added some functions to get the green, red, and blue channels individually and as always I'm going to show you how to do that yourself:

   1: /// <summary>
   2: /// Gets the Green filter for an image
   3: /// </summary>
   4: /// <param name="Image">Image to change</param>
   5: /// <returns>A bitmap object</returns>
   6: public static Bitmap GreenFilter(Bitmap Image)
   7: {
   8:     ColorMatrix TempMatrix = new ColorMatrix();
   9:     TempMatrix.Matrix = new float[][]{
  10:                     new float[] {0, 0, 0, 0, 0},
  11:                     new float[] {0, 1, 0, 0, 0},
  12:                     new float[] {0, 0, 0, 0, 0},
  13:                     new float[] {0, 0, 0, 1, 0},
  14:                     new float[] {0, 0, 0, 0, 1}
  15:                 };
  16:     return TempMatrix.Apply(Image);
  17: }
  18:  
  19: /*
  20: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
  21: 
  22: Permission is hereby granted, free of charge, to any person obtaining a copy
  23: of this software and associated documentation files (the "Software"), to deal
  24: in the Software without restriction, including without limitation the rights
  25: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26: copies of the Software, and to permit persons to whom the Software is
  27: furnished to do so, subject to the following conditions:
  28: 
  29: The above copyright notice and this permission notice shall be included in
  30: all copies or substantial portions of the Software.
  31: 
  32: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  38: THE SOFTWARE.*/
  39:  
  40: #region Usings
  41: using System.Drawing;
  42: using System.Drawing.Imaging;
  43: #endregion
  44:  
  45: namespace Utilities.Media.Image
  46: {
  47:     /// <summary>
  48:     /// Helper class for setting up and applying a color matrix
  49:     /// </summary>
  50:     public class ColorMatrix
  51:     {
  52:         #region Constructor
  53:  
  54:         /// <summary>
  55:         /// Constructor
  56:         /// </summary>
  57:         public ColorMatrix()
  58:         {
  59:         }
  60:  
  61:         #endregion
  62:  
  63:         #region Properties
  64:  
  65:         /// <summary>
  66:         /// Matrix containing the values of the ColorMatrix
  67:         /// </summary>
  68:         public float[][] Matrix { get; set; }
  69:  
  70:         #endregion
  71:  
  72:         #region Public Functions
  73:  
  74:         /// <summary>
  75:         /// Applies the color matrix
  76:         /// </summary>
  77:         /// <param name="OriginalImage">Image sent in</param>
  78:         /// <returns>An image with the color matrix applied</returns>
  79:         public Bitmap Apply(Bitmap OriginalImage)
  80:         {
  81:             Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
  82:             using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
  83:             {
  84:                 System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
  85:                 using (ImageAttributes Attributes = new ImageAttributes())
  86:                 {
  87:                     Attributes.SetColorMatrix(NewColorMatrix);
  88:                     NewGraphics.DrawImage(OriginalImage,
  89:                         new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
  90:                         0, 0, OriginalImage.Width, OriginalImage.Height,
  91:                         GraphicsUnit.Pixel,
  92:                         Attributes);
  93:                 }
  94:             }
  95:             return NewBitmap;
  96:         }
  97:  
  98:         #endregion
  99:     }
 100: }

The code above is very similar to the sepia tone and black and white code that I've shown earlier. This code uses a ColorMatrix with both the blue and red channels ignored (only focusing on the green channel). We can switch this to the red or blue channel or any combination of them that you want. After the ColorMatrix is created, it (well GDI+) just does a pass on the image using that filter. In return it spits back all green that is found in the image. Past that, there isn't much to the code itself.

Now if you try out the code, you'll notice that it's VERY green. I don't remember people in the Matrix looking like that, so how do we give our images the same look? As I said above, we need to overlay the image we get from here over the original image with a specific opacity. In order to do this, I'm going to use the Watermark function from my utility library:

   1: Bitmap TempImage = Utilities.Media.Image.Image.GreenFilter(ORIGINAL IMAGE LOCATION);
   2: Bitmap TempImage2 = (Bitmap)Bitmap.FromFile(ORIGINAL IMAGE LOCATION);
   3: Bitmap TempImage3 = Utilities.Media.Image.Image.Watermark(TempImage2, TempImage, 0.2f, 0, 0, Color.FromArgb(255, 0, 255));
   4: TempImage.Dispose();
   5: TempImage2.Dispose();
   6: TempImage3.Save(FINAL IMAGE LOCATION);
   7: TempImage3.Dispose();

You'll have to update the location of the original and final image locations but that's it. Doing the above creates an image with a light green filter similar to what was done in the Matrix but definitely play with the opacity to get the effect that you want. Anyway, I hope this helps you out, so give it a try, leave feedback, and happy coding.



Comments