最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用PyVista進行mesh的色彩映射的實現(xiàn)

 更新時間:2021年04月02日 09:56:04   作者:薛定貓  
這篇文章主要介紹了Python利用PyVista進行mesh的色彩映射的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近項目中需要對mesh做一個色彩映射,無意間發(fā)現(xiàn)vtk的封裝庫pyvista相當(dāng)好用,就試了試,在此做一個總結(jié)。

PyVista簡介

PyVista是什么

PyVista 是一個:

  • VTK for humans”, 可視化工具包(VTK)的高級API
  • 空間數(shù)據(jù)的網(wǎng)格數(shù)據(jù)結(jié)構(gòu)與濾波方法
  • 使3D繪圖更加簡單,可用于大型/復(fù)雜數(shù)據(jù)的圖像化

PyVista(以前的vtki)是可視化工具包(VTK)的一個助手模塊,它采用了一種不同的方法,通過NumPy和直接數(shù)組訪問與VTK進行接口。這個包提供了一個python化的、文檔化良好的接口,展示了VTK強大的可視化后端,以方便對空間引用的數(shù)據(jù)集進行快速原型化、分析和可視化集成。

該模塊可用于演示文稿和研究論文的科學(xué)繪圖,以及其他依賴網(wǎng)格的Python模塊的支持模塊。

參考:https://docs.pyvista.org/index.html

github

官方教程

pyvista和其他3D可視化工具比較

在這里插入圖片描述

參考:https://github.com/pyvista/pyvista/issues/146

pyvista使用

安裝

pip install pyvista -i https://pypi.tuna.tsinghua.edu.cn/simple

I/O讀取及可視化

mesh類型

pyvista支持讀取大多數(shù)常見的mesh文件類型,比如PLY,VTK,STL ,OBJ ,BYU 等,一些不常見的mesh文件類型,比如FEniCS/Dolfin_ XML format

(很遺憾,pyvista不支持點云PCD格式,不過可以通過pcdpy、pclpy、python-pcl等庫來讀取pcd文件)

import pyvista as pv
# 讀取
mesh = pv.read('pointCloudData/data.vtk')
# 顯示
mesh.plot()
# 其他類似
mesh = pv.read('pointCloudData/data.ply')
……

圖片類型

支持讀取圖片類型數(shù)據(jù)JPEG, TIFF, PNG等

# 讀取
image = pv.read('my_image.jpg')
# 顯示
image.plot(rgb=True, cpos="xy")

# 其余圖片類型類似
……

mesh彩色映射

項目中需要用到根據(jù)高度來對mesh進行彩色映射,在pyvista中大概有四種方法

自定義

代碼

import pyvista as pv
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np

def mesh_cmp_custom(mesh, name):
 """
 自定義色彩映射
 :param mesh: 輸入mesh
 :param name: 比較數(shù)據(jù)的名字
 :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 # Define the colors we want to use
 blue = np.array([12 / 256, 238 / 256, 246 / 256, 1])
 black = np.array([11 / 256, 11 / 256, 11 / 256, 1])
 grey = np.array([189 / 256, 189 / 256, 189 / 256, 1])
 yellow = np.array([255 / 256, 247 / 256, 0 / 256, 1])
 red = np.array([1, 0, 0, 1])

 c_min = mesh[name].min()
 c_max = mesh[name].max()
 c_scale = c_max - c_min

 mapping = np.linspace(c_min, c_max, 256)
 newcolors = np.empty((256, 4))
 newcolors[mapping >= (c_scale * 0.8 + c_min)] = red
 newcolors[mapping < (c_scale * 0.8 + c_min)] = grey
 newcolors[mapping < (c_scale * 0.55 + c_min)] = yellow
 newcolors[mapping < (c_scale * 0.3 + c_min)] = blue
 newcolors[mapping < (c_scale * 0.1 + c_min)] = black

 # Make the colormap from the listed colors
 my_colormap = ListedColormap(newcolors)
 mesh.plot(scalars=name, cmap=my_colormap)

if __name__ == '__main__':
 mesh = pv.read('pointCloudData/1.ply')
 mesh_cmp_custom(mesh, 'y_height')

效果:

在這里插入圖片描述

使用pyvista自帶的cmp

函數(shù)mesh.plot(scalars=name, cmap='viridis_r')

其中cmap支持的樣式:

‘Accent', ‘Accent_r', ‘Blues', ‘Blues_r', ‘BrBG', ‘BrBG_r', ‘BuGn', ‘BuGn_r', ‘BuPu', ‘BuPu_r', ‘CMRmap', ‘CMRmap_r', ‘Dark2', ‘Dark2_r', ‘GnBu', ‘GnBu_r', ‘Greens', ‘Greens_r', ‘Greys', ‘Greys_r', ‘OrRd', ‘OrRd_r', ‘Oranges', ‘Oranges_r', ‘PRGn', ‘PRGn_r', ‘Paired', ‘Paired_r', ‘Pastel1', ‘Pastel1_r', ‘Pastel2', ‘Pastel2_r', ‘PiYG', ‘PiYG_r', ‘PuBu', ‘PuBuGn', ‘PuBuGn_r', ‘PuBu_r', ‘PuOr', ‘PuOr_r', ‘PuRd', ‘PuRd_r', ‘Purples', ‘Purples_r', ‘RdBu', ‘RdBu_r', ‘RdGy', ‘RdGy_r', ‘RdPu', ‘RdPu_r', ‘RdYlBu', ‘RdYlBu_r', ‘RdYlGn', ‘RdYlGn_r', ‘Reds', ‘Reds_r', ‘Set1', ‘Set1_r', ‘Set2', ‘Set2_r', ‘Set3', ‘Set3_r', ‘Spectral', ‘Spectral_r', ‘Wistia', ‘Wistia_r', ‘YlGn', ‘YlGnBu', ‘YlGnBu_r', ‘YlGn_r', ‘YlOrBr', ‘YlOrBr_r', ‘YlOrRd', ‘YlOrRd_r', ‘a(chǎn)fmhot', ‘a(chǎn)fmhot_r', ‘a(chǎn)utumn', ‘a(chǎn)utumn_r', ‘binary', ‘binary_r', ‘bone', ‘bone_r', ‘brg', ‘brg_r', ‘bwr', ‘bwr_r', ‘cividis', ‘cividis_r', ‘cool', ‘cool_r', ‘coolwarm', ‘coolwarm_r', ‘copper', ‘copper_r', ‘cubehelix', ‘cubehelix_r', ‘flag', ‘flag_r', ‘gist_earth', ‘gist_earth_r', ‘gist_gray', ‘gist_gray_r', ‘gist_heat', ‘gist_heat_r', ‘gist_ncar', ‘gist_ncar_r', ‘gist_rainbow', ‘gist_rainbow_r', ‘gist_stern', ‘gist_stern_r', ‘gist_yarg', ‘gist_yarg_r', ‘gnuplot', ‘gnuplot2', ‘gnuplot2_r', ‘gnuplot_r', ‘gray', ‘gray_r', ‘hot', ‘hot_r', ‘hsv', ‘hsv_r', ‘inferno', ‘inferno_r', ‘jet', ‘jet_r', ‘magma', ‘magma_r', ‘nipy_spectral', ‘nipy_spectral_r', ‘ocean', ‘ocean_r', ‘pink', ‘pink_r', ‘plasma', ‘plasma_r', ‘prism', ‘prism_r', ‘rainbow', ‘rainbow_r', ‘seismic', ‘seismic_r', ‘spring', ‘spring_r', ‘summer', ‘summer_r', ‘tab10', ‘tab10_r', ‘tab20', ‘tab20_r', ‘tab20b', ‘tab20b_r', ‘tab20c', ‘tab20c_r', ‘terrain', ‘terrain_r', ‘turbo', ‘turbo_r', ‘twilight', ‘twilight_r', ‘twilight_shifted', ‘twilight_shifted_r', ‘viridis', ‘viridis_r', ‘winter', ‘winter_r'

代碼

import pyvista as pv
def mesh_cmp(mesh, name):
 """
  使用進行plot自帶的色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數(shù)據(jù)的名字
  :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mesh.plot(scalars=name, cmap='viridis_r')
 
if __name__ == '__main__':
 mesh = pv.read('vtkData/airplane.ply')
 mesh_cmp(mesh, 'y_height')

效果

在這里插入圖片描述

使用Matplotlib的cmp

代碼

import pyvista as pv
import matplotlib.pyplot as plt

def mesh_cmp_mpl(mesh, name):
 """
  使用Matplotlib進行色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數(shù)據(jù)的名字
  :return:
  """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mlp_cmap = plt.cm.get_cmap("viridis", 25)
 mesh.plot(scalars=name, cmap=mlp_cmap)
 
if __name__ == '__main__':
 mesh = pv.read('vtkData/airplane.ply')
 mesh_cmp_mpl(mesh, 'y_height')

效果

在這里插入圖片描述

使用colorcet的cmp

需要先安裝colorcet:

pip install colorcet

使用方法和上面幾種方法類似,若想使用colorcet的colormaps中的hot:

mesh.plot(scalars=name, cmap=“hot”)

代碼

def mesh_cmp_colorcet(mesh, name):
 """
  使用進行colorcet進行色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數(shù)據(jù)的名字
  :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mesh.plot(scalars=name, cmap=colorcet.fire)
 
if __name__ == '__main__':
 mesh = pv.read('vtkData/airplane.ply')
 mesh_cmp_colorcet(mesh, 'y_height')

效果:

在這里插入圖片描述

總結(jié)

pyvista相當(dāng)強大,而且比直接用vtk更加方便(代碼量肉眼可見的降低?。?/p>

到此這篇關(guān)于Python利用PyVista進行mesh的色彩映射的實現(xiàn)的文章就介紹到這了,更多相關(guān)PyVista mesh色彩映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python字符串格式化str.format()方法的實現(xiàn)

    Python字符串格式化str.format()方法的實現(xiàn)

    字符串的格式化是一個非常重要的功能,用于創(chuàng)建包含變量值的字符串,本來就來介紹一下Python字符串格式化str.format()方法的實現(xiàn),感興趣的可以了解一下
    2023-11-11
  • Python調(diào)用C/C++的方法解析

    Python調(diào)用C/C++的方法解析

    這篇文章主要介紹了Python調(diào)用C/C++的方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • python進程間通信Queue工作過程詳解

    python進程間通信Queue工作過程詳解

    這篇文章主要介紹了python進程間通信Queue工作過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Python與R語言的簡要對比

    Python與R語言的簡要對比

    這篇文章主要介紹了Python與R語言的簡要對比,小編覺得還挺不錯的,這里分享給大家,需要的朋友可以了解下。
    2017-11-11
  • 詳解Python的文件處理

    詳解Python的文件處理

    這篇文章主要為大家介紹了Python的文件處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 表格梳理python內(nèi)置數(shù)學(xué)模塊math分析詳解

    表格梳理python內(nèi)置數(shù)學(xué)模塊math分析詳解

    這篇文章主要為大家介紹了python內(nèi)置數(shù)學(xué)模塊math的分析詳解,文中通過表格梳理的方式以便讓大家在學(xué)習(xí)過程中一目望去清晰明了,有需要的朋友可以借鑒參考下
    2021-10-10
  • Python基于pywinauto實現(xiàn)的自動化采集任務(wù)

    Python基于pywinauto實現(xiàn)的自動化采集任務(wù)

    這篇文章主要介紹了Python基于pywinauto實現(xiàn)的自動化采集任務(wù),模擬了輸入單詞, 復(fù)制例句, 獲取例句, 清空剪切板, 然后重復(fù)這個操作,需要的朋友可以參考下
    2023-04-04
  • python獲取全國最新省市區(qū)數(shù)據(jù)并存入表實例代碼

    python獲取全國最新省市區(qū)數(shù)據(jù)并存入表實例代碼

    我們在開發(fā)中經(jīng)常會遇到獲取省市區(qū)等信息的時候,下面這篇這篇文章主要給大家介紹了關(guān)于python獲取全國最新省市區(qū)數(shù)據(jù)并存入表的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Python?Prim算法通過遍歷墻實現(xiàn)迷宮的生成

    Python?Prim算法通過遍歷墻實現(xiàn)迷宮的生成

    之前,我們在另外一篇文章中使用Prim算法生成了一個完美迷宮,利用的是遍歷網(wǎng)格的方法,這一次,我們要教教大家用遍歷墻的方法生成,感興趣的可以收藏一下
    2023-01-01
  • Python Django 封裝分頁成通用的模塊詳解

    Python Django 封裝分頁成通用的模塊詳解

    這篇文章主要介紹了Python Django 封裝分頁成通用的模塊詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08

最新評論

微博| 库尔勒市| 泰安市| 秦安县| 武强县| 油尖旺区| 莱西市| 婺源县| 蒲城县| 东阳市| 蓬溪县| 衡东县| 美姑县| 崇左市| 南华县| 香河县| 盐城市| 定远县| 会昌县| 搜索| 健康| 永靖县| 开原市| 南康市| 刚察县| 克拉玛依市| 禹州市| 辉县市| 大余县| 广灵县| 宽甸| 雅安市| 科技| 青川县| 同心县| 湖口县| 贺州市| 江都市| 江阴市| 延川县| 城步|