How to get pixel value and coordinate from nrrd file

So I make nrrd file. In this file has Annotation Data using 3D Slicer.

I want to get pixel coordinate and pixel value from this file.

I modified the code with reference to the source of this link, but I did not get any results.

python dicom – getting pixel value from dicom file

I really couldn’t find any information so any helpful resources would be appreciated as well. Thank you.

These examples in the script repository should help:

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify_voxels_in_a_volume

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_volume_voxel_coordinates_from_markup_fiducial_RAS_coordinates

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_markup_fiducial_RAS_coordinates_from_volume_voxel_coordinates

Thanks reply.

This code need to use python interaction in 3D Slicer. Right??

You need to run the code in Slicer’s Python environment. You probably don’t need to show Slicer GUI - you can run your script from the command-line.

Oh I can see array!

And i want to know each array’s all information.

now we can see like this.
[[[0 0 0 …, 0 0 0]
[0 0 0 …, 0 0 0]
[0 0 0 …, 0 0 0]

i want to remove … this part and i want to print all arrya elements.

and i save result to txt file. but result is same.

Writing out 3D volume to text file is a bad idea (large file, slow to read/write, potential data loss due to limited precision), but here is a complete example just to show how easy it is to do:

# Get some image data
import SampleData
volumeNode=SampleData.SampleDataLogic().downloadMRHead()

# Get voxels as numpy array
volumeArray=slicer.util.arrayFromVolume(volumeNode)

# Write voxels to text file
import numpy as np
with file('c:/tmp/test.txt', 'w') as outfile:
  for data_slice in volumeArray:
    np.savetxt(outfile, data_slice, fmt='%-7.2f')

If you want to write in a different format then search for Python code examples on the web.