日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python三角网格代码_Python 实现 Delaunay Triangulation

發布時間:2024/1/1 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。