Hi Gael,

Thanks for your response. That was very useful for me.

Nevertheless, I had to change some part of my script.

For begin, I created a sphere with 5 labels : 

for i in xrange(xdim):
    for j in xrange(ydim):
        for k in xrange(zdim):
            if (i * vx - cx)**2 + (j * vy - cy)**2 + (k * vz - cz)**2 < (R * R) :
                if k < 10 :
                    data[i,j,k] = 1
                if 10 < k < 20 :
                    data[i,j,k] = 2
                if 20 < k < 30 :
                    data[i,j,k] = 3
                if 30 < k < 40 :
                    data[i,j,k] = 4
                if 40 < k < 50 :
                    data[i,j,k] = 5


For the contour, I used :

contour.filter.minimum_contour = 1
contour.filter.maximum_contour = 5

instead of 

contour.filter.number_of_contours = int(im.max())

And I noticed that :

tvtk.PolyDataConnectivityFilter with extraction_mode = 5, it extracts all regions ( and not = 6 as in the VTK documentation).

Then I followed your advices for the colormap, it works very well.

lut = np.random.randint(256,size=(int(data.max()),4))
lut[:,3] = 255
surf.module_manager.scalar_lut_manager.lut.table = lut

Now, I try to pick a region and mark it. For that, I use :

picker = fig.on_mouse_pick(picker_callback,type="cell")

with the callback function :

def picker_callback(picker_obj):
    # Marking of the region, I just change the transparency for the moment (I look for a other way)
    cellid = data[picker_obj.mapper_position]
    if cellid != 0 :
        lut[cellid-1,3] = 50 
        surf.module_manager.scalar_lut_manager.lut.table =lut
        mlab.draw()

This is okay but the precision for performing pick operation is not terrific even if I try to change the tolerance of the picker, to use the on_mouse_pick with type = "point" and "cell"
But I don't see how I can do for improve that. 

I am attaching a the script if you have the time to run it.

##############################
# Code
##############################

import numpy as np

##############################
# Define the sphere cut in two
##############################

xdim, ydim, zdim = 50, 50, 50
vx, vy, vz = 1., 1., 1.
R = 25
cx = 25
cy = 25
cz = 25

data = np.zeros([xdim, ydim, zdim],np.uint8)

for i in xrange(xdim):
    for j in xrange(ydim):
        for k in xrange(zdim):
            if (i * vx - cx)**2 + (j * vy - cy)**2 + (k * vz - cz)**2 < (R * R) :
                if k < 10 :
                    data[i,j,k] = 1
                if 10 < k < 20 :
                    data[i,j,k] = 2
                if 20 < k < 30 :
                    data[i,j,k] = 3
                if 30 < k < 40 :
                    data[i,j,k] = 4
                if 40 < k < 50 :
                    data[i,j,k] = 5


##########################################################
# Using of "Tvtk segmentation example" from mayavi 
# to display the sphere
##########################################################

from enthought.mayavi import mlab
from enthought.tvtk.api import tvtk

#fig = mlab.figure(bgcolor=(0, 0, 0), size=(400, 500))
#fig = mlab.figure(bgcolor=(1, 1, 1), size=(400, 500))
fig = mlab.figure(size=(400, 500))
fig.scene.disable_render = True

src = mlab.pipeline.scalar_field(data)
src.update_image_data = True

contour = mlab.pipeline.contour(src, )
contour.filter.auto_contours = True
contour.filter.minimum_contour = 1
contour.filter.maximum_contour = 5

connect_ = tvtk.PolyDataConnectivityFilter(extraction_mode=5,color_regions=True)
connect = mlab.pipeline.user_defined(contour, filter=connect_)

surf = mlab.pipeline.surface(connect)

#col = np.array([[0,0,255,255],[255,255,255,255],[255,0,0,255],[255,255,255,255],[0,0,255,255]])
col = np.random.randint(256,size=(int(data.max()),4))
col[:,3] = 255

surf.module_manager.scalar_lut_manager.lut.table = col

###########################################################
# Using of the "on_mouse_pick" method of the scene 
# to change the color
##########################################################

def picker_callback(picker_obj):
    print  picker_obj.mapper_position
    cellid = data[picker_obj.mapper_position]
    print cellid
    if cellid != 0 :
        col[cellid-1,3] = 50 
        surf.module_manager.scalar_lut_manager.lut.table = col
        mlab.draw()

def picker_return(picker_obj):
    print  picker_obj.mapper_position
    cellid = data[picker_obj.mapper_position]
    print cellid
    if cellid != 0 :
        col[cellid-1,3] = 255 
        surf.module_manager.scalar_lut_manager.lut.table = col
        mlab.draw()

picker =fig.on_mouse_pick(picker_callback,type="cell")
picker =fig.on_mouse_pick(picker_return,type="cell",button="Right")

fig.scene.disable_render = False


Thanks a lot.
Eric

On Nov 16, 2010, at 3:27 PM, Gael Varoquaux wrote:

On Tue, Nov 16, 2010 at 02:19:31PM +0100, moscardi wrote:
src = "">
src.update_image_data = True
contour = mlab.pipeline.contour(src, )
contour.filter.number_of_contours = int(im.max())

with im.max = 1041 labels

Then :
connect_ = tvtk.PolyDataConnectivityFilter(extraction_mode=6, color_regions=True)
connect = mlab.pipeline.user_defined(contour, filter=connect_)
surf = mlab.pipeline.surface(contour,colormap="gist_rainbow")

If I use "extraction_mode=6", it display only one region. With
"extraction_mode=5" it display : connect_.number_of_extracted_regions =
697, so I haven't all regions.

It's probably the number of contours that is not right, try using
(2*im.max()). VTK has a factor of two (+1?) in the number contours that I
can never understand.

And finally, when I try to use the picker, I can't update the color of
the picked region.

Yes, it makes sens.

So, here you have a design problem typical of VTK: to be efficient with
VTK, just like most high-level libraries, you need to think in terms of a
few big problems, rather than many small problems. More concretely, you
need to use one surface module (that will create only one actor) rather
than 1041 surface modules. Now the problem comes when you want to modify
a property of one of these individual units. Often the trick in VTK is to
define a numerical value associated with this property on the object of
interest, for instance the scalar, that you associate with the color,
using the colormap.

Here you want to change this association, so you need to change the
colormap. I think the best approach is to define a custom colormap (as in
the example
http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/auto/example_custom_colormap.html
). As you can see in the example, the custom colormap relies on a table
giving the association between values and colors. If you choose that
number to be the same as your number of labels, you can then use the
picker to find the value of the scalar at the location of the pick, and
update this entry in the LUT (as in the example above).

Does that make sens (I need to run to a meeting right now, so these
explaination may be a bit confusing)? I know that this high-level
thinking seems a bit like thinking backward (and it may be) but when you
master it, you can assemble very versatile visualization with little
work.

Gaƫl

Eric MOSCARDI
INRIA - Virtual Plants
CIRAD, Avenue Agropolis
34398 Montpellier Cedex 5, France
04 67 61 58 00 (ask number 60 09)
email : eric.mosca...@sophia.inria.fr


------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
MayaVi-users mailing list
MayaVi-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mayavi-users

Reply via email to