C# How to: Image Solarise

Article Purpose

The focus of this is set on exploring the concept of . In this the tasks required to perform   are entirely implemented on pixel data level. manipulation is only implemented in the form of updating the individual Alpha, Red, Green and Blue colour components expressed by pixels.

Sample source code

This is accompanied by a sample source code Visual Studio project which is available for download here.

Download Sample Source Code

What is Image Solarisation?

can be described as a form of Image inversion/colour inversion. A distinction can be made  between solarisation and colour inversion when taking into regard threshold values implemented when performing image solarisation.

Colour inversion can be implemented by subtracting each colour component from the value 255 and then assigning the relevant colour component to the result of subtraction. Colour inversion in terms of a formula can be expressed as follows:

R = 255 – R1

G = 255 – G1

B = 255 – B1

In this expression R1 G2 and B1 represent the original value of a pixel’s Red, Green and Blue colour components prior to being updated. when expressed as a formula could be represented as follows:

R = R1

If R1 < RT   Then R = 255 – R1

G = G1

If G1 < GT   Then G = 255 – G1

B = B1

If B1 < BT   Then B = 255 – B1

When implementing this expression R1 G1 and B1 represent the original value of a pixel’s Red, Green and Blue colour components prior to being updated. RT GT and BT in this scenario represent configurable threshold values applicable to individual colour components. If a colour component’s original value equates to less than the corresponding threshold value only then should colour inversion be implemented.

Using the sample Application

The concepts discussed in this are implemented/tested by means of a Windows Forms application. When using the sample application a user has the ability to browse the local file system in order to specify a source/input image. The user interface includes three trackbar controls, each related to a colour component threshold value. Threshold values can range from 0 to 255 inclusive. A threshold value of 0 results in no change, whereas a value of 255 equates to regular colour inversion.

Updated/filtered images can be saved to the local file system at any stage by clicking the ‘Save Image’ button.

The following image is a screenshot of the Image Solarise sample application in action:


ImageSolarise_Screenshot


The Solarise Extension Method

The sample source code provides the definition for the Solarise extension method. The method has been defined as an extension method targeting the class.

The following code snippet details the implementation of the Solarise extension method:

 public static Bitmap Solarise(this Bitmap sourceBitmap, byte blueValue, 
                              byte greenValue, byte redValue) 
{ 
    BitmapData sourceData = sourceBitmap.LockBits(new Rectangle  (0, 0, 
                            sourceBitmap.Width, sourceBitmap.Height), 
                            ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 

byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
sourceBitmap.UnlockBits(sourceData);
byte byte255 = 255;
for (int k = 0; k + 4 < pixelBuffer.Length; k += 4) { if (pixelBuffer[k] < blueValue) { pixelBuffer[k] = (byte)(byte255 - pixelBuffer[k]); }
if (pixelBuffer[k + 1] < greenValue) { pixelBuffer[k + 1] = (byte)(byte255 - pixelBuffer[k + 1]); }
if (pixelBuffer[k + 2] < redValue) { pixelBuffer[k + 2] = (byte)(byte255 - pixelBuffer[k + 2]); } }
Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData resultData = resultBitmap.LockBits(new Rectangle (0, 0, resultBitmap.Width, resultBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length); resultBitmap.UnlockBits(resultData);
return resultBitmap; }

In order to manipulate pixel colour component values directly we first need to lock the source into memory by invoking the Bitmap.LockBits method. Once the source is locked into memory we can copy the underlying pixel buffer using the Marshal.Copy method.

The next step involves iterating through the byte buffer of colour components. Notice how each iteration modifies an entire pixel by iterating by 4. As discussed earlier when implementing image solarisation the associated formula employs threshold comparison, which determines whether or not colour inversion should be applied.

The final operation performed by the Solarise method involves copying the modified pixel buffer into a newly created object which will be returned to the calling code.

Sample Images

The original source image used to create all of the solarised sample images in this has been licensed under the Creative Commons Attribution-Share Alike 3.0 Unported, 2.5 Generic, 2.0 Generic and 1.0 Generic license. The original image is attributed to Kenneth Dwain Harrelson and can be downloaded from Wikipedia.

The Original Image

 

Monarch_In_May

Solarised Images

 

Butterfly1 Butterfly2
Butterfly3 Butterfly4
Butterfly5 Butterfly7
Butterfly8 Butterfly10

Related Articles

41 Responses to “C# How to: Image Solarise”



  1. 1 C# How to: Bitmap Colour Tint | Software by Default Trackback on April 26, 2013 at 1:07 AM
  2. 2 C# How to: Bitwise Bitmap Blending | Software by Default Trackback on April 26, 2013 at 1:13 AM
  3. 3 C# How to: Image Contrast | Software by Default Trackback on April 26, 2013 at 1:16 AM
  4. 4 C# How to: Bitmap Colour Shading | Software by Default Trackback on April 26, 2013 at 1:19 AM
  5. 5 C# How to: Bi-tonal Bitmaps | Software by Default Trackback on April 26, 2013 at 1:21 AM
  6. 6 C# How to: Bitmap Colour Balance | Software by Default Trackback on April 26, 2013 at 1:23 AM
  7. 7 C# How to: Image Arithmetic | Software by Default Trackback on April 27, 2013 at 8:21 AM
  8. 8 C# How to: Image Convolution | Software by Default Trackback on May 1, 2013 at 5:42 PM
  9. 9 C# How to: Image filtering implemented using a ColorMatrix | Software by Default Trackback on May 1, 2013 at 9:04 PM
  10. 10 C# How to: Blending Bitmap images using colour filters | Software by Default Trackback on May 1, 2013 at 10:50 PM
  11. 11 C# How to: Decoding/Converting Base64 strings to Bitmap images | Software by Default Trackback on May 2, 2013 at 9:35 PM
  12. 12 C# How to: Image Edge Detection | Software by Default Trackback on May 11, 2013 at 1:23 PM
  13. 13 C# How to: Difference Of Gaussians | Software by Default Trackback on May 18, 2013 at 12:50 AM
  14. 14 C# How to: Image Median Filter | Software by Default Trackback on May 18, 2013 at 4:16 AM
  15. 15 C# How to: Image Unsharp Mask | Software by Default Trackback on May 18, 2013 at 12:18 PM
  16. 16 C# How to: Image Colour Average | Software by Default Trackback on May 18, 2013 at 9:48 PM
  17. 17 C# How to: Image Erosion and Dilation | Software by Default Trackback on May 19, 2013 at 10:24 AM
  18. 18 C# How to: Morphological Edge Detection | Software by Default Trackback on May 25, 2013 at 8:22 AM
  19. 19 C# How to: Boolean Edge Detection | Software by Default Trackback on June 1, 2013 at 2:09 AM
  20. 20 C# How to: Gradient Based Edge Detection | Software by Default Trackback on June 1, 2013 at 4:46 PM
  21. 21 C# How to: Image Cartoon Effect | Software by Default Trackback on June 2, 2013 at 4:13 PM
  22. 22 C# How to: Sharpen Edge Detection | Software by Default Trackback on June 7, 2013 at 5:11 AM
  23. 23 C# How to: Calculating Gaussian Kernels | Software by Default Trackback on June 8, 2013 at 10:59 AM
  24. 24 C# How to: Image Blur | Software by Default Trackback on June 9, 2013 at 10:19 PM
  25. 25 C# How to: Image Transform Rotate | Software by Default Trackback on June 16, 2013 at 10:40 AM
  26. 26 C# How to: Image Transform Shear | Software by Default Trackback on June 16, 2013 at 5:45 PM
  27. 27 C# How to: Compass Edge Detection | Software by Default Trackback on June 22, 2013 at 9:34 PM
  28. 28 C# How to: Oil Painting and Cartoon Filter | Software by Default Trackback on June 30, 2013 at 10:47 AM
  29. 29 C# How to: Stained Glass Image Filter | Software by Default Trackback on June 30, 2013 at 10:50 AM
  30. 30 C# How to: Generate a Web Service from WSDL | Software by Default Trackback on June 30, 2013 at 4:07 PM
  31. 31 C# How to: Bitmap Colour Substitution implementing thresholds | Software by Default Trackback on July 6, 2013 at 4:33 PM
  32. 32 C# How to: Swapping Bitmap ARGB Colour Channels | Software by Default Trackback on July 6, 2013 at 5:02 PM
  33. 33 C# How to: Image filtering by directly manipulating Pixel ARGB values | Software by Default Trackback on July 8, 2013 at 2:57 AM
  34. 34 C# How to: Image ASCII Art | Software by Default Trackback on July 14, 2013 at 7:22 AM
  35. 35 C# How to: Weighted Difference of Gaussians | Software by Default Trackback on July 14, 2013 at 8:11 PM
  36. 36 C# How to: Image Boundary Extraction | Software by Default Trackback on July 21, 2013 at 10:23 AM
  37. 37 C# How to: Image Abstract Colours Filter | Software by Default Trackback on July 28, 2013 at 7:41 PM
  38. 38 C# How to: Fuzzy Blur Filter | Software by Default Trackback on August 9, 2013 at 6:39 AM
  39. 39 C# How to: Image Distortion Blur | Software by Default Trackback on August 9, 2013 at 10:13 PM
  40. 40 C# How to: Standard Deviation Edge Detection | Software by Default Trackback on August 8, 2015 at 8:10 AM
  41. 41 C# How to: Min/Max Edge Detection | Software by Default Trackback on August 9, 2015 at 11:29 AM

Leave a comment




Dewald Esterhuizen

Blog Stats

  • 869,840 hits

Enter your email address to follow and receive notifications of new posts by email.

Join 228 other subscribers

Archives

RSS SoftwareByDefault on MSDN

  • An error has occurred; the feed is probably down. Try again later.