如何制作一个超快速的计算机视觉Windows应用程序

260 阅读3分钟

你是否在寻找一个强大的计算机视觉库,并为它们建立一个漂亮的图形用户界面?试试Mahotas的Python库。对于GUI部分,你可以用Python4Delphi(P4D)无缝运行它。P4D是一个免费而简单的工具,它允许你运行Python脚本,以及在Delphi中创建新的Python模块和类型。

Mahotas是一个快速的计算机视觉算法库(全部用C++实现以提高速度),在NumPy数组上操作。Mahotas支持Python 2.7和3.4+。

目前,Mahotas有超过100个用于图像处理和计算机视觉的函数,而且它还在不断增加。

下面是Mahotas提供的一些值得注意的算法。

  • 分水岭
  • 凸点计算。
  • 命中率和失误率,变薄。
  • Zernike & Haralick, LBP, 和TAS特征。
  • 加速强化特征(SURF),局部特征的一种形式。
  • 阈值处理。
  • 卷积。
  • Sobel边缘检测。
  • 曲线插值
  • SLIC超级像素。

这篇文章将指导你如何运行Mahotas库来解决计算机视觉问题,并使用Python for Delphi在Delphi Windows GUI应用中显示它

首先,使用Python4Delphi中的项目Demo1 与RAD Studio打开并运行我们的Python GUI。然后将脚本插入到下层备忘录中,点击执行按钮,在上层备忘录中得到结果。你可以在GitHub上找到Demo1的源代码。关于Delphi如何在这个神奇的Python GUI中运行你的Python代码的幕后细节,可以在这个链接中找到。

How To Make An Ultra-Fast Computer Vision Windows App - demo

演示

让我们做一个理智的测试,运行这个简单的例子(使用Mahotas提供的示例图像),以上述阈值区域作为种子调用分水岭(我们使用大津来定义阈值)。

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

我们的Python4Delphi VCL中的结果。

How To Make An Ultra-Fast Computer Vision Windows App - results in action

How To Make An Ultra-Fast Computer Vision Windows App - an array of results

对于下一个例子,我们尝试使用mahotas.distance(计算距离图)的一个非常简单的例子。

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

How To Make An Ultra-Fast Computer Vision Windows App - second example

对于最后一个例子,我们将调用阈值处理函数,自动减少和改变图像中的颜色。对于这个例子,我加载了外部样本(不包括在Mahotas图像样本中)。

你可以像通常的方式一样通过写出它们的路径和文件名来加载你自己的图像,或者你也可以在这里编辑__init__.py文件。"C:UsersUsernameAppDataLocalProgramsPythonPython38Libsite-packagesmahotasdemos" 。我加载了我自己的图片,它由'cat'定义:在__init__.py文件中的'cat.jpg'。

我们在下面的代码例子中使用了Ridler-Calvard阈值(请访问这里的参考资料)。

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

How To Make An Ultra-Fast Computer Vision Windows App - recognizing cats

恭喜你,现在你已经学会了如何运行Mahotas库来解决计算机视觉问题,并使用Python for Delphi在Delphi Windows GUI应用程序中显示它现在你可以对你的图像进行各种修改,或者使用Mahotas库和Python4Delphi学习更多计算机视觉操作。

查看Python的Mahotas计算机视觉库并在你的项目中使用它:https://pypi.org/project/mahotas/

查看Python4Delphi ,它可以让你轻松地使用Delphi为Windows建立Python图形用户界面:https://github.com/pyscripter/python4delphi