Kuwahara filter

From HandWiki
Short description: Type of filter used in image processing

The Kuwahara filter is a non-linear smoothing filter used in image processing for adaptive noise reduction. Most filters that are used for image smoothing are linear low-pass filters that effectively reduce noise but also blur out the edges. However the Kuwahara filter is able to apply smoothing on the image while preserving the edges.

The modified for color images Kuwahara filter effectively reducing noise without blurring the edges.

It is named after Michiyoshi Kuwahara, Ph.D., who worked at Kyoto and Osaka Sangyo Universities in Japan , developing early medical imaging of dynamic heart muscle in the 1970s and 80s.

The Kuwahara operator

Window used by a Kuwahara filter. It is divided into 4 square regions a,b,c,d with the pixels located on the central row and column belonging to more than one region.

Suppose that [math]\displaystyle{ I(x,y) }[/math] is a grey scale image and that we take a square window of size [math]\displaystyle{ 2a+1 }[/math] centered around a point [math]\displaystyle{ (x,y) }[/math] in the image. This square can be divided into four smaller square regions [math]\displaystyle{ Q_{i=1 \cdots 4} }[/math] each of which will be [1]

[math]\displaystyle{ Q_i (x,y) = \begin{cases} \left[ x,x+a \right] \times \left[ y,y+a \right] & \mbox{ if }i = 1\\ \left[ x-a,x \right] \times \left[ y,y+a \right] & \mbox{ if }i = 2\\ \left[ x-a,x \right] \times \left[ y-a,y \right] & \mbox{ if }i = 3\\ \left[ x,x+a \right] \times \left[ y-a,y \right] & \mbox{ if }i = 4\\ \end{cases} }[/math]

where [math]\displaystyle{ \times }[/math] is the cartesian product. Pixels located on the borders between two regions belong to both regions so there is a slight overlap between subregions.

The arithmetic mean [math]\displaystyle{ m_i(x,y) }[/math] and standard deviation [math]\displaystyle{ \sigma _i(x,y) }[/math] of the four regions centered around a pixel (x,y) are calculated and used to determine the value of the central pixel. The output of the Kuwahara filter [math]\displaystyle{ \Phi(x,y) }[/math] for any point [math]\displaystyle{ (x,y) }[/math] is then given by [math]\displaystyle{ \Phi(x,y) = m_i(x, y) }[/math] where [math]\displaystyle{ i = \operatorname{arg\min}_j \sigma_j(x, y) }[/math].

The behaviour of the Kuwahara filter for 2 cases.
The effect of using a Kuwahara filter on an original image(top left) using windows with sizes 5,9 and 15 pixels.

This means that the central pixel will take the mean value of the area that is most homogenous. The location of the pixel in relation to an edge plays a great role in determining which region will have the greater standard deviation. If for example the pixel is located on a dark side of an edge it will most probably take the mean value of the dark region. On the other hand, should the pixel be on the lighter side of an edge it will most probably take a light value. On the event that the pixel is located on the edge it will take the value of the more smooth, least textured region. The fact that the filter takes into account the homogeneity of the regions ensures that it will preserve the edges while using the mean creates the blurring effect.

Similarly to the median filter the Kuwahara filter uses a sliding window approach to access every pixel in the image. The size of the window is chosen in advance and may vary depending on the desired level of blur in the final image. Bigger windows typically result in the creation of more abstract images whereas small windows produce images that retain their detail. Typically windows are chosen to be square with sides that have an odd number of pixels for symmetry. However, there are variations of the Kuwahara filter that use rectangular windows. Additionally, the subregions do not need to overlap or have the same size as long as they cover all of the window.

Color images

For color images, the filter should not be performed by applying the filter to each RGB channel separately, and then recombining the three filtered color channels to form the filtered RGB image. The main problem with that is that the quadrants will have different standard deviations for each of the channels. For example, the upper left quadrant may have the lowest standard deviation in the red channel, but the lower right quadrant may have the lowest standard deviation in the green channel. This situation would result in the color of the central pixel to be determined by different regions, which might result in color artifacts or blurrier edges.

To overcome this problem, for color images a slightly modified Kuwahara filter must be used. The image is first converted into another color space, the HSV color model. The modified filter then operates on only the "brightness" channel, the Value coordinate in the HSV model. The variance of the "brightness" of each quadrant is calculated to determine the quadrant from which the final filtered color should be taken from. The filter will produce an output for each channel which will correspond to the mean of that channel from the quadrant that had the lowest standard deviation in "brightness". This ensures that only one region will determine the RGB values of the central pixel.

ImageMagick uses a similar approach, but using the Rec. 709 Luma as the brightness metric.[2]

Julia Implementation

The output of the julia code
using Statistics
using Images

"Computes an average color from a list of colors"
rgbAverage(colors) = RGB(
	sum(map(c -> c.r, colors)) / length(colors),
	sum(map(c -> c.g, colors)) / length(colors),
	sum(map(c -> c.b, colors)) / length(colors)
);

"""
    kuwahara(I, window_size)

Applies the kuwahara filter to an image `I`, using a window square of size `window_size`
"""
function kuwahara(I, window_size)
	# Converts the image to a hsv colorspace
	# (needed to run on colored images)
	hsv = HSV.(I)
	# Gets the brightness value as values[y, x]
	values = channelview(float.(hsv))[3,:,:]
    # For Rec.601 Luma, replace the above two lines with:
    # values = Gray.(I)

	# Create an empty image of the same dimensions as the input
    resulting_image = similar(I)

	# The size of each quadrant of the window
	quadrant_size = Int(ceil(window_size / 2))

    # (y, x) loop over the original image
    for y in 1:size(I, 1)
        for x in 1:size(I, 2)
			# The top left position of the window
			tl_x = x - (window_size ÷ 2)
			tl_y = y - (window_size ÷ 2)

			# Function that keeps a number between 1 and the image size
			# (makes sure all the positions we're using are valid)
			c1(i) = map(j -> clamp(j, 1, size(values,1)), i)
			c2(i) = map(j -> clamp(j, 1, size(values,2)), i)

			# The positions of each quadrant
			quadrant_a = [c1(tl_y:quadrant_size+tl_y), c2(tl_x:tl_x+quadrant_size)]
			quadrant_b = [c1(tl_y:quadrant_size+tl_y), c2(tl_x+quadrant_size:tl_x+window_size)]
			quadrant_c = [c1(tl_y+quadrant_size:tl_y+window_size), c2(tl_x:tl_x+quadrant_size)]
		    quadrant_d = [c1(tl_y+quadrant_size:tl_y+window_size), c2(tl_x+quadrant_size:tl_x+window_size)]

			# Standard deviation of each quadrant
			σ_a = std(values[quadrant_a...])
			σ_b = std(values[quadrant_b...])
			σ_c = std(values[quadrant_c...])
			σ_d = std(values[quadrant_d...])

			# Select the quadrant with the smallest standard deviation
			quadrants = [quadrant_a, quadrant_b, quadrant_c, quadrant_d]
			min = argmin([σ_a, σ_b, σ_c, σ_d])
			quadrant = quadrants[min]

			# The pixel we're processing receives the average of the quadrant
			pixels = I[quadrant...]
			resulting_image[y, x] = rgbAverage(pixels)
		end
    end

    return resulting_image
end

# Test code with a sample image
url = "https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg"
download(url, "lion.jpg")

img = load("lion.jpg")
window_size = 13

result = kuwahara(img, window_size)
save("result.png", result)

Applications

Kuwahara filter used to create a painting-like effect in an image.

Originally the Kuwahara filter was proposed for use in processing RI-angiocardiographic images of the cardiovascular system.[3] The fact that any edges are preserved when smoothing makes it especially useful for feature extraction and segmentation and explains why it is used in medical imaging.

The Kuwahara filter however also finds many applications in artistic imaging and fine-art photography due to its ability to remove textures and sharpen the edges of photographs. The level of abstraction helps create a desirable painting-like effect in artistic photographs especially in the case of the colored image version of the filter. These applications have known great success and have encouraged similar research in the field of image processing for the arts.

Although the vast majority of applications have been in the field of image processing there have been cases that use modifications of the Kuwahara filter for machine learning tasks such as clustering.[4]

The Kuwahara filter has been implemented in CVIPtools.[5]

Drawbacks and restrictions

The Kuwahara filter despite its capabilities in edge preservation has certain drawbacks.

  1. At a first glance it is noticeable that the Kuwahara filter does not take into account the case where two regions have equal standard deviations. This is not often the case in real images since it is rather hard to find two regions with exactly the same standard deviation due to the noise that is always present. In cases where two regions have similar standard deviations the value of the center pixel could be decided at random by the noise in these regions. Again this would not be a problem if the regions had the same mean. However, it is not unusual for regions of very different means to have the same standard deviation. This makes the Kuwahara filter susceptible to noise. Different ways have been proposed for dealing with this issue, one of which is to set the value of the center pixel to [math]\displaystyle{ (m_1+m_2)/2 }[/math] in cases where the standard deviation of two regions do not differ more than a certain value [math]\displaystyle{ D }[/math].
  2. The block artifacts created when using the Kuwahara filter are visible in the right image.
    The Kuwahara filter is also known to create block artifacts in the images especially in regions of the image that are highly textured. These blocks disrupt the smoothness of the image and are considered to have a negative effect in the aesthetics of the image. This phenomenon occurs due to the division of the window into square regions. A way to overcome this effect is to take windows that are not rectangular(i.e. circular windows) and separate them into more non-rectangular regions. There have also been approaches where the filter adapts its window depending on the input image.[6]

Extensions of the Kuwahara filter

The success of the Kuwahara filter has spurred an increase the development of edge-enhancing smoothing filters. Several variations have been proposed for similar use most of which attempt to deal with the drawbacks of the original Kuwahara filter.

The "Generalized Kuwahara filter" proposed by P. Bakker[7] considers several windows that contain a fixed pixel. Each window is then assigned an estimate and a confidence value. The value of the fixed pixel then takes the value of the estimate of the window with the highest confidence. This filter is not characterized by the same ambiguity in the presence of noise and manages to eliminate the block artifacts.

The "Mean of Least Variance"(MLV) filter, proposed by M.A. Schulze [8] also produces edge-enhancing smoothing results in images. Similarly to the Kuwahara filter it assumes a window of size [math]\displaystyle{ 2d-1 \times 2d-1 }[/math] but instead of searching amongst four subregions of size [math]\displaystyle{ d \times d }[/math] for the one with minimum variance it searches amongst all possible [math]\displaystyle{ d \times d }[/math] subregions. This means the central pixel of the window will be assigned the mean of the one subregion out of a possible [math]\displaystyle{ d ^2 }[/math] that has the smallest variance.

The "Adaptative Kuwahara filter", proposed by K. Bartyzel,[9] is a combination of the anisotropic Kuwahara filter and the adaptative median filter. In comparison with the standard Kuwahara filter, both the objects and the edges retain a better quality. As opposed to the standard Kuwahara filter, the window size is changing, depending on the local properties of the image. For each of the four basic areas surrounding a pixel, the mean and variance are calculated. Then, the window size of each of the four basic areas is increased by 1. If the variance of a new window is smaller than before the resizing of the filter window, then the mean and variance of the basic area will take the newly calculated values. The window size continues to be increased until the new variance is greater than the previous one, or the maximum allowable window size is reached. The variance of the four areas are then compared, and the value of the output pixel is the average value of the basic area for which the variance was the smallest.

A more recent attempt in edge-enhancing smoothing was also proposed by J. E. Kyprianidis.[6] The filter's output is a weighed sum of the local averages with more weight given the averages of more homogenous regions.

See also

  • Edge-preserving filtering

References

  1. Giuseppe Papari, Nicolai Petkov, and Patrizio Campisi, Artistic Edge and Corner Enhancing Smoothing, IEEE TRANSACTIONS ON IMAGE PROCESSING, VOL. 16, NO. 10, OCTOBER 2007, pages 2449–2461
  2. ImageMagick Source Code: commit 65ed6392f7db81740a6856f7d166c045e89c4764, in MagickCore/effect.c, function "KuwaharaImage()" https://github.com/ImageMagick/ImageMagick/blob/65ed6392f7db81740a6856f7d166c045e89c4764/MagickCore/effect.c#L1756-L1987
  3. M. Kuwahara. K. Hachimura, S. Eiho, and M. Kinoshita,"Processing of RI-angiocardiographic images," in Digital Processing of Biomedical Images, K. Preston Jr. and M. Onoe, Editors. New York: Plenum, 1976. pp. 187–202.
  4. H. Al-Marzouqi, Data Clustering Using a Modified Kuwahara Filter, Neural Networks, 2009. IJCNN 2009. International Joint Conference on, pp. 128–132, 14–19 June 2009.
  5. CVIPtools Developer Site: http://cviptools.ece.siue.edu
  6. 6.0 6.1 J. E. Kyprianidis, H. Kang, J. and Döllner ,2009, Image and Video Abstraction by Anisotropic Kuwahara Filtering. Computer Graphics Forum, 28: 1955–1963.
  7. P. Bakker, L. Van Vliet, P. Verbeek, Edge preserving orientation adaptive filtering, Computer Vision and Pattern Recognition, 1999. IEEE Computer Society Conference on.
  8. M.A. Schulze, J.A. Pearce, "A morphology-based filter structure for edge-enhancing smoothing ," Image Processing, 1994. Proceedings. ICIP-94., IEEE International Conference, vol.2, no., pp. 530–534 vol.2, 13–16 Nov 1994
  9. K. Bartyzel, "Adaptative Kuwahara filter ," 2015, SIViP 10, pp. 663-670.

Bibliography

  • Umbaugh, Scott E. (November 2011). Digital Image Processing and Analysis: Human and Computer Vision Applications with CVIPtools, Second Edition. CRC Press. ISBN 978-1-4398-0205-2. 

External links