C# How to: Image Abstract Colours Filter

Article Purpose

This article explores Abstract Colour Image filters as a process of Non-photo Realistic Image Rendering. The output produced reflects a variety of artistic effects.

Colour Values Red, Blue Filter Size 9
Edge Tracing Black Edge Threshold 55

Mushroom: Red - Blue, Filter Size 9, Edge Tracing Black, Edge Threshold 55

Sample Source Code

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

Colour Values Blue Filter Size 9
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Blue, Filter Size 9, Color Shift Right, Edge Tracing Double Intensity, Edge Threshold 60

Using the Sample Application

The sample source code that accompanies this article includes a based sample application. The concepts discussed in this article have been illustrated through the implementation of the sample application.

When executing the sample application, through the user interface several options are made available to the user, described as follows:

  • Load/Save Source/input may be loaded from the local system through clicking the Load Image button. If desired by the user, resulting output can be saved to the local file system through clicking the Save Image button.
  • Colour Channels – The colour values Red, Green and Blue can be updated or remain unchanged when applying a filter, indicated through the associated .
  • Filter Size – The level of filter intensity depends on the size of the filter. Larger filter sizes result in more intense filtering being applied. Smaller filter size result in less intense filtering being applied.
  • Colour Shift Type – Colour intensity values can be swapped around through selecting the Colour Shift type: Shift Left and Shift Right.
  • Edge Tracing Type – When applying a filter the edges forming part of the original source/input will be expressed as part of the output . The method in which are indicated/highlighted will be determined through the type of Edge Tracing specified when applying the filter. Supported types of Edge Tracing include: Black, White, Half Intensity, Double Intensity and Inverted.
  • Edge Threshold – Edges forming part of the source/input are determines through means of implementing a threshold value. Lower threshold values result in more emphasized edges expressed within resulting . Higher threshold values reduce edge emphasis in resulting .

 

Colour Values Green Filter Size 9
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Green, Filter Size 9, Color Shift Left, Edge Tracing Double Intensity, Edge Threshold 60

Colour Values Red, Blue Filter Size 9
Edge Tracing Double Intensity Edge Threshold 55

Mushroom: Red - Blue, Filter Size 9, Color Shift Right, Edge Tracing Double Intensity, Edge Threshold 55

The following image is a screenshot of the Image Abstract Colour Filter sample application in action:

Image Abstract Colour Filter Sample Application

Abstracting Image Colours

The Abstract Colour Filter explored in this article can be considered a non-photo realistic filter. As the title implies, non-photo realistic filters transforms an input , usually a photograph, producing a result which visibly lacks the aspects of realism expressed in the input . In most scenarios the objective of non-photo realistic filters can be described as using photographic in rendering having an animated appearance.

Colour Values Blue Filter Size 11
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Blue, Filter Size 11, Color Shift Left, Edge Tracing Double Intensity, Edge Threshold 60

Colour Values Red Filter Size 11
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Red, Filter Size 11, Color Shift Left, Edge Tracing Double Intensity, Edge Threshold 60

The Abstract Colour Filter can be broken down into two main components: Colour Averaging and . Through implementing a variety of colour averaging algorithms resulting express abstract yet uniform colours. Abstract colours result in output no longer appearing photo realistic, instead output appear unconventionally augmented/artistic.

Output express a lesser degree of definition and detail, when compared to input . In some scenarios output might not be easily recognisable. In order to retain some detail, edge/boundary detail detected from input will be emphasised in result .

Colour Values Green Filter Size 11
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Green, Filter Size 11, Color Shift Left, Edge Tracing, Double Intensity, Edge Threshold 60

Colour Values Green, Blue Filter Size 11
Edge Tracing Double Intensity Edge Threshold 75

Mushroom: Green - Blue, Filter Size 11, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 75

The steps required when applying an Abstract Colour Filter can be described as follows:

  1. Perform – Using the source/input perform , producing a binary expressing in the foreground/as white.
  2. Calculate Neighbourhood Colour Averages – Iterate each forming part of the input , inspecting the ’s neighbouring . Calculate the sum total and average of each colour channel, Red, Green and Blue. The value of the currently being iterated should be set depending to neighbourhood average.
  3. Trace Edges within Colour Averages – Simultaneously iterate the colour average and the edge detected . If the being iterated forms part of an edge, update the corresponding in the average colour , depending on the specified method of Edge Tracing.

 

Colour Values Red, Green Filter Size 11
Edge Tracing Double Intensity Edge Threshold 75

Mushroom: Red - Green, Filter Size 11, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 75

Colour Values Red Filter Size 11
Edge Tracing Black Edge Threshold 60

Mushroom: Red, Filter Size 11, Color Shift Left, Edge Tracing Black, Edge Threshold 60

Implementing Pixel Neighbourhood Colour Averaging

In the sample source code neighbourhood colour averaging has been implemented through the definition of the AverageColoursFilter . This method creates a new using the source as input. The following code snippet provides the definition:

public static Bitmap AverageColoursFilter(this Bitmap sourceBitmap,  
                                          int matrixSize,   
                                          bool applyBlue = true, 
                                          bool applyGreen = true, 
                                          bool applyRed = true, 
                                          ColorShiftType shiftType = 
                                          ColorShiftType.None)  
{
    byte[] pixelBuffer = sourceBitmap.GetByteArray(); 
    byte[] resultBuffer = new byte[pixelBuffer.Length]; 

int calcOffset = 0; int byteOffset = 0; int blue = 0; int green = 0; int red = 0; int filterOffset = (matrixSize - 1) / 2;
for (int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for (int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { byteOffset = offsetY * sourceBitmap.Width*4 + offsetX * 4;
blue = 0; green = 0; red = 0;
for (int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) { calcOffset = byteOffset + (filterX * 4) + (filterY * sourceBitmap.Width * 4);
blue += pixelBuffer[calcOffset]; green += pixelBuffer[calcOffset + 1]; red += pixelBuffer[calcOffset + 2]; } }
blue = blue / matrixSize; green = green / matrixSize; red = red / matrixSize;
if (applyBlue == false ) { blue = pixelBuffer[byteOffset]; }
if (applyGreen == false ) { green = pixelBuffer[byteOffset + 1]; }
if (applyRed == false ) { red = pixelBuffer[byteOffset + 2]; }
if (shiftType == ColorShiftType.None) { resultBuffer[byteOffset] = (byte)blue; resultBuffer[byteOffset + 1] = (byte)green; resultBuffer[byteOffset + 2] = (byte)red; resultBuffer[byteOffset + 3] = 255; } else if (shiftType == ColorShiftType.ShiftLeft) { resultBuffer[byteOffset] = (byte)green; resultBuffer[byteOffset + 1] = (byte)red; resultBuffer[byteOffset + 2] = (byte)blue; resultBuffer[byteOffset + 3] = 255; } else if (shiftType == ColorShiftType.ShiftRight) { resultBuffer[byteOffset] = (byte)red; resultBuffer[byteOffset + 1] = (byte)blue; resultBuffer[byteOffset + 2] = (byte)green; resultBuffer[byteOffset + 3] = 255; } } }
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(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
resultBitmap.UnlockBits(resultData);
return resultBitmap; }
Colour Values Green, Blue Filter Size 17
Edge Tracing Black Edge Threshold 85

Mushroom: Green - Blue, Filter Size 17, Color Shift Left, Edge Tracing Black, Edge Threshold 85

Implementing Gradient Based Edge Detection

When applying an Abstract Colours Filter, one of the required steps involve . The sample source code implements   through the definition of the GradientBasedEdgeDetectionFilter method. This method has been defined as an targeting the class. The as follows:

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

byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height]; byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length); sourceBitmap.UnlockBits(sourceData);
int sourceOffset = 0, gradientValue = 0; bool exceedsThreshold = false;
for (int offsetY = 1; offsetY < sourceBitmap.Height - 1; offsetY++) { for (int offsetX = 1; offsetX < sourceBitmap.Width - 1; offsetX++) { sourceOffset = offsetY * sourceData.Stride + offsetX * 4; gradientValue = 0; exceedsThreshold = true;
// Horizontal Gradient CheckThreshold(pixelBuffer, sourceOffset - 4, sourceOffset + 4, ref gradientValue, threshold, 2); // Vertical Gradient exceedsThreshold = CheckThreshold(pixelBuffer, sourceOffset - sourceData.Stride, sourceOffset + sourceData.Stride, ref gradientValue, threshold, 2);
if (exceedsThreshold == false) { gradientValue = 0;
// Horizontal Gradient exceedsThreshold = CheckThreshold(pixelBuffer, sourceOffset - 4, sourceOffset + 4, ref gradientValue, threshold);
if (exceedsThreshold == false) { gradientValue = 0;
// Vertical Gradient exceedsThreshold = CheckThreshold(pixelBuffer, sourceOffset - sourceData.Stride, sourceOffset + sourceData.Stride, ref gradientValue, threshold);
if (exceedsThreshold == false) { gradientValue = 0;
// Diagonal Gradient : NW-SE CheckThreshold(pixelBuffer, sourceOffset - 4 - sourceData.Stride, sourceOffset + 4 + sourceData.Stride, ref gradientValue, threshold, 2); // Diagonal Gradient : NE-SW exceedsThreshold = CheckThreshold(pixelBuffer, sourceOffset - sourceData.Stride + 4, sourceOffset - 4 + sourceData.Stride, ref gradientValue, threshold, 2);
if (exceedsThreshold == false) { gradientValue = 0;
// Diagonal Gradient : NW-SE exceedsThreshold = CheckThreshold(pixelBuffer, sourceOffset - 4 - sourceData.Stride, sourceOffset + 4 + sourceData.Stride, ref gradientValue, threshold);
if (exceedsThreshold == false) { gradientValue = 0;
// Diagonal Gradient : NE-SW exceedsThreshold = CheckThreshold(pixelBuffer, sourceOffset - sourceData.Stride + 4, sourceOffset + sourceData.Stride - 4, ref gradientValue, threshold); } } } } }
resultBuffer[sourceOffset] = (byte)(exceedsThreshold ? 255 : 0); resultBuffer[sourceOffset + 1] = resultBuffer[sourceOffset]; resultBuffer[sourceOffset + 2] = resultBuffer[sourceOffset]; resultBuffer[sourceOffset + 3] = 255; } }
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(resultBuffer, 0, resultData.Scan0, resultBuffer.Length); resultBitmap.UnlockBits(resultData);
return resultBitmap; }
Colour Values Red, Green Filter Size 5
Edge Tracing Black Edge Threshold 85

Mushroom: Red - Green - Blue, Filter Size 5, Color Shift Right, Edge Tracing Black, Edge Threshold 85

Implementing an Abstract Colour Filter

The AbstractColorsFilter method serves as means of combining an average colour and an edge detected . This targets the class. The following code snippet details the definition:

public static Bitmap AbstractColorsFilter(this Bitmap sourceBitmap, 
                                          int matrixSize, 
                                          byte edgeThreshold, 
                                          bool applyBlue = true, 
                                          bool applyGreen = true, 
                                          bool applyRed = true, 
                                          EdgeTracingType edgeType =  
                                          EdgeTracingType.Black, 
                                          ColorShiftType shiftType = 
                                          ColorShiftType.None) 
{ 
    Bitmap edgeBitmap =  
    sourceBitmap.GradientBasedEdgeDetectionFilter(edgeThreshold); 

Bitmap colorBitmap = sourceBitmap.AverageColoursFilter(matrixSize, applyBlue, applyGreen, applyRed, shiftType);
byte[] edgeBuffer = edgeBitmap.GetByteArray(); byte[] colorBuffer = colorBitmap.GetByteArray(); byte[] resultBuffer = colorBitmap.GetByteArray();
for (int k = 0; k + 4 < edgeBuffer.Length; k += 4) { if (edgeBuffer[k] == 255) { switch (edgeType) { case EdgeTracingType.Black: resultBuffer[k] = 0; resultBuffer[k+1] = 0; resultBuffer[k+2] = 0; break; case EdgeTracingType.White: resultBuffer[k] = 255; resultBuffer[k+1] = 255; resultBuffer[k+2] = 255; break; case EdgeTracingType.HalfIntensity: resultBuffer[k] = ClipByte(resultBuffer[k] * 0.5); resultBuffer[k + 1] = ClipByte(resultBuffer[k + 1] * 0.5); resultBuffer[k + 2] = ClipByte(resultBuffer[k + 2] * 0.5); break; case EdgeTracingType.DoubleIntensity: resultBuffer[k] = ClipByte(resultBuffer[k] * 2); resultBuffer[k + 1] = ClipByte(resultBuffer[k + 1] * 2); resultBuffer[k + 2] = ClipByte(resultBuffer[k + 2] * 2); break; case EdgeTracingType.ColorInversion: resultBuffer[k] = ClipByte(255 - resultBuffer[k]); resultBuffer[k+1] = ClipByte(255 - resultBuffer[k+1]); resultBuffer[k+2] = ClipByte(255 - resultBuffer[k+2]); break; } } }
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(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
resultBitmap.UnlockBits(resultData);
return resultBitmap; }
Colour Values Red, Green Filter Size 17
Edge Tracing Double Intensity Edge Threshold 85

Mushroom: Red - Green, Filter Size 17, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 85

Sample Images

This article features a number of sample images. All featured images have been licensed allowing for reproduction. The following images feature as sample images:

1280px-Mycena_atkinsoniana_60804

1024px-Lactarius_indigo_48568

Amanita_muscaria_(fly_agaric)

683px-Pleurotus_pulmonarius_LC0228

Additional Filter Result Images

The following series of images represent additional filter results.

Colour Values Red Filter Size 9
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Red, Filter Size 9, Color Shift Right, Edge Tracing Double, Edge Threshold 60

Colour Values Green, Blue Filter Size 9
Edge Tracing Double Intensity Edge Threshold 60

Mushroom: Green - Blue, Filter Size 9, Color Shift Right, Edge Tracing Double Intensity, Edge Threshold 60

Colour Values Green, Blue Filter Size 9
Edge Tracing Black Edge Threshold 60

Mushroom: Green - Blue, Filter Size 9, Color Shift Left, Edge Tracing Black, Edge Threshold 60

Colour Values Red, Green, Blue Filter Size 9
Edge Tracing Double Intensity Edge Threshold 55

Mushroom: Red - Green - Blue, Filter Size 9, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 55

Colour Values Red, Green, Blue Filter Size 11
Edge Tracing Black Edge Threshold 75

Mushroom: Red - Green - Blue, Filter Size 11, Color Shift None, Edge Tracing Black, Edge Threshold 75

Colour Values Red, Blue Filter Size 11
Edge Tracing Double Intensity Edge Threshold 75

Mushroom: Red - Blue, Filter Size 11, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 75

Colour Values Green Filter Size 17
Edge Tracing Black Edge Threshold 85

Mushroom: Green, Filter Size 17, Color Shift None, Edge Tracing Black, Edge Threshold 85

Colour Values Red Filter Size 17
Edge Tracing Black Edge Threshold 85

Mushroom: Red, Filter Size 17, Color Shift None, Edge Tracing Black, Edge Threshold 85

Colour Values Red, Green, Blue Filter Size 5
Edge Tracing Half Edge Edge Threshold 85

Mushroom: Red - Green - Blue, Filter Size 5, Color Shift None, Edge Tracing Half Edge, Threshold 85

Colour Values Blue Filter Size 5
Edge Tracing Double Intensity Edge Threshold 75

Mushroom: Blue, FilterSize 5, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 75

Colour Values Green Filter Size 5
Edge Tracing Double Intensity Edge Threshold 75

Mushroom: Green, Filter Size 5, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 75

Colour Values Red Filter Size 5
Edge Tracing Double Intensity Edge Threshold 75

Mushroom: Red, Filter Size 5, Color Shift None, Edge Tracing Double Intensity, Edge Threshold 75

Colour Values Red, Green, Blue Filter Size 5
Edge Tracing Black Edge Threshold 75

Mushroom: Red - Green - Blue, Filter Size 3, Color Shift Left, Edge Tracing Black, Edge Threshold 75

Colour Values Red, Green, Blue Filter Size 9
Edge Tracing Black Edge Threshold 55

Mushroom: Red - Green - Blue, Filter Size 3, Color Shift None, Edge Tracing Black, Edge Threshold 75

Colour Values Red, Green, Blue Filter Size 3
Edge Tracing Black Edge Threshold 75

Mushroom: Red - Green - Blue, Filter Size 3, Color Shift Right, Edge Tracing Black, Edge Threshold 75

Related Articles and Feedback

Feedback and questions are always encouraged. If you know of an alternative implementation or have ideas on a more efficient implementation please share in the comments section.

I’ve published a number of articles related to imaging and images of which you can find URL links here:

Leave a comment




Dewald Esterhuizen

Blog Stats

  • 869,735 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.