RAD Studio Easily Perform Powerful Computer Vision Functions In Python GUI App With Mahotas Library

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Easily Perform Powerful Computer Vision Functions In Python GUI App With Mahotas Library
January 31, 2021 by Anbarasan

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 Для просмотра ссылки Войди или Зарегистрируйся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 Для просмотра ссылки Войди или Зарегистрируйся
  • Open windows open command prompt, and type pip install -U mahotas to install Mahotas. For more info for Installing Python Modules Для просмотра ссылки Войди или Зарегистрируйся
  • 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 Для просмотра ссылки Войди или Зарегистрируйся source.
Код:
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.
Python:
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()
1612127490151.png
Labeling Images
1612127504159.png
Thresholding Otsu’s Method

1612127518700.png
Mahotas Color Space conversions Demo
Note: Samples used for demonstration were picked from Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся, and perform image processing fuctions such as watershed, filtering regions, morphological processing, in your applications. Check out Для просмотра ссылки Войди или Зарегистрируйся and easily build Python GUIs for Windows using Delphi.