In this article, we’ll go over what image processing is, how digital image processing is superior, Image Processing vs Computer Vision, using python windows GUI builder for image processing, using python libraries to perform image processing tasks, and much more.
Table of Contents
What does image processing mean?
According to Gonzalez and Rafael in 2018, Image Processing or Digital Image Processing refers to processing digital images using a digital computer. Note that a digital image is composed of a finite number of elements; each element has a particular location and value. These elements are called picture elements, image elements, and pixels. Pixel is the term used most widely to denote the elements of a digital image. Since images are defined over two dimensions (perhaps more), digital image processing may be modeled in the form of multidimensional systems.
How is digital image processing better?
As a subcategory or field of digital signal processing, digital image processing has many advantages over analog image processing. It allows a much wider range of algorithms to be applied to the input data and can avoid problems such as the build-up of noise and distortion during processing.
The generation and development of digital image processing are mainly affected by three factors: First, the development of computers; second, the development of mathematics (especially the creation and improvement of discrete mathematics theory); third, the increasing demand for a wide range of applications in environment, agriculture, military, industry, medical science, etc.
Image Processing is frequently used in ‘Computer Vision’. Don’t be confused-we are going to talk about the difference between these terms and how they connect.
Image Processing vs Computer Vision
The main difference between Image Processing and Computer Vision approaches is the goals (not the methods used). For example, if the goal is to enhance an image for later use, then this may be called image processing. If the goal is to emulate human vision, like object recognition, event detection, or automatic driving, then it may be called computer vision.
Both Image Processing algorithms and Computer Vision (CV) algorithms take an image as input; however, in image processing, the output is also an image, whereas in computer vision the output can be some features/information about the image.
Read our previous article about Computer Vision here:
Why use Python for Image Processing?
Now, almost every image processing or computer vision library has a form of scripting interface in its main functions. Most of the time, this scripting language is Python (this article shows the power of Delphi to provide a nice GUI for Python scripting power).
Developers favor this particular ecosystem because of the following:
- Easy to learn and easy to use, without losing the quality of programming decisions.
- Python has been on the market for a long time and has been established.
- Python continues to evolve dynamically.
- With Python, you can use the coolest OOP solutions and you can write simple and effective code.
- Python provides a huge number of libraries that solve the whole range of programming problems.
How to use Pillow, Matplotlib, scikit-learn, scikit-image, and Mahotas Python libraries to perform Image Processing tasks
All of them would be integrated with Python4Delphi to create Windows Apps with Image Processing capabilities.
Prerequisites
Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.
Time to get Started!
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
How do I perform Image Processing with Pillow Library on Windows?
Pillow or PIL is the Python Imaging Library that adds Image Processing capabilities to your Python interpreter. This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.
The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.
Another reason you would consider using Pillow is the fact that it is quite easy to use and very popular with Python users. Pillow is a common tool in the arsenal of most Data Scientists who work with images. Pillow provides various image processing methods that are very useful, especially in augmenting training data for computer vision problems.
This section will guide you to combine Python4Delphi with Pillow library, inside Delphi and C++Builder, from installing Pillow with pip to perform simple geometrical transformations to the image.
After installing Python4Delphi properly, you can get Pillow or PIL using pip or easy install to your command prompt:
[crayon-67679cf8d2c99217662924/]Don’t forget to put the path where your Pillow installed, to the System Environment Variables:
System Environment Variable Examples
[crayon-67679cf8d2ca4243850904/]The following is a code example of Pillow to perform geometrical transformations with the image (run this inside the lower Memo of Python4Delphi Demo01 GUI):
[crayon-67679cf8d2ca6177816654/]Here is the result in Python GUI
Read more:
How do I perform Image Processing with Matplotlib Library on Windows?
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
Matplotlib is one of the most popular and oldest Data Visualization libraries in Python which is commonly used in Exploratory Data Analysis and Machine Learning. In Data Science and Machine Learning, Matplotlib helps us to gain insights from a huge amount of data through different Data Visualization techniques.
In this section, you will see that Matplotlib is not only powerful for Data Visualization, but also a powerful tool for Image Processing!
First, here is how you can get Matplotlib to work with Python4Delphi to create GUI with Data Visualization and Image Processing capabilities:
[crayon-67679cf8d2ca8901109119/]The following is a code example of Matplotlib “Hello World!” for Image Processing (load image as NumPy array and show image). Run this inside the lower Memo of Python4Delphi Demo01 GUI:
[crayon-67679cf8d2ca9597068110/]Here is the Matplotlib for Image Processing result in the Python GUI:
How do I perform Image Processing with scikit-learn Library on Windows?
scikit-learn is an open-source Python library for Machine Learning. Scikit-Learn has simple and efficient tools for predictive data analysis that are built on top of SciPy, NumPy, and Matplotlib.
As mentioned before, Image Processing allows a wide range of algorithms to be applied to the input data, and some of the algorithms belong to the Machine Learning domain. So, if you are looking for one of the ideal Image Processing libraries in Python, scikit-learn might be your choice.
This section will guide you to combine Python4Delphi with scikit-learn, inside Delphi and C++Builder, from installing scikit-learn with pip, to using it to quantize colors of a given image using Machine Learning algorithm, such as K-Means clustering!
The following code for Color Quantization using K-Means is credited to Robert Layton, Olivier Grisel, and Mathieu Blondel (visit the original source here). Run the following code inside the lower Memo of Python4Delphi Demo01 GUI:
[crayon-67679cf8d2cab651111393/]scikit-learn Python4Delphi Results
Using the above example, we reduce the number of colors required to show the image from 96,615 unique colors to 64, while preserving the overall appearance quality. Pretty cool, isn’t it?
How do I perform Image Processing with scikit-image Library on Windows?
scikit-image is an image processing library that implements algorithms and utilities for use in research, education, and industry applications. It is released under the liberal Modified BSD open source license, provides a well-documented API in the Python programming language, and is developed by an active, international team of collaborators.
scikit-image aims to:
- To provide high quality, well-documented and easy-to-use implementations of common image processing algorithms.
- To facilitate education in image processing.
- To address industry challenges.
First, here is how you can get scikit-image:
[crayon-67679cf8d2cae985581136/]Next, we will show the demo of the scikit-image library to perform skeletonization to the sample image. Skeletonization reduces binary objects to 1 pixel wide representations. This can be useful for feature extraction, and/or representing an object’s topology.
Run the following code inside the lower Memo of Python4Delphi Demo01 GUI:
[crayon-67679cf8d2cb0918767219/]scikit-image Skeletonize Result
Skeletonize effect works by making successive passes of the image. On each pass, border pixels are identified and removed on the condition that they do not break the connectivity of the corresponding object.
How do I perform Image Processing with Mahotas Library on Windows?
Mahotas is a fast computer vision algorithms library (all implemented in C++ for speed) that operates over NumPy arrays. Mahotas supports Python 2.7 and 3.4+.
Currently, Mahotas has over 100 functions for image processing and computer vision and it keeps growing.
Here are some notable algorithms provided by Mahotas:
- Watershed
- Convex points calculations.
- Hit & miss, thinning.
- Zernike & Haralick, LBP, and TAS features.
- Speeded-Up Robust Features (SURF), a form of local features.
- Thresholding.
- Convolution.
- Sobel edge detection.
- Spline interpolation
- SLIC superpixels.
Are you looking for a powerful Image Processing library and build a nice GUI for them? This section will show you how to get started!
First, here is how you can get Mahotas:
[crayon-67679cf8d2cb1477420521/]The following is a code example of Mahotas to use the thresholding to transform an image to black and white nicely (run this inside the lower Memo of Python4Delphi Demo01 GUI):
[crayon-67679cf8d2cb3144520988/]Here is the Mahotas Thresholding Result in the Python GUI:
Conclusion
In conclusion, it is very easy to work with digital image processing tasks now, compared to, let’s say, 5-10 years ago. This is achieved through the development of various libraries and processing methods (we have tried five of them), as well as through the development of computing hardware and specialized processors.