# Misaligned intensity volume and ground truth mask

**URL:** https://discourse.itk.org/t/misaligned-intensity-volume-and-ground-truth-mask/7245
**Category:** Algorithms
**Tags:** python, simpleitk
**Created:** [October 2, 2024, 4:28pm UTC](https://discourse.itk.org/t/misaligned-intensity-volume-and-ground-truth-mask/7245 "2024-10-02T16:28:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vojtech\_Benda](https://discourse.itk.org/user_avatar/discourse.itk.org/vojtech_benda/32/3623_2.png) [@Vojtech\_Benda](https://discourse.itk.org/u/Vojtech_Benda)
#### Post date: [October 2, 2024, 4:28pm UTC](https://discourse.itk.org/t/misaligned-intensity-volume-and-ground-truth-mask/7245/1 "2024-10-02T16:28:23Z")

</div>

I’m trying to extract volume sizes of a liver and tumors from their ground truth volume masks. I’m using the LabelIntensityStatisticsImageFilter, but for one of the inputs it throws me an error:

```auto
RuntimeError: Exception thrown in SimpleITK LabelIntensityStatisticsImageFilter_Execute: /tmp/SimpleITK-build/ITK-prefix/include/ITK-5.4/itkImageToImageFilter.hxx:215:
ITK ERROR: LabelImageToStatisticsLabelMapFilter(0x5632f36abe80): Inputs do not occupy the sam	e physical space! 
	InputImage Origin: [-1.0000000e+00, -1.0000000e+00, 1.0000000e+00], InputImage_1 Origin: [-2.4910001e+02, 2.4902318e+02, -6.5100000e+02]
		Tolerance: 1.0000000e-06
	InputImage Spacing: [1.0000000e+00, 1.0000000e+00, 1.0000000e+00], InputImage_1 Spacing: [9.7656202e-01, 9.7656202e-01, 2.5000000e+00]
		Tolerance: 1.0000000e-06
	InputImage Direction: -1.0000000e+00 0.0000000e+00 0.0000000e+00
	0.0000000e+00 -1.0000000e+00 0.0000000e+00
	0.0000000e+00 0.0000000e+00 1.0000000e+00
	, InputImage_1 Direction: 1.0000000e+00 0.0000000e+00 0.0000000e+00
	0.0000000e+00 -1.0000000e+00 0.0000000e+00
0.0000000e+00 0.0000000e+00 1.0000000e+00

	Tolerance: 1.0000000e-06

```

Viewing the inputs confirms that they do not at all occupy the same physical space (see image below), caused by non matching direction, spacing and origin.  
 ![wrong_space](https://discourse.itk.org/uploads/default/original/2X/b/be3bdb3e9f3ee490ac052638c90941915bb7f72f.png)

I assumed simply resampling the ground truth volume using the intensity volume as reference image would align them (example code below), but that didn’t work.

```auto
vol = sitk.ReadImage(vol_path)
gt = sitk.ReadImage(gt_path)
gt_res = sitk.Resample(gt, vol, interpolator=sitk.sitkNearestNeighbor, outputPixelType=gt.GetPixelID())

```

The resampled volume’s origin, spacing and direction is correct, but it is empty - all voxels have value 0. The same result happens when using any of Slicer’s resample filters. Am I missing something or is this approach straight up wrong?

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [October 2, 2024, 5:44pm UTC](https://discourse.itk.org/t/misaligned-intensity-volume-and-ground-truth-mask/7245/2 "2024-10-02T17:44:06Z")

</div>

No, resampling isn’t going to align them. Is a matter of the label image losing the origin, spacing and direction from original image? If so, you could copy them over using the Get/Set methods for Origin, Direction and Spacing.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 2, 2024, 6:30pm UTC](https://discourse.itk.org/t/misaligned-intensity-volume-and-ground-truth-mask/7245/3 "2024-10-02T18:30:08Z")

</div>

Hello @Vojtech_Benda,

Per @dchen’s advice, copy the information from the volume to the segmentation:

```auto
gt.CopyInformation(vol)

```

---

<div class="post-metadata">

### Author: ![Vojtech\_Benda](https://discourse.itk.org/user_avatar/discourse.itk.org/vojtech_benda/32/3623_2.png) [@Vojtech\_Benda](https://discourse.itk.org/u/Vojtech_Benda)
#### Post date: [October 2, 2024, 6:41pm UTC](https://discourse.itk.org/t/misaligned-intensity-volume-and-ground-truth-mask/7245/4 "2024-10-02T18:41:32Z")

</div>

Many thanks @dchen and @zivy. That fixed the misalignment. It also fixed the squished appearance of the ground truth mask.

 ![good_space](https://discourse.itk.org/uploads/default/original/2X/7/7460721cbbd9e05c6419ef54610ceea839f13105.png)

Didn’t expect it to be that easy. Thanks a lot 🙂
