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

**URL:** https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195
**Category:** Beginner Questions
**Created:** [August 28, 2019, 7:41pm UTC](https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195 "2019-08-28T19:41:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![keyurs19](https://discourse.itk.org/user_avatar/discourse.itk.org/keyurs19/32/1344_2.png) [@keyurs19](https://discourse.itk.org/u/keyurs19)
#### Post date: [August 28, 2019, 7:41pm UTC](https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195/1 "2019-08-28T19:41:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)
#### Post date: [August 29, 2019, 9:33am UTC](https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195/2 "2019-08-29T09:33:12Z")

</div>

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](https://itk.org/ItkSoftwareGuide.pdf). 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](https://itk.org/Doxygen/html/SphinxExamples_2src_2Filtering_2ImageGradient_2GradientOfVectorImage_2Code_8cxx-example.html#_a9) 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)

---

<div class="post-metadata">

### Author: ![keyurs19](https://discourse.itk.org/user_avatar/discourse.itk.org/keyurs19/32/1344_2.png) [@keyurs19](https://discourse.itk.org/u/keyurs19)
#### Post date: [August 29, 2019, 7:45pm UTC](https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195/3 "2019-08-29T19:45:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [August 29, 2019, 8:05pm UTC](https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195/4 "2019-08-29T20:05:07Z")

</div>

> [@keyurs19](#):
>
> std::cout\<\<pixelValue\<\<std::endl;

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

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [August 29, 2019, 8:07pm UTC](https://discourse.itk.org/t/how-to-store-the-underlying-voxel-values-into-a-data-structure-after-reading-a-nrrd-image/2195/5 "2019-08-29T20:07:41Z")

</div>

Take a look at this example:  
[https://itk.org/ITKExamples/src/Filtering/ImageGradient/ComputeGradientMagnitude/Documentation.html](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](https://itk.org/Doxygen/html/group__GradientFilters.html)
