.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_examples_imaging_filter_images.py:
Image Filtering
===============
Learn how to filter images through a pipeline.
.. code-block:: default
# Make sure we have the image plugin
import numpy as np
import imagepypelines as ip
ip.require('image')
Construct our filtering blocks
.. code-block:: default
# Frequency Filter
@ip.blockify()
def freq_filter(src, kernel):
return src * kernel
# Easy "frequency space" normalized lowpass filter
@ip.blockify()
def circular_pass_filter(shape, radius=0.1, type='low'):
x, y = np.meshgrid(np.linspace(-1, 1, shape[0]), np.linspace(-1, 1, shape[1]))
circle = np.sqrt(x*x + y*y)
if type=='low':
return np.where(circle > radius, 0, 1)
elif type=='high':
return np.where(circle > radius, 1, 0)
else:
raise ValueError("Filter 'type' must be either 'low' or 'high'")
Define our tasks and build the pipeline
.. code-block:: default
tasks = {
# set an entry point for images into the pipeline
'images': ip.Input(),
# get the image dimensions for later use
'shape': (ip.image.Dimensions(order="WHC"), 'images'),
# take the fourier transform
'fft': (ip.image.ImageFFT(order="WHC"), 'images'),
# produce 'frequency space' circular pass filters
'circles': (circular_pass_filter, 'shape'),
# add an extra dimension to the filters for broadcasting the arrays
'unsqueezed': (ip.image.Unsqueeze(-1), 'circles'),
# the convolution of your image and filter is element-wise
# multiplication in frequency space!!!
'filtered': (freq_filter, 'fft', 'unsqueezed'),
# take the inverse fourier transform of your filtered images
'ifft': (ip.image.ImageIFFT(order="WHC"), 'filtered'),
# Scale the images, cast dtype, and view the filtered images
# in sequence! Note: simple filtering in RGB results in artifacts!
'safe': (ip.image.DisplaySafe(), 'ifft'),
'null' : (ip.image.CompareView(pause_for=500), 'images','safe')
}
im_filt = ip.Pipeline(tasks)
Let's process some data!
------------------------
Let's grab some example data from the ImagePypelines standard set
.. code-block:: default
images = [ip.image.panda(), ip.image.gecko(), ip.image.redhat()]
# Number and view the images!
processed = im_filt.process(images)
.. image:: /examples/imaging/images/sphx_glr_filter_images_001.png
:alt: Image1, Image2
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 2.312 seconds)
.. _sphx_glr_download_examples_imaging_filter_images.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: filter_images.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: filter_images.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_