O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Median filter

This is a very simple filter that returns the median value from the pixel and its neighbors. Both pillow and skimage provide built-in functions for this filter.

The following code sample applies a median filter over an image using pillow:

>>> from PIL import Image>>> from PIL import ImageFilter>>> img = Image.open("image.png")>>> blur_img = img.filter(ImageFilter.MedianFilter(7))>>> blur_img.show()

Similarly, using skimage, the following code applies a median filter to a given image:

>>> from skimage import io>>> from skimage import filters>>> img = io.imread("image.png")>>> out = filters.median(img, disk(7))>>> io.imshow(out)>>> io.show()

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required