In this article we explore the tasks related to creating a Cartoon Effect from images which reflect real world non-animated scenarios. When applying a Cartoon Effect it becomes possible with relative ease to create images appearing to have originated from a drawing/animation.
Cartoon version of Steve Ballmer: Low Pass 3×3, Threshold 65.
Sample source code
This article is accompanied by a sample source code Visual Studio project which is available for download here.
CPU: Gaussian 7×7, Threshold 84
Using the Sample Application
A Sample Application has been included as part of the sample source code accompanying this article. The Sample Application is a Windows Forms based application which enables a user to specify source/input images, apply various methods of implementing the Cartoon Effect. In addition users are able to save generated images to the local file system.
When using the Sample Application click the Load Image button to load image files from the local file system. On the right-hand side of the Sample application’s user interface, users are provided with two configuration options: Smoothing Filter and Threshold.
Rose: Gaussian 3×3 Threshold 28.
In this article and sample source code image detail and definition can be reduced through means of image smoothing filters. Several image smoothing options are available to the user, the following section serves as a discussion of each option.
Gaussian 3×3 – Gaussian blur filters can be very effective at removing image noise, smoothing an image background, whilst still preserving the edges expressed in the sample/input image. A Convolution matrix/kernel of 3×3 dimensions result in slight image blurring.
Gaussian 5×5 – A Gaussian blur operation being implemented by making use of a convolution matrix/kernel defined with dimensions of 5×5. A slightly larger kernel results in an increased level of image blurring being expressed by output images. A greater level of image blurring equates to a larger degree of image noise reduction/removal.
Rose: Gaussian 7×7 Threshold 48.
Gaussian 7×7 – As can be expected when specifying a convolution matrix/kernel conforming to 7×7 size dimension an even more intense level of image blurring can be detected when looking at result images. Notice how increased levels of image blurring negatively affects the process of edge detection. Consider the following: In a scenario where too many elements are being detected as part of an edge as a result of image noise, specifying a higher level of image blurring should reduce edges being detected. The reasoning can be explained in terms of image blurring reducing image definition/detail, higher levels of image blurring will thus result in a greater level of image detail/definition reduction. Lower definition images are less likely to express the same level of detected edges when compared to higher definition images.
CPU: Median 3×3, Threshold 96.
Median 3×3 – When applying a Median filter to an image the resulting image should express a lesser degree of image noise. In other words, the Median filter can be considered as well suited to performing noise reduction. Also note that a Median filter under certain conditions has the ability to preserve the edges contained in an image. In the following section we explore the importance of edge detection in achieving a Cartoon Effect. Important concepts to take note of: The Median Filter when implemented on an image performs noise reduction whilst preserving image edges. In relation, edge detection represents a core concept/task when creating a Cartoon Effect. The Median Filter’s edge preservation property compliments the process of edge detection. When an image contains a low level of image noise the Median 3×3 Filter could be considered.
Median 5×5 – The 5×5 dimension implementation of the Median Filter result in producing images which exhibit a higher degree of smoothing and a lesser expression of image noise. If the 3×3 Median Filter fails to provide adequate levels of image smoothing and noise reduction the 5×5 Median Filter could be implemented.
Cartoon version of Steve Ballmer: Sharpen 3×3, Threshold 80.
Median 7×7 – The last Median Filter implemented by the sample source code conforms to a 7×7 size dimension. This filter variation results in a high level of image noise reduction. The trade off to more effective noise reduction will be expressed in result images appearing extremely smooth, in some scenarios perhaps overly so.
Mean 3×3 – The Mean Filter provides a different implementation towards achieving image smoothing and noise reduction.
Mean 5×5 – The 5×5 dimension Mean Filter variation serves as a more intense version of the Mean 3×3 Filter. Depending on the level of image noise and type of image noise a Mean Filter could prove a more efficient implementation in comparison to a Median Filter.
Low Pass 3×3 – In much the same fashion as Median and Mean Filters, a Low Pass Filter achieves images smoothing and noise reduction. Notice when comparing Median, Mean and Low Pass Filtering, the differences observed in output results are only expressed as slight differences. The most effective filter to apply should be seen as as being dependent on the input/source image characteristics.
CPU: Gaussian 3×3, Threshold 92.
Low Pass 5×5 – This filter variation being of a larger dimension serves as a more intense implementation of the Low Pass 3×3 Filter.
Sharpen 3×3 – In certain scenarios input/source images may already be smoothed/blurred to such an extent where the edge detection process performs below expectation. Edge detection can be improved when applying a image Sharpen filter.
Threshold values specified by the user through the user interface serves the purpose of enabling the user to finely control the extent/intensity of edges being detected. Implementing a higher Threshold value will have the result of less edges being detected. In order to reduce the level of image noise being detected as false edges the Threshold value should be increased. When too few edges are being detected the Threshold value should be decreased.
The following image is a screenshot of the Image Cartoon Effect Sample Application in action:
Explanation of the Cartoon Effect
The Cartoon Effect consists of reducing image detail/definition whilst at the same instance performing edge detection. The resulting smoothed image and the edges detected in the source/input image should be combined, where detected edges are being expressed in the colour black. The final image reflects an appearance similar to that of an animated/artist drawn image.
- Gaussian 3×3
- Gaussian 5×5
- Gaussian 7×7
- Median 3×3
- Median 5×5
- Median 7×7
- Median 9×9
- Mean 3×3
- Mean 5×5
- Low Pass 3×3
- Low Pass 5×5
- Sharpen 3×3
Rose: Mean 5×5 Threshold 37.
All of the filter methods listed above are implemented by means of Image Convolution. The size dimensions listed for each filter option relates to the dimension of the kernel/matrix being implemented by a filter.
When applying a filter, the intensity/extent will be determined by the size dimensions of the Convolution kernel/matrix implemented. Smaller kernel/matrix dimensions result in a filter being applied to a lesser extent. Larger kernel/matrix dimensions will result in the filter effect being more evident, being applied to a greater extent. Image noise reduction will be achieved when implementing a filter.
The Sample Source code implements Gradient Based Edge Detection using the original source/input image, therefore not being influenced by any smoothing operations. I have published an in-depth article on the topic of Gradient Based Edge Detection which can be located here: C# How to: Gradient Based Edge Detection.
Rose: Median 3×3 Threshold 37.
The Sample source code implements Gradient Based Edge Detection by means of iterating each pixel that forms part of the sample/input image. Whilst iterating image pixels the sample code calculate various gradients from the current pixel’s neighbouring pixels, on a per colour component basis (Red, Green and Blue). Referring to neighbouring pixels, calculations include the value of each of the surrounding pixels in regards to the pixel currently being iterated. Neighbouring pixel calculations are better know as kernel/window/matrix operations.
Note: Do not confuse image convolution and the method in which we iterate and calculate gradients. Although both methods have various aspects in common, image convolution is regarded as linear filter processing, whereas our method qualifies as a non-linear filter.
We calculate various gradients, which is to be compared against the user specified global threshold value. If a calculated gradient value exceeds the value of the user specified threshold the pixel currently being iterated will be considered as part of an edge.
The first gradients to be calculated involves the pixel directly above, below, left and right of the current pixel. A gradient will be calculated for each colour component. The gradient values being calculated can be considered as an indicator reflecting the rate of change. If the sum total of the calculated gradients exceed that of the global threshold the pixel will be considered as forming part an edge.
When the comparison of the threshold value and the total gradient value reflects in favour of the threshold the following set of gradients will be calculated. This process of calculating gradients will continue either until a gradient value exceeds the threshold or all gradients have been calculated.
If a pixel was detected as forming part of an edge, the pixel’s colour will be set to black. In the case of non-edge pixels, the original colour components from the source/input image will be used in setting the current pixel’s value.
Rose: Gaussian 3×3 Threshold 28
Implementing Cartoon Effects
The sample source code implementation can be divided into five distinct components: Cartoon Effect Filter, smoothing helper method, Median Filter implementation, Convolution implementation and the collection of pre-defined matrix/kernel values.
The SmoothingFilterType enumeration, defined by the sample source code, serves as a strongly typed definition of a the collection of implemented smoothing filters. The definition as follows:
Rose: Low Pass 3×3 Threshold 61
The SmoothingFilter extension method targets the Bitmap class. This method implements image convolution. The primary task performed by the SmoothingFilter extension method involves translating convolution filter options into the correct method calls. The definition as follows:
Cartoon version of Steve Ballmer: Sharpen 3×3 Threshold 80
The CartoonEffectFilter extension method targets the Bitmap class. This method defines all the tasks required in order to implement a Cartoon Filter. From an implementation point of view, consuming code is only required to invoke this method, no other additional method calls are required. The definition as follows:
The sample image featuring an Amber flush Rose has been licensed under the Creative Commons Attribution-Share Alike 3.0 Unported, 2.5 Generic, 2.0 Generic and 1.0 Generic license and can be downloaded from Wikipedia.
The sample image featuring a Computer Processor has been licensed under the Creative Commons Attribution-Share Alike 2.0 Generic license and can be downloaded from Wikipedia.l The original author is attributed as Andrew Dunn – http://www.andrewdunnphoto.com/
The Original Image
No Smoothing, Threshold 100
Gaussian 3×3, Threshold 73
Gaussian 5×5, Threshold 78
Gaussian 7×7, Threshold 84
Low Pass 3×3, Threshold 72
Low Pass 5×5, Threshold 81
Mean 3×3, Threshold 79
Mean 5×5, Threshold 80
Median 3×3, Threshold 85
Median 5×5, Threshold 105
Median 7×7, Threshold 127
Median 9×9, Threshold 154
Sharpen 3×3, Threshold 114
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:
- C# How to: Image filtering by directly manipulating Pixel ARGB values
- C# How to: Image filtering implemented using a ColorMatrix
- C# How to: Blending Bitmap images using colour filters
- C# How to: Bitmap Colour Substitution implementing thresholds
- C# How to: Generating Icons from Images
- C# How to: Swapping Bitmap ARGB Colour Channels
- C# How to: Bitmap Pixel manipulation using LINQ Queries
- C# How to: Linq to Bitmaps – Partial Colour Inversion
- C# How to: Bitmap Colour Balance
- C# How to: Bi-tonal Bitmaps
- C# How to: Bitmap Colour Tint
- C# How to: Bitmap Colour Shading
- C# How to: Image Solarise
- C# How to: Image Contrast
- C# How to: Bitwise Bitmap Blending
- C# How to: Image Arithmetic
- C# How to: Image Convolution
- C# How to: Image Edge Detection
- C# How to: Difference Of Gaussians
- C# How to: Image Median Filter
- C# How to: Image Unsharp Mask
- C# How to: Image Colour Average
- C# How to: Image Erosion and Dilation
- C# How to: Morphological Edge Detection
- C# How to: Boolean Edge Detection
- C# How to: Gradient Based Edge Detection
- C# How to: Sharpen Edge Detection
- C# How to: Calculating Gaussian Kernels
- C# How to: Image Blur
- C# How to: Image Transform Rotate
- C# How to: Image Transform Shear
- C# How to: Compass Edge Detection
- C# How to: Oil Painting and Cartoon Filter
- C# How to: Stained Glass Image Filter