Python利用PyVista進(jìn)行mesh的色彩映射的實(shí)現(xiàn)
最近項(xiàng)目中需要對(duì)mesh做一個(gè)色彩映射,無(wú)意間發(fā)現(xiàn)vtk的封裝庫(kù)pyvista相當(dāng)好用,就試了試,在此做一個(gè)總結(jié)。
PyVista簡(jiǎn)介PyVista是什么PyVista 是一個(gè):
VTK for humans”, 可視化工具包(VTK)的高級(jí)API 空間數(shù)據(jù)的網(wǎng)格數(shù)據(jù)結(jié)構(gòu)與濾波方法 使3D繪圖更加簡(jiǎn)單,可用于大型/復(fù)雜數(shù)據(jù)的圖像化PyVista(以前的vtki)是可視化工具包(VTK)的一個(gè)助手模塊,它采用了一種不同的方法,通過(guò)NumPy和直接數(shù)組訪問(wèn)與VTK進(jìn)行接口。這個(gè)包提供了一個(gè)python化的、文檔化良好的接口,展示了VTK強(qiáng)大的可視化后端,以方便對(duì)空間引用的數(shù)據(jù)集進(jìn)行快速原型化、分析和可視化集成。
該模塊可用于演示文稿和研究論文的科學(xué)繪圖,以及其他依賴(lài)網(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/simpleI/O讀取及可視化
mesh類(lèi)型
pyvista支持讀取大多數(shù)常見(jiàn)的mesh文件類(lèi)型,比如PLY,VTK,STL ,OBJ ,BYU 等,一些不常見(jiàn)的mesh文件類(lèi)型,比如FEniCS/Dolfin_ XML format
(很遺憾,pyvista不支持點(diǎn)云PCD格式,不過(guò)可以通過(guò)pcdpy、pclpy、python-pcl等庫(kù)來(lái)讀取pcd文件)
import pyvista as pv# 讀取mesh = pv.read(’pointCloudData/data.vtk’)# 顯示mesh.plot()# 其他類(lèi)似mesh = pv.read(’pointCloudData/data.ply’)……圖片類(lèi)型
支持讀取圖片類(lèi)型數(shù)據(jù)JPEG, TIFF, PNG等
# 讀取image = pv.read(’my_image.jpg’)# 顯示image.plot(rgb=True, cpos='xy')# 其余圖片類(lèi)型類(lèi)似……mesh彩色映射
項(xiàng)目中需要用到根據(jù)高度來(lái)對(duì)mesh進(jìn)行彩色映射,在pyvista中大概有四種方法
自定義
代碼
import pyvista as pvimport matplotlib.pyplot as pltfrom matplotlib.colors import ListedColormapimport numpy as npdef 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’)
效果:
函數(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 pvdef mesh_cmp(mesh, name): ''' 使用進(jìn)行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’)
效果
代碼
import pyvista as pvimport matplotlib.pyplot as pltdef mesh_cmp_mpl(mesh, name): ''' 使用Matplotlib進(jìn)行色彩映射 :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:
pip install colorcet
使用方法和上面幾種方法類(lèi)似,若想使用colorcet的colormaps中的hot:
mesh.plot(scalars=name, cmap=“hot”)
代碼
def mesh_cmp_colorcet(mesh, name): ''' 使用進(jìn)行colorcet進(jìn)行色彩映射 :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’)
效果:
pyvista相當(dāng)強(qiáng)大,而且比直接用vtk更加方便(代碼量肉眼可見(jiàn)的降低!)
到此這篇關(guān)于Python利用PyVista進(jìn)行mesh的色彩映射的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)PyVista mesh色彩映射內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門(mén)第四篇(腳本變量、函數(shù)、過(guò)程和條件語(yǔ)句)2. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)3. jscript與vbscript 操作XML元素屬性的代碼4. Jsp servlet驗(yàn)證碼工具類(lèi)分享5. XML在語(yǔ)音合成中的應(yīng)用6. 基于PHP做個(gè)圖片防盜鏈7. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)8. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)9. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)10. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
