[vtkusers] projecting an RGB image on a sphere (python binding)

Stefan Mauerberger stefan.mauerberger at mnet-online.de
Fri Dec 10 06:19:23 EST 2010


Hi everyone,

I'm a new VTK user. A few days ago I discovered the python bindings of
VTK. I use it to get my data into VTK format. And I use ParaView for
visualization. I'm exited and so far nearly everything works quite well.

Now I want to project an image (2D RGB array) on a sphere. The 'vector
components' should be shown at any point in RGB color space. The
projection works but the coloring of the points does not work. (see
attached sample code)

It would be wonderful if you could help me!

Regards

Stefan

PS: Is there a way to improve the performance. Going through all the
indexes is very slow. The array passed as a whole would be much faster.

> import numpy as np
> import vtk 
> 
> # dummy random RGB image 
> def im(lat,lon):
>     im = np.random.random_integers( 0, 254, 3)
>     im = im.astype('int8')
>     return im.tolist()
> 
> # parameters of the sphere
> Lat = np.linspace( 0.2, 2.1, 200 ).tolist()
> Lon = np.linspace( 1.1, 3.2, 200 ).tolist()
> r   = 1.
> 
> # create VTK points and array
> points = vtk.vtkPoints()
> array  = vtk.vtkTypeInt8Array()
> array.SetNumberOfComponents( 3 )
> for nlat, lat  in enumerate( Lat ):
>     for nlon, lon in enumerate( Lon ):
>         # transform to carthesien coord.
>         x = r * np.sin( lat ) * np.cos( lon )
>         y = r * np.sin( lat ) * np.sin( lon )
>         z = r * np.cos( lat ) 
>         points.InsertNextPoint( ( x, y, z ) )
>         # get RGB random values 
>         R, G, B = im( nlat, nlon)
>         array.InsertNextTuple3( R, G, B )
> 
> # generate VTK structured grid
> sgrid = vtk.vtkStructuredGrid()
> sgrid.SetDimensions( 1, len( Lon ), len( Lat ) ) 
> sgrid.SetPoints( points )
> sgrid.GetPointData().AddArray( array )
> 
> # write to file 
> writer = vtk.vtkStructuredGridWriter()
> writer.SetInput( sgrid )
> writer.SetFileName( 'file.vtk' )
> writer.SetFileTypeToBinary()
> writer.Write()




More information about the vtkusers mailing list