How to store the underlying voxel values into a data structure after reading a nrrd image?

Hello everyone,

I am a new ITK user. I have to read a .nrrd file into C++ and then calculate the gradient of the values using C++. I was able to read the .nrrd file based on the following code:

#include
#include
#include
#include

#include <itkImage.h>
#include <itkExceptionObject.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkMetaDataObject.h>
#include <itkNrrdImageIO.h>

int main(int argc, char* argv[])
{
typedef itk::Vector<signed short,2> VectorType;
typedef itk::Image<VectorType,3> DiffusionImageType;
typedef DiffusionImageType::Pointer DiffusionImagePointer;

typedef itk::ImageFileReader FileReaderType;
FileReaderType::Pointer reader = FileReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
DiffusionImageType::Pointer image = reader->GetOutput();

return 0;
}

However, I can not find a way to extract the information and store it into a 3D matrix for further processing.

Any help would be appreciated.

Thanks,
Keyur Shah

1 Like

Hello @keyurs19, and welcome into the ITK community!

As you are new to ITK, I strongly recommend that you take a quick look at the ITK Software Guide. This will give you an insight about how processing is done.

Usually for basic processing like gradient, ITK use the concept of filter to which you connect your image to get the expected output, and you combine further filters to do a pipeline. Thus you don’t need to manipulate the buffer of pixels values.
See this example for instance.

HTH,

Edit: Nonetheless, should you access the pixel values, there are multiple ways, by pointer to buffer (GetBufferPointer static method), by direct access (GetPixel method), or by using image iterators (refer to software guide)

2 Likes

Hello @tim-evain,

Thank you for your prompt reply. For my application, I have to compute the gradient in each x,y and z directions. Would you know if there’s a specific way to do that using the concept of filter in ITK?

I also tried exploring the GetPixel method as per your suggestion. I tried accessing a specific pixel and tried to display its intensity, but was not really able to see value in the output window. Here’s the code that I used:

#include “itkImage.h”
#include “itkImageFileReader.h”

int main( int argc, char* argv[] )
{
if( argc != 2 )
{
std::cerr << "Usage: "<< std::endl;
std::cerr << argv[0];
std::cerr << " ";
std::cerr << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 3;

using PixelType = unsigned char;
    using ImageType = itk::Image< PixelType, Dimension >;

using ReaderType = itk::ImageFileReader< ImageType >;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
reader->Update();

ImageType::Pointer image = reader->GetOutput();
//std::cout<<image<<std::endl;

const ImageType::IndexType pixelIndex = {{27,29,37}};
ImageType::PixelType pixelValue = image->GetPixel( pixelIndex );
std::cout<<"Pixel Value =";
std::cout<<pixelValue<<std::endl;

return EXIT_SUCCESS;

}

Thanks,
Keyur Shah

Try using std::cout<<static_cast<unsigned int>(pixelValue)<<std::endl;. This will ensure you get the numeric value, not a character.

Take a look at this example:
https://itk.org/ITKExamples/src/Filtering/ImageGradient/ComputeGradientMagnitude/Documentation.html

And this list of classes regarding gradients:
https://itk.org/Doxygen/html/group__GradientFilters.html