Python 实现 Delaunay Triangulation

Delaunay Triangulation 是一种空间划分的方法,它能使得分割形成的三角形最小的角尽可能的大,关于 Delaunay Triangulation 的详细介绍,请参考这里,Delaunay Triangulation在很多领域都有应用,科学计算领域它是有限元和有限体积法划分网格的重要方法,除此之外在图像识别、视觉艺术等领域也有它的身影。
贴一段有趣的油管视频,用 Delaunay Triangulation 进行人脸识别的演示:Delaunay Triangulation and Voronoi Diagram in OpenCV
接下来写一下怎么用 Python 实现 Delaunay Triangulation,需要用到的模块有Numpy, MatplotlibScipy,基本的思路是随机制造几个点,然后利用scipy.spatial.Delaunay对这些点进行处理配对三角形,最后用matplotlibtripcolor(填充三角形颜色,如果不需要填充颜色,可以用triplot).

下面贴上代码:

from scipy.spatial import Delaunay
import numpy as np
import matplotlib.pyplot as plt

# Triangle Settings
width = 200
height = 40
pointNumber = 1000
points = np.zeros((pointNumber, 2))
points[:, 0] = np.random.randint(0, width, pointNumber)
points[:, 1] = np.random.randint(0, height, pointNumber)

# Use scipy.spatial.Delaunay for Triangulation
tri = Delaunay(points)

# Plot Delaunay triangle with color filled
center = np.sum(points[tri.simplices], axis=1)/3.0
color = np.array([(x - width/2)**2 + (y - height/2)**2 for x, y in center])
plt.figure(figsize=(7, 3))
plt.tripcolor(points[:, 0], points[:, 1], tri.simplices.copy(), facecolors=color, edgecolors='k')


# Delete ticks, axis and background
plt.tick_params(labelbottom='off', labelleft='off', left='off', right='off',
                bottom='off', top='off')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['top'].set_color('none')

# Save picture
plt.savefig('Delaunay.png', transparent=True, dpi=600)

贴上结果:

Delaunay.png

来自fangs.in

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Machine Learning in Python (Scikit-learn)-(No.1) 作者:范淼(人人...
    hzyido阅读 6,294评论 2 13
  • Python 兵器谱 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发...
    hzyido阅读 64,570评论 1 23
  • 快一个半月没有写过东西,不管是博客还是简书。快有一个半月没有画东西。不管是临摹还是创作。你不能说我这一个半月在闲着...
    七公子steven阅读 256评论 0 1
  • 姓名:徐祖德 公司:广东思沃精密机械有限公司 230期_利他1组 272期_乐观2组志工 【日精进打卡第107天】...
    徐祖德阅读 180评论 0 0
  • 今天犯懒,就想翻出以前的文章糊弄了事。翻了新浪微博、QQ空间,看着就乐了…又开始为难,因为实在不像现在的我会写出来...
    伊卡洛斯林阅读 904评论 0 0