Problem while copying metadata of a dicom image

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?

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?

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);

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 =;
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 =;
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.

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