# How to preserve the direction cosine matrix

**URL:** <https://discourse.slicer.org/t/how-to-preserve-the-direction-cosine-matrix/6968>\
**Category:** Development\
**Created:** [May 30, 2019, 6:06am UTC](https://discourse.slicer.org/t/how-to-preserve-the-direction-cosine-matrix/6968 "2019-05-30T06:06:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpgao](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wpgao](https://discourse.slicer.org/u/wpgao)\
**Post date:** [May 30, 2019, 6:06am UTC](https://discourse.slicer.org/t/how-to-preserve-the-direction-cosine-matrix/6968/1 "2019-05-30T06:06:00Z")

</div>

Hi Guys,

Recently, I had write a batch to resample a series of MR volumes. However, there is one problem that the direction cosine matrix of the output volume is not the same as that of the input volume.  
Thanks！

```auto
reader=sitk.ImageFileReader()
writer=sitk.ImageFileWriter()
reader.SetFileName(str(fi_d))
inputVolume=reader.Execute()
reader.ReadImageInformation()
if inputVolume is not None:
 			resample=sitk.ResampleImageFilter()
 			resample.SetInterpolator(sitk.sitkLinear)
 			resample.SetTransform(sitk.Transform(3,sitk.sitkIdentity))
 			resample.SetOutputDirection(reader.GetDirection())
 			print(reader.GetDirection())
 			resample.SetOutputOrigin(reader.GetOrigin())
 			out_spacing=[1.0,1.0,1.0]
 			resample.SetOutputSpacing(out_spacing)
 			original_spacing=reader.GetSpacing()
 			original_size=reader.GetSize()
 			out_size=[int(np.round(original_size[0]*(original_spacing[0]/out_spacing[0]))),int(np.round(original_size[1]*(original_spacing[1]/out_spacing[1]))),int(np.round(original_size[2]*(original_spacing[2]/out_spacing[2])))]
 			resample.SetSize(out_size)
 			resample.SetDefaultPixelValue(0.0)
 			out=resample.Execute(inputVolume)
 			prefix="%s_iso.nii"%fileName
 			resampleVolumeFilePath=os.path.join(self.saveVolumeDir,prefix)
 			writer.SetFileName(str(resampleVolumeFilePath))
 			writer.Execute(out)

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [May 30, 2019, 3:32pm UTC](https://discourse.slicer.org/t/how-to-preserve-the-direction-cosine-matrix/6968/2 "2019-05-30T15:32:41Z")

</div>

This looks like a good question for [the SimpleITK](https://discourse.itk.org/search?q=simpleitk) forum. From a quick look it seems you are setting the output direction to match the input, but maybe something else if going on.

---

<div class="post-metadata">

**Author:** ![wpgao](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wpgao](https://discourse.slicer.org/u/wpgao)\
**Post date:** [May 31, 2019, 2:07am UTC](https://discourse.slicer.org/t/how-to-preserve-the-direction-cosine-matrix/6968/3 "2019-05-31T02:07:50Z")

</div>

Hi Pieper,

Thanks for your advice!
