日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

对标记点进行三角化

發布時間:2024/9/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对标记点进行三角化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

《Python計算機視覺編程》(《Programming Computer Vision with Python》)一書的第三章第二節分段仿射扭曲一節中,使用狄洛克三角剖分方法時提示錯誤

ModuleNotFoundError: No module named ‘matplotlib.delaunay’

即,matplotlib庫中已經不再支持delaunay模塊了。所以我們需要尋找一個別的模塊來代替它。我在網上找到了from scipy.spatial import Delaunay 這個方法,scipy庫中依然保留有這個模塊,所以可以用scipy庫來代替matplotlib這個庫。
具體實現函數如下:(取自網絡)

from scipy.spatial import Delaunay import numpy as np import matplotlib.pyplot as plt# Triangle Settings width = 100 height = 100 pointNumber = 100 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')plt.show()

輸出結果:

這個版本的代碼添加了將三角形內部填充上顏色的過程。這個過程可以省略。所以代碼可修剪為:

from scipy.spatial import Delaunay import numpy as np import matplotlib.pyplot as plt# Triangle Settings width = 100 height = 100 pointNumber = 100 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 plt.figure(figsize=(7, 3)) plt.triplot(points[:, 0], points[:, 1], tri.simplices.copy())# 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')plt.show()

結果如圖:

如果你想要得到和書中例子類似的圖片,只需要將上面的代碼中隨機取值部分更改為正態分布隨機取值就可以了,修改后代碼如下:

from scipy.spatial import Delaunay import numpy as np import matplotlib.pyplot as plt# Triangle Settings pointNumber = 100 points = np.zeros((pointNumber, 2)) x, y = np.random.standard_normal((2, pointNumber)) points[:, 0] = x points[:, 1] = y# 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 plt.figure(figsize=(7, 3)) plt.triplot(points[:, 0], points[:, 1], tri.simplices.copy())# 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')plt.show()

結果為:

本文基于python3+win10環境。

總結

以上是生活随笔為你收集整理的对标记点进行三角化的全部內容,希望文章能夠幫你解決所遇到的問題。

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