python三角网格代码_Python 实现 Delaunay Triangulation
Delaunay Triangulation 是一種空間劃分的方法,它能使得分割形成的三角形最小的角盡可能的大,關于 Delaunay Triangulation 的詳細介紹,請參考這里,Delaunay Triangulation在很多領域都有應用,科學計算領域它是有限元和有限體積法劃分網格的重要方法,除此之外在圖像識別、視覺藝術等領域也有它的身影。
貼一段有趣的油管視頻,用 Delaunay Triangulation 進行人臉識別的演示:Delaunay Triangulation and Voronoi Diagram in OpenCV
接下來寫一下怎么用 Python 實現 Delaunay Triangulation,需要用到的模塊有Numpy, Matplotlib 和 Scipy,基本的思路是隨機制造幾個點,然后利用scipy.spatial.Delaunay對這些點進行處理配對三角形,最后用matplotlib的tripcolor(填充三角形顏色,如果不需要填充顏色,可以用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
總結
以上是生活随笔為你收集整理的python三角网格代码_Python 实现 Delaunay Triangulation的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 交通灯管理系统思路总结
- 下一篇: c语言编程练习:温度转换
