5 EASY Ways To Integrate Image Processing In Your Apps

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, pels, 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:

  1. Easy to learn and easy to use, without losing the quality of programming decisions.
  2. Python has been on the market for a long time and has been established.
  3. Python continues to evolve dynamically.
  4. With Python, you can use the coolest OOP solutions and you can write simple and effective code.
  5. 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.

python4delphi run demo01
Open Demo01.dproj.

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:

Don’t forget to put the path where your Pillow installed, to the System Environment Variables:

System Environment Variable Examples

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):

Here is the result in Python GUI

pillow demo
pillow demo

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:

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:

Here is the Matplotlib for Image Processing result in the Python GUI:

Matplotlib Demo with Python4Delphi in Windows.
Matplotlib Demo with Python4Delphi in Windows.

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:

scikit-learn Python4Delphi Results

scikit-learn Demo with Python4Delphi in Windows.
scikit-learn Demo with Python4Delphi in Windows.

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:

  1. To provide high quality, well-documented and easy-to-use implementations of common image processing algorithms.
  2. To facilitate education in image processing.
  3. To address industry challenges.

First, here is how you can get scikit-image:

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:

scikit-image Skeletonize Result

scikit-image Demo with Python4Delphi in Windows.
scikit-image Demo with Python4Delphi in Windows.

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:

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):

Here is the Mahotas Thresholding Result in the Python GUI:

Mahotas Demo with Python4Delphi in Windows.
Mahotas Demo with Python4Delphi in Windows.

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.