[vtkusers] How to do a grayscale open / close using vtkImageOpenClose3D?

James Carroll mrmaple at gmail.com
Thu Aug 25 15:55:39 EDT 2005


Hi,  I'm trying to port some C++ ITK to some Python VTK.  What I have
seems to work,
but I'm not sure I'm doing the right thing, and it's working too
quickly.  I'd expect it to take at least 5 seconds, but the opening
takes under a second.

I'm doing this because the ITK C++ example was taking over a minute
and a half, where an IDL version took about 5 seconds and a python /
numarray version took about 10 seconds.  I'd like to get a feel for
vtk's speed.

Where can I find a discussion on what SetOpenValue and SetCloseValue
really mean?  What values would do about the same thing as the
grayscale Opening in ITK?

Here's my python for vtk, and the C++ ITK that I'm trying to replicate. 

import vtk

import time
class SimpleTimer:
    def __init__(self):
        pass

    def start(self):
        self.startTime = time.clock()
    #

    def timeSoFarStr(self):
        totalTimeSec = time.clock() - self.startTime
        minutes = int(totalTimeSec / 60.0)
        seconds = int(totalTimeSec % 60.0)
        return "%0i:%02i" % (minutes, seconds)
    #
#

if __name__ == "__main__":

    tAll = SimpleTimer()
    tAll.start()

    tStep = SimpleTimer()
    tStep.start()

    # create a (unsigned char ?) stack  1024x1024x7
    quadric = vtk.vtkQuadric()
    quadric.SetCoefficients(.5,1,.2,0,.1,0,0,.2,0,0)
    vol = vtk.vtkSampleFunction()
    vol.SetSampleDimensions(1024,1024,7)
    vol.SetImplicitFunction(quadric)

    pipe = vol.GetOutput()

    print "Stack load (generate) took", tStep.timeSoFarStr()
    tStep.start()

    # Grayscale opening wxth 9x9x3 ball structuring element
    opener = vtk.vtkImageOpenClose3D()
    opener.SetInput(pipe)
    opener.SetOpenValue(128.0)
    opener.SetCloseValue(128.0)
    opener.SetKernelSize(9,9,3)
    opener.Update()

    pipe = opener.GetOutput()

    print "Grayscale opening took", tStep.timeSoFarStr()

    print "whole process took", tAll.timeSoFarStr()

The ITK c++ that I'm trying to emulate for the Opening:

	typedef itk::BinaryBallStructuringElement< PixelType, 3 >
StructuringElementType;
	typedef itk::GrayscaleMorphologicalOpeningImageFilter< ImageType, ImageType, 
		StructuringElementType > OpeningFilterType;
	OpeningFilterType::Pointer opening_filter = OpeningFilterType::New();

	StructuringElementType structuringElement;
	structuringElement.SetRadius(4);
	structuringElement.CreateStructuringElement();

	opening_filter->SetKernel( structuringElement );
	opening_filter->SetInput(median_filter->GetOutput());
	try 
	{
		opening_filter->Update();
	}

Thanks,
-Jim



More information about the vtkusers mailing list