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(orpip3) tool to install the libraries, so—if it isn't already installed—we need to install pip first. As mentioned here (https://packaging.python.org/key_projects/#venv). We just need to make sure to upgrade pip (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 http://jupyter.readthedocs.io/en/latest/install.html.