# Getting file name from a DICOM slice

**URL:** https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364
**Category:** Development
**Created:** [July 3, 2020, 8:00pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364 "2020-07-03T20:00:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)
#### Post date: [July 3, 2020, 8:00pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/1 "2020-07-03T20:00:36Z")

</div>

I would like to get a filename/path from a particular slice of a dicom image. For example I have loaded a DICOM file with several hundred slices and I would like to get the filename/path of a particular slice.

I have went through this link:

> [@Identify a DICOM Instance file name in 3D DICOM volume](https://discourse.slicer.org/t/identify-a-dicom-instance-file-name-in-3d-dicom-volume/11968):
>
> Hi all, How can I identify a the file name of a single slice in a loaded 3D DICOM volume? I have loaded a DICOM directory with several hundred slices, but after I have identified a single slice of interest, I would like to know which file name it is. Thanks

And was able to develop a python script that allowed me to get a filename of a particular slice, but now I want to do it in C++ as well. However, many of the functionalities and modules available in python are not available in C++.

Currently, I am able to get the VTKMRMLvolume node, but was wondering where I can go from there.

Any help is appreciated!

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [July 3, 2020, 9:43pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/2 "2020-07-03T21:43:24Z")

</div>

All Slicer features are available in both C++ and Python. If you have difficulty figuring out the syntax for any particular method then let us know.

---

<div class="post-metadata">

### Author: ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)
#### Post date: [July 4, 2020, 12:13am UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/3 "2020-07-04T00:13:15Z")

</div>

Thank you for the reply Andras!

There are two methods in particular that I would like to know:

1. For a given DICOM file, I would like to know which particular slice I am on (the slice number), I know that we would get this information from the DataProbe, and in python we would do  
`infoWidget = slicer.modules.DataProbeInstance.infoWidget`  
`infoWidget.layerIJKs['B']`  
to get the voxel coordinate, what would be the syntax to achieve this in C++?

2. If I currently have the instanceUID, how would I get the filename from it? In python I would do:  
`filename=slicer.dicomDatabase.fileForInstance(instUids[k])`  
where k is the voxel coordinate, but what would the syntax be in C++?

Thank you in advance and I really appreciate your help!

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [July 4, 2020, 3:28am UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/4 "2020-07-04T03:28:02Z")

</div>

> [@DavidCai1246](#):
>
> For a given DICOM file, I would like to know which particular slice I am on (the slice number), I know that we would get this information from the DataProbe, and in python we would do

There are many ways for getting position from a view. If you want to know the current position of the mouse pointer, you can get it from the crosshair node (see [example](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_current_mouse_coordinates_in_a_slice_view)). If you want the user to mark a position then I would recommend to make the user place a markup point. If you just want the user to select a slice then you can get slice position and orientation from the slice node.

> [@DavidCai1246](#):
>
> If I currently have the instanceUID, how would I get the filename from it? In python I would do:  
> `filename=slicer.dicomDatabase.fileForInstance(instUids[k])`

You can get the DICOM database by calling

```
qSlicerApplication::application()->dicomDatabase()

```

---

<div class="post-metadata">

### Author: ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)
#### Post date: [July 6, 2020, 6:22pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/5 "2020-07-06T18:22:05Z")

</div>

> [@lassoan](#):
>
> If you just want the user to select a slice then you can get slice position and orientation from the slice node.

Thanks for the reply!

Could we also access the slice position from DataProbe?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [July 6, 2020, 6:35pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/6 "2020-07-06T18:35:16Z")

</div>

Data Probe module has no a logic class therefore there is no clean way of getting slice position from it. But since it is so simple to get slice index and position information from MRML nodes, there is no need to use the Data Probe module for this.

---

<div class="post-metadata">

### Author: ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)
#### Post date: [July 6, 2020, 6:50pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/8 "2020-07-06T18:50:12Z")

</div>

Sounds good I will look into this!

> [@lassoan](#):
>
> If you just want the user to select a slice then you can get slice position and orientation from the slice node.

I’m a bit new to slicer so I was wondering by slice node do you mean the volume node? Because I cant seem to find anything regarding a slice node. Also what would the syntax be to pull a slice index from a slice node?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [July 6, 2020, 7:04pm UTC](https://discourse.slicer.org/t/getting-file-name-from-a-dicom-slice/12364/9 "2020-07-06T19:04:49Z")

</div>

There is actually a slice node ([vtkMRMLSliceNode](http://apidocs.slicer.org/master/classvtkMRMLSliceNode.html)) for each slice view, which stores slice position and orientation in its `SliceToRAS` matrix. Slice position in RAS coordinate system is stored in the last column of the matrix.
