Simple test for SimpleITK to VTK conversion

I have tried a simple test to check my conversion from SimpleITK to VTK:

// visualize vtkVolume color
... /// convert VTK ro SimpleITK
sitk::Image img = importer.Execute();
////////////////////////////////////////////
img = sitk::GrayscaleErode(img, 10); // <-- simple test
////////////////////////////////////////////
...// Convert from SimpleITK to VTK and visualize color volume
pImageImport->SetImportVoidPointer((void*)img.GetBufferAsUInt8());
... and so on ...
m_pDICOMReader->GetOutput()->DeepCopy(pImageImport->GetOutput());
m_pDICOMReader->Update();
m_pImageExtractor->SetInputConnection(m_pDICOMReader->GetOutputPort());
m_pImageExtractor->SetComponents(0);
m_pImageExtractor->Update();

// visualize vtkVolume color

The fact is that the volume after this conversion is exactly the same as before conversion … can you tell me why why this SimpleITK to VTK conversion is not working correctly ?

Plus, this line of code:

img = sitk::GrayscaleErode(img, 10);

takes more than 3 hours … on Debug mode (on Release mode I didn’t tried)

Some filters are 100 times faster in release mode.

Your example code does not make it clear what are you trying to do. Can you share a self-contained, runnable example?

Yes, Dzenan, you had right, the release mode is much more faster.

Is pretty hard to reproduce right here, but I will try to give here more details:

My goal is to transfer the VTK data to SimpleITK, apply some filters, and then transfer data back to VTK in order to visualize the data.

I have a vtkDICOMReader to read input data (CT dicom), and a vtkVolume to visualize data, and the result is:

image (see window level)

Now, I intend to import VTK data to SimpleITK , my purpose is to do some segmentation:

////  **importing data from VTK to SimpleITK**  ///////////////////////////////////
int* nDim =  **m_pDICOMReader** ->GetOutput()->GetDimensions();
TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]); //  **result: >>>>512|512|102**
uint8_t* in = new uint8_t[nDim[0] * nDim[1] * nDim[2]];
double* dSpacing =  **m_pDICOMReader** ->GetOutput()->GetSpacing();
std::vector<double> spacing;
spacing.push_back(dSpacing[0]);
spacing.push_back(dSpacing[1]);
spacing.push_back(dSpacing[2]);
importer.SetSpacing(spacing);
double* dOrigin =  **m_pDICOMReader** ->GetOutput()->GetOrigin();
std::vector<double> origin;
origin.push_back(dOrigin[0]);
origin.push_back(dOrigin[1]);
origin.push_back(dOrigin[2]);
importer.SetOrigin(origin);
std::vector<unsigned int> size;
size.push_back((unsigned int)nDim[0]);
size.push_back((unsigned int)nDim[1]);
size.push_back((unsigned int)nDim[2]);
importer.SetSize(size);
importer.SetBufferAsUInt8(in);
sitk::Image  **img**  = importer.Execute();
///////////////////////////////////////////////////////////////////////////

to check if the conversion is ok, I apply no SimpleITK filter for now …

////  now I want to import data back from SimpleITK to VTK  ///////////////////
std::vector<double> spacing =  img.GetSpacing();
img  = sitk::Cast(img, sitk::PixelIDValueEnum::sitkUInt8);
pImageImport->SetImportVoidPointer((void*)img.GetBufferAsUInt8());
pImageImport->SetDataScalarTypeToUnsignedChar();
pImageImport->SetDataExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, size[2] - 1);
pImageImport->SetWholeExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, size[2] - 1);
pImageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
pImageImport->Update();
nDim = pImageImport->GetOutput()->GetDimensions();
TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]); // result >>>>512|512|102, which is correct
m_pDICOMReader->GetOutput()->DeepCopy(pImageImport->GetOutput());
m_pDICOMReader->Update();
/////////////////////////////////////////////////////////////////////////////

But the result is a diffuse cube:
image (see window level)

Why this conversion is not alright ? How can I check what part of conversion is not working correctly ?

The problem is that you are not importing the image you read. You are importing a block of uninitialized memory which you allocate by uint8_t* in = new uint8_t[nDim[0] * nDim[1] * nDim[2]];.

in needs to be defined as short* in = m_pDICOMReader->GetOutput().GetBufferAsInt16(); or something similar.

1 Like