Are you looking for a powerful computer vision library and building a nice GUI for them? Try Mahotas library for Python. For the GUI part, you can run it seamlessly with Python4Delphi (P4D). P4D is a free and simple tool that allows you to run Python scripts as well as create new Python modules and types in Delphi.
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.
This post will guide you on how to run the Mahotas library to solve computer vision problems and use Python for Delphi to display it in the Python Windows GUI Builder.
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.
Table of Contents
Demo
Let’s do a sanity test by running this simple example (using an example image that is shipped with Mahotas) by calling watershed using above threshold regions as a seed (we use Otsu to define threshold):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Import using ``mh`` abbreviation which is common: import mahotas as mh from pylab import imshow, show # Load and show one of the demo images im = mh.demos.load('nuclear') imshow(im) show() # Automatically compute a threshold T_otsu = mh.thresholding.otsu(im) print(T_otsu) # Label the thresholded image (thresholding is done with numpy operations seeds,nr_regions = mh.label(im > T_otsu) print(seeds,nr_regions) # Call seeded watershed to expand the threshold labeled = mh.cwatershed(im.max() - im, seeds) print(labeled) |
The result in our Python4Delphi VCL:
For the next example, we try a very simple example of using mahotas.distance (which computes a distance map):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import pylab as p import numpy as np import mahotas as mh f = np.ones((256,256), bool) f[100:,120:] = False f[62:72,16:24] = False # f is basically True with the exception of two islands: one in the lower-right # corner, another, middle-left dmap = mh.distance(f) p.imshow(dmap) p.show() |
For the final example, we will invoke thresholding functions, to automatically reduce and change colors in the image. For this example, I load external samples (not included in Mahotas image samples).
You can load your own images by writing their path and filenames like the usual way, or you can also edit the __init__.py file here: “C:UsersUsernameAppDataLocalProgramsPythonPython38Libsite-packagesmahotasdemos”. I load my own image which is defined by ‘cat’: ‘cat.jpg’ in the __init__.py file.
We use the Ridler-Calvard threshold in the following code example (visit the reference here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import mahotas as mh import numpy as np from pylab import imshow, gray, show, subplot from os import path # Load your own photo originalPhoto = mh.demos.load('cat') # Load photo in grayscale photo = mh.demos.load('cat', as_grey=True) # Convert to integer values (using numpy operations) photo = photo.astype(np.uint8) # Compute Ridler-Calvard threshold T_rc = mh.rc(photo) thresholded_rc = (photo > T_rc) # Now call pylab functions to display the image subplot(2,1,1) imshow(originalPhoto) subplot(2,1,2) imshow(thresholded_rc) show() |
Congratulations, now you have learned how to run the Mahotas library to solve computer vision problems and use Python for Delphi to display it in the Delphi Windows GUI app! Now you can make various modifications to your images or learn more computer vision operations using Mahotas library and Python4Delphi.
Check out the Mahotas computer vision library for Python and use it in your projects: https://pypi.org/project/mahotas/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi