[vtkusers] Thresholding an image using VTK from PYTHON.

gte631d at mail.gatech.edu gte631d at mail.gatech.edu
Thu Jun 26 14:56:55 EDT 2003


Hi All,
I'm having a problem, with something that should be pretty straighforward.

Purpose: to perform a simple threshold on a vtkImageData(3D by nature) object

Problem: The output of the vtkImgeThreshold object looks like an "empty" 
image.  By "Empty" I mean the Image information of the output from 
vtkImageThreshold Object is similar to the image information
of emptyimage = vtkImageData()

Obviously I am doing somethig wrong (or making a wrong assumption).  Please 
help!

Current method (that look's ok, but doesn't work):

#-------------- Begin current test code Segment -----------------------------

# **Assume that the image we want to operate on is called "inim"<--INPUT IMAGE

print inim.GetClassName()
	##RETRUNS: vtkImageData
	##we can also get info about the dimensions, spacing, etc

print inim.GetPointData().GetScalars()
	##RETURNS a lot of info including the Number of Tuples.


##Now we want to do a simple threshold
thim = vtkImageThreshold()
thim.SetInput(inim)
thim.ThresholdByUpper(45)
thim.SetInValue(1)
thim.SetOutValue(0)
thim.SetOutputScalarTypeToShort()

threshout = thim.GetOutput()
print threshout.GetPointData().GetScalars()
	##Returns "NONE"

## other than for the ClassName of threshout being vtkImageData, it holds no
## similarity (in structure or scalars) to inim.

### What I am trying to get is threshout, such that it is identical in 
structure to inim,
### but whose scalar are thresholded according to the parameters set in thim, 
which a
### a vtkImageThreshold object


#----------------- End Problem Statement ------------------------

#--------- The following Display functions are handy to see necessary info -----
def PrintImageInfo(inim):
    print "GetClassName() = ",		inim.GetClassName()
    print "GetDataDimension() = ",	inim.GetDataDimension()
    print "GetScalarSize() = ",	    inim.GetScalarSize()
    print "GetNumberOfScalarComponents() = ",\
                                    inim.GetNumberOfScalarComponents()
    print "GetDimensions() = ",	    inim.GetDimensions()
    print "GetSpacing() = ", 		inim.GetSpacing()
    print "GetOrigin() = ", 		inim.GetOrigin()
    print "GetExtent() = ", 		inim.GetExtent()
    print "GetScalarRange() = ", 	inim.GetScalarRange()
    print "GetScalarTypeMax () = ", inim.GetScalarTypeMax()
    print "GetScalarTypeMin () = ", inim.GetScalarTypeMin()
    print "GetPointData().GetScalars() :"
    print inim.GetPointData().GetScalars()

def PrintImageScalars(inim):
    print "Print the //Planes// of the 3D mtx"
    dims = inim.GetDimensions()
    print dims
    fill_val = 0
    for zz in range(0, dims[2]):
        for yy in range(0, dims[1]):
            for xx in range(0, dims[0]):
                print inim.GetScalarComponentAsFloat(xx, yy, zz, 0),
            print
        print

def PrintImageThreshObjInfo (thim):
    print "GetReplaceIn () = ", thim.GetReplaceIn () 
    print "GetInValue () = ",   thim.GetInValue () 
    print "GetReplaceOut () = ",thim.GetReplaceOut () 
    print "GetOutValue () = ",  thim.GetOutValue () 
    print "GetUpperThreshold () = ",    thim.GetUpperThreshold () 
    print "GetLowerThreshold () = ",    thim.GetLowerThreshold ()
    print "GetOutputScalarType () = ",  thim.GetOutputScalarType ()

#--------------- End Display Functions -----------------------------------------

**Please let me know how I could threshold an image (vtkImageData) in python 
and get a valid output.

Thanks,
Sarju




More information about the vtkusers mailing list