# Problem while copying metadata of a dicom image

**URL:** https://discourse.itk.org/t/problem-while-copying-metadata-of-a-dicom-image/2978
**Category:** Engineering
**Created:** [April 21, 2020, 2:41am UTC](https://discourse.itk.org/t/problem-while-copying-metadata-of-a-dicom-image/2978 "2020-04-21T02:41:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)
#### Post date: [April 21, 2020, 2:41am UTC](https://discourse.itk.org/t/problem-while-copying-metadata-of-a-dicom-image/2978/1 "2020-04-21T02:41:57Z")

</div>

Hi Guys, Let me give you some context of my problem:

I’m doing a registration between two images, MovingVolume and FixedVolume and I’m going to do some processing to generate two final images that are related with the MovingVolume, so I want to create two images with the metadata of the MovingVolume (m\_Labeled\_Image and registeredImage). So I’m trying this:

```
std::vector<std::string> metadata = MovingVolume.GetMetaDataKeys();
for (int i = 0; i < metadata.size(); i++)
   m_Labeled_image.SetMetaData(metadata[i], MovingVolume.GetMetaData(metadata[i]));

```

For the m\_Labeled\_image the copy is OK, but when I try to do the same for the registeredImage, which is the result of the registration, I got an itk Exception saying that the key does not exist.

So, what am I doing wrong?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 21, 2020, 2:42pm UTC](https://discourse.itk.org/t/problem-while-copying-metadata-of-a-dicom-image/2978/2 "2020-04-21T14:42:02Z")

</div>

Your description sounds fine. Can you share the code fragment declaring `registeredImage` and `m_Labeled_image`, as well as code for setting the metadata on both?

---

<div class="post-metadata">

### Author: ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)
#### Post date: [April 21, 2020, 4:36pm UTC](https://discourse.itk.org/t/problem-while-copying-metadata-of-a-dicom-image/2978/3 "2020-04-21T16:36:58Z")

</div>

Here is the code:

```
sitk::Image Moving_Image = sitk::ReadImage(m_moving_image_path, sitk::sitkFloat32);

//Works Fine
std::vector<std::string> metadata = Moving_Image.GetMetaDataKeys();
for (int i = 0; i < metadata.size(); i++)
    m_Labeled_image.SetMetaData(metadata[i], Moving_Image.GetMetaData(metadata[i]));

m_Labeled_image = sitk::Image(Moving_Image.GetSize(), sitk::sitkFloat32);
m_Labeled_image.SetOrigin(Moving_Image.GetOrigin());
m_Labeled_image.SetDirection(Moving_Image.GetDirection());

MNI_Atlas = sitk::Normalize(MNI_Atlas);
Moving_Image = sitk::Normalize(Moving_Image);

UtilsRegistration utils;
//Printing some information about the images
utils.printSpacing(MNI_Atlas, "Fixed");
utils.printDimension(MNI_Atlas, "Fixed");
utils.printImageType(MNI_Atlas, "Fixed");

std::cout << std::endl;

utils.printSpacing(Moving_Image, "Moving");
utils.printDimension(Moving_Image, "Moving");
utils.printImageType(Moving_Image, "Moving");

std::cout << std::endl << "Doing Affine Registration" << std::endl << std::endl;
AffineRegistration affineReg(MNI_Atlas, Moving_Image);
sitk::Transform affine_trans = affineReg.run();
sitk::Image transformed_image_affine = sitk::Resample(Moving_Image, MNI_Atlas, affine_trans, sitk::sitkLinear, 0, Moving_Image.GetPixelID());

std::cout << std::endl << "Doing Bspline Registration" << std::endl << std::endl;
BsplineRegistration bsplineReg(MNI_Atlas, transformed_image_affine);
sitk::Transform bspline_trans = bsplineReg.run();
sitk::Image registered_image = sitk::Resample(transformed_image_affine, bspline_trans, sitk::sitkLinear, 0, sitk::sitkUInt16);

for (int i = 0; i < metadata.size(); i++)
{ //Here is the error
        std::string metadata_value = Moving_Image.GetMetaData(metadata[i]);
        registered_image.SetMetaData(metadata[i], metadata_value );
}

```

That Utils class is just a class that I created for organizing some parts of the code.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 21, 2020, 4:42pm UTC](https://discourse.itk.org/t/problem-while-copying-metadata-of-a-dicom-image/2978/4 "2020-04-21T16:42:25Z")

</div>

> [@Felippe\_Trigueiro\_An](#):
>
> Moving\_Image = sitk::Normalize(Moving\_Image);

This step probably erases the metadata from the image, as filtering discards the metadata by default.
