Transfer from VTK to SimpleITK

I come back here because I am working blind (I cannot compile SimpleITK, this is drive me crazy, because I am running out of time). And until I solve that issue (working on that by days !!! unbelievable) I am trying to write code that import data from VTK into SimpleITK, and I wrote:

sitk::ImportImageFilter importer;
importer.SetSpacing(m_pDICOMReader->GetOutput()->GetSpacing());
importer.SetOrigin(m_pDICOMReader->GetOutput()->GetOrigin());
importer.SetSize(m_pDICOMReader->GetOutput()->GetScalarSize());
sitk::Image img = importer.Execute();

it is ok ? (m_pDICOMReader is vtkDICOMReader). I really need this code functional.

Thank you for your time !

You are missing the main thing, importer.SetImportPointer(). I am not sure whether vtkDICOMReader has direction, but if it does you might want to set that too.

2 Likes

The Doxygen for the SimpleITK ImportImageFilter is here:
https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ImportImageFilter.html

While ITK’s class used SetImportPointer the SimpleITK class used SetBuferAs... where ... is the pixel component type e.g. SetBufferAsFloat for Images of float.

2 Likes

vtkDICOMReader does not have direction feature … but in this case I should setup myself direction ?

Direction is identity by default, so you don’t need to set it up.

1 Like

Thank you Dzenan for your time, you are kind !

Please have patience with me. I come back because I have tried to use this example:

to import data from vtkImageData into sitk::Image through sitk::ImportImageFilter.

Here is the code:

sitk::ImportImageFilter importer;

int* nDim = m_pDICOMReader->GetOutput()->GetDimensions();
TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]);
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);
int nSize = m_pDICOMReader->GetOutput()->GetScalarSize();
std::vector<unsigned int> size;
size.push_back(nSize);
importer.SetSize(size);
importer.SetBufferAsUInt8(in);

try
{
	sitk::Image img = importer.Execute();
}
catch (sitk::GenericException& error)
{
	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_SETMESSAGESTRING, 0, (LPARAM)error.GetDescription());
}

but I got the following error:

sitk::ERROR: The length of size is invalid! Only image of dimension 2 or 3 are supported

What I have done wrong ? I really need this conversion in order to keep working ! Thank you for any help !

The error indicates that size is invalid. So what is the value of size in your code?

nSize is 2 in my case.

Hello @flaviu2,

In your code the parameter to SetSize should be the contents of nDim. In ITK/SimpleITK the size of an image is the number of pixels/voxels per dimension.

2 Likes

Great ! Seem to go, now I have to try the outcome, for the moment I have no error !!! I hope is ok … Now I stop the work for 24 hours, but I come back here with feedback ! @zivy & @blowekamp & @dzenanz Thank you !!!

Here is my new code that seem to go without errors:

sitk::ImportImageFilter importer;

int* nDim = m_pDICOMReader->GetOutput()->GetDimensions();
TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]);
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);

try
{
	sitk::Image img = importer.Execute();
}
catch (sitk::GenericException& error)
{
	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_SETMESSAGESTRING, 0, (LPARAM)error.GetDescription());
}

delete[] in;
1 Like