[vtkusers] strange problem while creating a binary mask

Anja Ende anja.ende at googlemail.com
Mon Oct 30 12:46:30 EST 2006


Hi everyone,

I am having a strange problem with vtkImageExtrudeFilter output. The problem
is that it does not also happen all the time, so I am not sure what is going
on.

So, what I am trying to do is create a binary mask based on some user
selection.

The code for using the vtkImageExtrude function is as follows:

vtkLinearExtrusionFilter* extrude = vtkLinearExtrusionFilter::New();
extrude->SetInput(polydata);
extrude->SetScaleFactor(1);
extrude->SetExtrusionTypeToNormalExtrusion();
extrude->SetVector(0, 0, 1);

vtkPolyDataToImageStencil* dataToStencil = vtkPolyDataToImageStencil::New();
dataToStencil->SetInput(extrude->GetOutput());

vtkImageStencil* stencil = vtkImageStencil::New();
stencil->SetInput(m_viewer->GetOutput());
stencil->SetStencil(dataToStencil->GetOutput());
stencil->ReverseStencilOff();
stencil->SetBackgroundValue(0);

stencil->Update();

Now, to binarize the image, I am using the code from the vtk build (thanks
to Gabriel for pointing this out!) . The code can be found at:

/Hybrid/Testing/Cxx/TestImageStencilData.cxx<http://public.kitware.com/cgi-bin/viewcvs.cgi/*checkout*/Hybrid/Testing/Cxx/TestImageStencilData.cxx?root=VTK&content-type=text/plain>

I am putting the function here also:

The code is as follows:

static void GetStencilDataAsImageData(

  vtkImageStencilData * stencilData, vtkImageData *image )
{
  int extent[6];
  stencilData->GetExtent( extent );
  extent[5] = extent[4]; // Otherwise we cannot write it out as a PNG!
  int extent1[6] = {0,50,0,50,0,0};

  image->SetExtent(extent1);
  image->SetScalarTypeToUnsignedChar();
  image->SetNumberOfScalarComponents(3);
  image->AllocateScalars();

  // Fill image with zeroes
  for (int y=extent1[2]; y <= extent1[3]; y++)

    {
    unsigned char *ptr = (unsigned char *)(image->GetScalarPointer(
                              extent1[0], y, extent1[4] ));
    for (int x=extent1[0]; x <= extent1[1]; x++)
      {
      *ptr = 0;  ++ptr;

      *ptr = 0;  ++ptr;
      *ptr = 0;  ++ptr;
      }
    }

  vtkIdType increments[3];
  image->GetIncrements( increments );

  int iter = 0;
  for (int y=extent1[2]; y <= extent1[3]; y++, iter = 0)

    {
    int r1,r2;
    int moreSubExtents = 1;
    while( moreSubExtents )
      {
      moreSubExtents = stencilData->GetNextExtent(
        r1, r2, extent1[0], extent1[1], y, extent1[4], iter);


      // sanity check
      if (r1 <= r2 )
        {
        unsigned char *beginExtent =
          (unsigned char *)(image->GetScalarPointer( r1, y, extent1[4] ));
        unsigned char *endExtent   =

          (unsigned char *)(image->GetScalarPointer( r2, y, extent1[4] ));
        while (beginExtent <= endExtent)
          {
          *beginExtent     = (unsigned char)(255);
          *(beginExtent+1) = (unsigned char)(255);

          *(beginExtent+2) = (unsigned char)(255);
          beginExtent += increments[0];
          }
        }
      } // end for each extent tuple
    } // end for each scan line


The function is called as follows:

vtkImageData * data = vtkImageData::New();

GetStencilDataAsImageData(stencil->GetStencil(), data);

Now, the problem is that I am expecting a black background and white
foreground of the my selection region. I get this most of the time but
sometimes the selection is inverted!! That is my background is white
and foreground is black! I have no idea why this is happening. I am
guessing it could even be something with the vtkImageExtrude filter.

Has anyone else noticed this or know what might be going on here?

Thanks fo any help you might be able to give me.

Cheers,

Anja
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061030/277b4bde/attachment.htm>


More information about the vtkusers mailing list