Access image pixel values in LOADABLE

Hello everyone, everything good? Could someone help me with a question.

In the CLI I was able to get the pixels of the image using the GetBufferPointer () method.
Here’s the example:

	typedef itk::Image<float, 3> ImageType;
typedef itk::ImageFileReader< ImageType  >  ReaderType;

typename ReaderType::Pointer reader;
typename ImageType::Pointer image;

reader = ReaderType::New();	
reader->SetFileName( inputVolume.c_str() );
reader->Update();
image = reader->GetOutput();

*float dataPixel = image->GetBufferPointer();

Now in LOADABLE I was looking for some method of the vtkImageData class that could return the pixel values similar to the object used above itk :: Image. Here is the code I am using:

vtkMRMLVolumeNode* volumeNode = vtkMRMLVolumeNode::SafeDownCast(d->InputVolumeComboBox->currentNode());
vtkImageData* image = volumeNode->GetImageData();

float *dataPixel= static_cast<float *>(image->GetScalarPointer(0,0,0));

I can get the dimension and other information from the vtkImageData object, but I am not able to get the values of the pixels, using the GetScalarPointer () function returns empty.

Would anyone have any suggestions or tell me the correct way to do this ??

What do you mean by empty? Are you sure your image is float type?
What you do seems mostly correct. It is safer to get scalar for a specific extent by using GetScalarPointerForExtent() or get the full buffer using GetScalarPointer(). Most often origin is (0,0,0) and extent starts from (0,0,0), so GetScalarPointer(0,0,0) should work as well.

In general, it is better to avoid pixel-wise access and use VTK filters for any processing. What would you like to achieve?

Andras, thanks for the reply.

Let me explain to you what I was doing in CLI, I was working with image processing (.nrrd) on GPUs, using OPENCL. I upload an image into an object with type of pixel float “itk :: Image <float, 3>”.

In order to send the image to GPU, I get a multidimensional float vector containing all pixel values ​​using the “image-> GetBufferPointer ()”. Just to I can send “image” (float vector) via Opencl to a GPU or CPU in my machine, the GPU will process the image and I get a result also in a one-dimensional float vector.

With this I import this vector using an itk class ImportImageFilter, creating an itk image object of type float, and finally I use the class CastImageFilter to convert the image to pixel of type short.

I was able to implement in CLI and compare different types of filters, comparing their traditional version with the version using Opencl (as a result I have a gain of up to 200x faster depending on the complexity of the algorithm, image size … etcc).

But the CLI has its limitations, right? So I started to venture into LOADABLE. But I’m having difficulties in this matter, I need to get the values of the pixels in float (the image is of short type in the 3D Slicer display), so after the result of the execution in the GPU I will have to convert to short so to display the result the image.

Can did you understand more or less? :stuck_out_tongue:

Thanks again for your attention.

I would recommend to implement processing in CLI (as you have done it already), and if needed add more sophisticated user interface in a Python (or maybe C++) loadable module.

CLIs are nice because you can easily run them without GUI, in batch mode and you can easily run them from loadable modules, too.