Comments (6)
NaN
values plot as solid grey by default - you could change the nan_color
argument when plotting or change it in the vtki.rcParams['nan_color'] = 'yellow'
from pyvista-support.
Andrew Annex:
I take that back, I did have masked arrays but there are no masked values, other dems will have them though so it is good to know
but yeah I don’t get the rotation issue, there should be a 1:1 correspondence with each dem pixel and each img pixel
doing: “grid.plot(texture=vtki.numpy_to_texture(np.rot90(img)))” gets me closer, but there is still that odd repeat at either end of the mesh
thanks for the quick responses though, I need to go but can check back in later
from pyvista-support.
So real quick… are you sure you want to map your data onto the mesh as a texture? If your data values have:
a 1:1 correspondence with each dem pixel and each img pixel
then you might actually want to put the data on the cells (“pixels”) of your mesh and plot it as data values - texture mapping is meant for plotting images that don’t have a 1:1 correspondence with the mesh discretization.
Try this for example:
import numpy as np
import vtki
# Panel doesn't handle structured grids very well
vtki.rcParams['use_panel'] = False
# load the data
dem = np.load('dem_vtki.npy')
rimg = np.load('img_vtki.npy')
# A nice default view direction for this data
cpos = [-1,1,1]
# Make a mesh from the DEM
x = np.arange(0, dem.shape[0])
y = np.arange(0, dem.shape[1])
mx, my = np.meshgrid(x,y, indexing='ij')
grid = vtki.StructuredGrid(mx,my,dem)#.elevation()
# Add image data to the mesh with 1:1 correspondance
grid['dem'] = rimg.ravel(order='F')
grid.plot(cpos=cpos, cmap='gray')
Which produces:
from pyvista-support.
Andrew Annex:
ahh, that works, I am not familiar with all of the terminology yet so I may have made that more confusing than it needed to bbe
so as a follow on question, say if I have the same image but with 4x the resolution, can I “drape” that texture over the lower resolution cells or would I need to interpolate the elevation data to match the dimensions?
from pyvista-support.
That’s when texture mapping makes sense!
from pyvista-support.
Solves problem originally posted by Andrew Annex - then interpolates the given image to a high resolution to showcase texture mapping a high res image to a low res mesh
import numpy as np
import vtki
# Panel doesn't handle structured grids very well
vtki.rcParams['use_panel'] = False
# load the data
dem = np.load('dem_vtki.npy')
rimg = np.load('img_vtki.npy')
x = np.arange(0, dem.shape[0])
y = np.arange(0, dem.shape[1])
mx, my = np.meshgrid(x,y, indexing='ij')
grid = vtki.StructuredGrid(mx,my,dem)
# Make sure to map texture on a flat plane
# defaults to best fitting plane which is not
# what you need
e = grid.bounds
grid.texture_map_to_plane(origin=[e[0], e[2], 0],
point_u=[e[1], e[3], 0],
point_v=[e[0], e[3], 0],
inplace=True)
norm = lambda x: (x - np.nanmin(x)) / (np.nanmax(x) - np.nanmin(x))
img = norm(rimg) * 255
img = np.dstack((img,img,img)).astype(np.uint8)
img = np.flip(np.moveaxis(np.transpose(img), 0, -1), 0)
print(dem.shape, img.shape, img.min(), img.max(), img.dtype,)
texture = vtki.numpy_to_texture(img)
grid.plot(texture=texture, cpos=[-1,1,1])
##############################################
# Interpolate the image
from scipy.interpolate import griddata
# Make a mesh from the DEM
xi = np.linspace(0, dem.shape[0], num=dem.shape[0]*4)
yi = np.linspace(0, dem.shape[1], num=dem.shape[1]*4)
mxi, myi = np.meshgrid(xi, yi)
result = griddata(grid.points[:,0:2],
rimg.ravel(order='F'),
tuple((mxi, myi)),
method='linear')
# Make texture:
norm = lambda x: (x - np.nanmin(x)) / (np.nanmax(x) - np.nanmin(x))
result = norm(result) * 255
fake_image = result.reshape((mxi.shape[0], mxi.shape[1]), order='F')
# fake_image = np.swapaxes(fake_image, 0, 1)
# fake_image = np.flip(fake_image, 1)
fake_image = np.flip(fake_image, 0)
img = np.dstack((fake_image,fake_image,fake_image)).astype(np.uint8)
print(img.shape, img.min(), img.max(), img.dtype,)
import matplotlib.pyplot as plt
plt.imshow(img)
plt.show()
texture = vtki.numpy_to_texture(img)
grid.plot(texture=texture, cpos=[-1,1,1], )
from pyvista-support.
Related Issues (20)
- create uniform mesh HOT 3
- Change the orientation of a scalar bar's title
- Applying textures on models from .obj files HOT 6
- How to create an Unstructured grid from points and faces HOT 2
- Type of data, plotting with glyphs HOT 3
- Controlling the thickness and orientation of a slice in Pyvista HOT 3
- Controlling the image position in window, without any click
- Mask/Remove Voxels in Air (small intersection with surface) HOT 3
- custom slider parameters
- running pyvista on headless machine with Centos OS HOT 1
- Seismic Horizon with Pyvista HOT 8
- Algorithm in sample function?
- Automatically updates HOT 3
- Slow interaction HOT 1
- read from text HOT 4
- Uniform Grid thresholding question HOT 8
- Take data by an input x,y,x coordinate (csv file) from vtk data file HOT 4
- Program broke when creating a mesh using pv.PolyData() HOT 4
- ATTENTION: Please move questions to PyVista's Discussions HOT 1
- plotter.enable_terrain_style not working for me HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvista-support.