# Copying a volume's scalar type

**URL:** <https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886>\
**Category:** Development\
**Created:** [August 16, 2017, 11:50pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886 "2017-08-16T23:50:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 16, 2017, 11:50pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/1 "2017-08-16T23:50:02Z")

</div>

How do I copy just the image information from a Scalar Volume Node “`originalNode`” to a new node initialized by `newnode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode')`?

The problem with `newnode.Copy(originalNode)` is that it also copies the Scalar Type, which is too finicky to change, and it seems to round down the `Image Origin` and has an extra decimal place in `Image Spacing`. This causes the sequence to only store as a “Medical Reality Bundle” when I want it to save as a `.seq.nrrd` file.

I [committed a temporary fix where it still registers with the fixed and moving volume being the same](https://github.com/moselhy/SlicerSequenceRegistration/commit/8b053feedb1b0f7b6a11fa1b10451ecdaf71094c), but it is a little time consuming.

---

<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:** [August 17, 2017, 2:15am UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/2 "2017-08-17T02:15:43Z")

</div>

Image geometry (origin, spacing, axis directions) are stored in the IJK to RAS (voxel to physical space transformation) matrix. You can copy that information like this:

```
ijkToRas = vtk.vtkMatrix4x4()
sourceVolume.GetIJKToRASMatrix(ijkToRas)
targetVolume.SetIJKToRASMatrix(ijkToRas)
```

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 17, 2017, 6:03pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/3 "2017-08-17T18:03:13Z")

</div>

Thanks, then what do I do to copy the signal intensities over without copying the scalar type?

---

<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:** [August 17, 2017, 6:47pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/4 "2017-08-17T18:47:57Z")

</div>

The problem seems to be that Elastix does not set IJKToRAS matrix of the resampled moving volume accurately. We should confirm this and report to Elastix developers, but until then we can apply the workaround of overwriting IJKToRAS of all the registered volumes to be exactly the same as the fixed volume’s IJKToRAS.

Does Elastix change the scalar type as well? If yes, then the simplest solution is probably to use vtkImageCast filter to set it to the same as the fixed volume’s scalar type.

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 18, 2017, 7:19pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/5 "2017-08-18T19:19:12Z")

</div>

Yes, Elastix changes the scalar type.

> [@lassoan](#):
>
> the simplest solution is probably to use vtkImageCast filter to set it to the same as the fixed volume’s scalar type

Would this be any faster than registering the fixed volume with itself?

---

<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:** [August 18, 2017, 7:56pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/6 "2017-08-18T19:56:26Z")

</div>

Yes, copying and casting a volume is much faster. Do not register a volume to itself, as the result may not be always identical.

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 18, 2017, 8:20pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/7 "2017-08-18T20:20:59Z")

</div>

Thank you, how do I copy and cast the volume `inputvol` with scalar type `int` to `outputvol` with scalar type `short`?

---

<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:** [August 18, 2017, 9:02pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/8 "2017-08-18T21:02:55Z")

</div>

VolNode-\>GetImageData() =\> cast filter =\> VolNode-\>SetAndObserveImageData()

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 21, 2017, 3:17pm UTC](https://discourse.slicer.org/t/copying-a-volumes-scalar-type/886/9 "2017-08-21T15:17:21Z")

</div>

Thank you, here is the full code for anyone wanting to change the scalar volume type of a volume node `volumeNode`:

```
imageCast = vtk.vtkImageCast()
imageCast.SetInputData(volumeNode.GetImageData())
# This could be substituted by SetOutputScalarTypeToX where X is the desired type
imageCast.SetOutputScalarTypeToShort()
imageCast.Update()
volumeNode.SetAndObserveImageData(imageCast.GetOutput())
```
