Book Image

Hands-On Image Processing with Python

By : Sandipan Dey
Book Image

Hands-On Image Processing with Python

By: Sandipan Dey

Overview of this book

Image processing plays an important role in our daily lives with various applications such as in social media (face detection), medical imaging (X-ray, CT-scan), security (fingerprint recognition) to robotics & space. This book will touch the core of image processing, from concepts to code using Python. The book will start from the classical image processing techniques and explore the evolution of image processing algorithms up to the recent advances in image processing or computer vision with deep learning. We will learn how to use image processing libraries such as PIL, scikit-mage, and scipy ndimage in Python. This book will enable us to write code snippets in Python 3 and quickly implement complex image processing algorithms such as image enhancement, filtering, segmentation, object detection, and classification. We will be able to use machine learning models using the scikit-learn library and later explore deep CNN, such as VGG-19 with Keras, and we will also use an end-to-end deep learning model called YOLO for object detection. We will also cover a few advanced problems, such as image inpainting, gradient blending, variational denoising, seam carving, quilting, and morphing. By the end of this book, we will have learned to implement various algorithms for efficient image processing.
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Preface
Index

Setting up different image processing libraries in Python


The next few paragraphs describe to install different image processing libraries and set up the environment for writing codes to process images using classical image processing techniques in Python. In the last few chapters of this book, we will need to use a different setup when we use deep-learning-based methods.

 

Installing pip

We are going to use the pip(orpip3tool to install the libraries, so—if it isn't already installed—we need to install pip first. As mentioned here (https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip), pip is already installed if we are using Python 3 >=3.4 downloaded from python.org, or if we are working in a Virtual Environment (https://packaging.python.org/tutorials/installing-packages/#creating-and-using-virtual-environments) created by virtualenv (https://packaging.python.org/key_projects/#virtualenv) or pyvenv (https://packaging.python.org/key_projects/#venv). We just need to make sure to upgrade pip (https://pip.pypa.io/en/stable/installing/#upgrading-pip). How to install pip for different OSes or platforms can be found here: https://stackoverflow.com/questions/6587507/how-to-install-pip-with-python-3.

Installing some image processing libraries in Python

In Python, there are many libraries that we can use for image processing. The ones we are going to use are: NumPy, SciPy, scikit-image, PIL (Pillow), OpenCV, scikit-learn, SimpleITK, and Matplotlib.

The matplotliblibrary will primarily be used for display purposes, whereas numpy will be used for storing an image. The scikit-learn library will be used for building machine-learning models for image processing, and scipy will be used mainly for image enhancements. The scikit-image, mahotas, and opencv libraries will be used for different image processing algorithms.

The following code block shows how the libraries that we are going to use can be downloaded and installed with pip from a Python prompt (interactive mode):

>>> pip install numpy
>>> pip install scipy
>>> pip install scikit-image
>>> pip install scikit-learn
>>> pip install pillow
>>> pip install SimpleITK
>>> pip install opencv-python 
>>> pip install matplotlib

 

 

 

 

There may be some additional installation instructions, depending on the OS platform you are going to use. We suggest the reader goes through the documentation sites for each of the libraries to get detailed platform-specific installation instructions for each library. For example, for the scikit-image library, detailed installation instructions for different OS platforms can be found here: http://scikit-image.org/docs/stable/install.html. Also, the reader should be familiar with websites such as stackoverflow to resolve platform-dependent installation issues for different libraries.

Finally, we can verify whether a library is properly installed or not by importing it from the Python prompt. If the library is imported successfully (no error message is thrown), then we don't have any installation issue. We can print the version of the library installed by printing it to the console.

The following code block shows the versions for the scikit-image and PIL Python libraries: 

>>> import skimage, PIL, numpy
>>> print(skimage.__version__)
# 0.14.0
>>> PIL.__version__
# 5.1.0 
>>> numpy.__version__
# 1.14.5

Let us ensure that we have the latest versions of all of the libraries.

Installing the Anaconda distribution

We also recommend to download and install the latest version of the Anaconda distribution; this will eliminate the need for explicit installation of many Python packages. 

Note

More about installing Anaconda for different OSes can be found at https://conda.io/docs/user-guide/install/index.html.

 

 

Installing Jupyter Notebook

We are going to use Jupyter notebooks to write our Python code. So, we need to install thejupyter package first from a Python prompt with >>> pip install jupyter, and then launch the Jupyter Notebook app in the browser using >>> jupyter notebook. From there, we can create new Python notebooks and choose a kernel. If we use Anaconda, we do not need to install Jupyter explicitly; the latest Anaconda distribution comes with Jupyter.

Note

More about running Jupyter notebooks can be found at http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html.

We can even install a Python package from inside a notebook cell; for example, we can installscipy with the !pip install scipy command.

Note

For more information on installing Jupyter, please refer to http://jupyter.readthedocs.io/en/latest/install.html.