Most GUI-based image processing applications come with a bundle of image filters. There's a wide variety of filters available, and different applications group them into different categories. Some of the common filtering categories are blur, enhancement, edge detection, and more.
From an image processing standpoint, image filters are known operations that help us achieve a specific effect. For example, I once used the counting objects algorithm presented as an example in this book to try to count the number of bubbles in a printed circuit board soaked in water. As you probably realize, pictures obtained from a real-life image are not as sterile as those presented in the sky at night example. And so prior to using the algorithm, I had to clean up the images. By "clean up" I mean filtering the image using known filters. I ended up using a threshold combined with a median filter, and then converting the image to a 1-bit (black-and-white) version prior to running the algorithm.
The following text assumes you have some background in image filtering. If not, my suggestion is that you experiment with a GUI application such as GIMP to get a feel for what filters to use and how they can help you with basic image processing. Once you have the preprocessing figured out, that is, you know what filters you want to run on your image prior to the final algorithm, you can implement the filters with a Python script that makes use of PIL filters. (You might not even require a final algorithm if you select the proper filters.)
PIL provides us with the class ImageFilter, which supports a good number of filters. To use ImageFilter, import it as follows: from PIL import ImageFilter (or simply import ImageFilter). Once you've imported ImageFilter, call the filter() method that's part of the Image object (not ImageFilter object) to filter an image:
>>> from PIL import Image, ImageChops, ImageFilter
>>> img = Image.open('../images/nightsky.png')
>>> fil_img = inv_img.filter(ImageFilter.MinFilter(l5))
In the preceding example, I've used the night sky images you've seen before and inverted the output so as to work on black stars over white background. I then filtered the image using a MinFilter filter (see Figure 9-10). The MinFilter works on a pixel-by-pixel level. For every pixel, it returns the minimum pixel from the square of size n (in the example, 15) centered on the given pixel. As you can see, even from this small example, there's quite a bit to be gained by working with image filters.
ImageFilter provides fixed image enhancement filters easily distinguishable due to their capitalized names:
>>> [filt for filt in dir(ImageFilter) if filt.isupper()]
['BLUR', 'CONTOUR', 'DETAIL', 'EDGE_ENHANCE', 'EDGE_ENHANCE_MORE', 'EMBOSS',
By the term "fixed" I mean that they accept no parameters. To use these filters, call the filter() method with the fixed filter, as follows:
>>> new_img = img.filter(ImageFilter.CONTOUR)
The names of these filters should provide direction as to what they perform. ImageFilter also provides nonfixed filters (i.e., filters that accept parameters). Table 9-4 lists some additional filters supported by the ImageFilter object.
Table 9-4. Some Image Filters
MaxFilter(size=3) MedianFilter(size=3) MinFilter(size=3) ModeFilter(size=3)
For every pixel in the original image, returns the pixel with the maximum value from a square of width size placed around the original pixel. size must be odd (3, 5, 7, . . . ).
For every pixel in the original image, returns the median pixel from a square of width size placed around the original pixel. size must be odd (3, 5, 7, . . . ).
For every pixel in the original image, returns the pixel with the minimum value from a square of width size placed around the original pixel. size must be odd (3, 5, 7, . . . ).
For every pixel in the original image, returns the most common pixel from a square of width size placed around the original pixel. size must be odd (3, 5, 7, . . . ).
Was this article helpful?