Do you want to perform some Image Processing tasks in your Delphi GUI App? This post will get to understand how to use Mahotas Python Library using Python4Delphi in Delphi/C++ application. Mahotas is a computer vision and image processing library for Python.
Mahotas contains traditional image processing functions such as filtering and morphological operations as well as more modern computer vision functions for feature computation, including interest point detection and local descriptors.
Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi. They let you easily execute Python scripts, create new Python modules and new Python types. You can use Python4Delphi a number of different ways such as:
- Create a Windows GUI around your existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed-sensitive Python apps with functions from Delphi for more speed.
Prerequisites.
- If not python and Python4Delphi is not installed on your machine, Check this how to run a simple python script in Delphi application using Python4Delphi sample app
- Open windows open command prompt, and type pip install -U mahotas to install Mahotas. For more info for Installing Python Modules check here
- First, run the Demo1 project for executing Python script in Python for Delphi. Then load the Texblob sample script in the Memo1 field and press the Execute Script button to see the result. On Clicking Execute Button the script strings are executed using the below code. Go to GitHub to download the Demo1 source.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
Mahotas Python Library sample script details:
- How to label a region in an image. Labeled images are integer images where the values correspond to different regions. I.e., region 1 is all of the pixels which have value 1, region two is the pixels with value 2, and so on. By convention, region 0 is the background and often handled differently.
- Thresholding is a type of image segmentation, where we change the pixels of an image to make the image easier to analyze. In thresholding, we convert an image from color or grayscale into a binary image, i.e., one that is simply black and white. A well-known thresholding method is Otsu’s method which is demonstrated below.
- Color space conversion is the translation of the representation of a color from one basis to another. An RGB image is represented as a 3-dimensional array of shape
(h,w,3)
, where each pixel is represented by three values, red/green/blue. Image color space conversion using rgb2grey,rgb2sepia method were shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import mahotas as mh import mahotas.demos import numpy as np from pylab import * from os import path #Labeling the region regions = np.zeros((8,8), bool) regions[:3,:3] = 1 regions[6:,6:] = 1 labeled, nr_objects = mh.label(regions) imshow(labeled, interpolation='nearest') show() #Threshold photo = mahotas.demos.load('luispedro', as_grey=True) photo = photo.astype(np.uint8) gray() imshow(photo) show() #thresholding methods uising Otsu’s method: Alternatively you can use Riddler-Calvard Method T_otsu = mahotas.otsu(photo) print(T_otsu) gray() imshow(photo > T_otsu) show() #Color space Conversions lena = mh.demos.load('lena') print(lena.shape) lenag = mh.colors.rgb2grey(lena) imshow(lenag) show() #Convert to sepia lenaimg = mh.demos.load('lena') lenas = mh.colors.rgb2sepia(lenaimg) imshow(lenas) show() |
Note: Samples used for demonstration were picked from here with only the difference of printing the outputs. You can check the APIs and some more samples from the same place.
You have read the quick overview of Mahotas library, download this library from here, and perform image processing fuctions such as watershed, filtering regions, morphological processing, in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.