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 <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();
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.

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.


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)


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;
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] );

ImageType::Pointer image = reader->GetOutput();

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



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:

And this list of classes regarding gradients:

1 Like