Get the metadata of dicom without reading the image

(esme) #1

Dear all,

I would like to get the windows level and the windows width but without reading the image. I read in this forum that this is possible but i did not find any example without updating the reader. My code works well, but i want to save memory as my images are very big so i would prefer not to load the image as i am only inerested in the metadata. But if i do not update the reader i cannot get the metadata.

Thank you in advance

using PixelType = signed short;
constexpr unsigned int Dimension = 2;
using ImageType = itk::Image< PixelType, Dimension >;
using ReaderType = itk::ImageFileReader< ImageType >;
ReaderType::Pointer reader = ReaderType::New();
using ImageIOType = itk::GDCMImageIO;
ImageIOType::Pointer dicomIO = ImageIOType::New();

reader->SetFileName("cases/demo/export1.dcm");
reader->SetImageIO(dicomIO);

try
{

	reader->Update();

}
catch (itk::ExceptionObject &ex)
{
	std::cout << ex << std::endl;
	return;
}

// Now that the image has been read, we obtain the MetaDataDictionary from
// the ImageIO object using the \code{GetMetaDataDictionary()} method.
using DictionaryType = itk::MetaDataDictionary;
const  DictionaryType & dictionary = dicomIO->GetMetaDataDictionary();

using MetaDataStringType = itk::MetaDataObject< std::string >;
auto end = dictionary.End();



	std::string entryId = "0028|1050";
	auto tagItr = dictionary.Find(entryId);

	// If the entry is actually found in the Dictionary, then we can attempt to
	// convert it to a string entry by using a \code{dynamic\_cast}.
	if (tagItr != end)
	{
		MetaDataStringType::ConstPointer entryvalue =
			dynamic_cast<const MetaDataStringType *>(
				tagItr->second.GetPointer());

		if (entryvalue)
		{
			
			std::string tagvalue = entryvalue->GetMetaDataObjectValue();
			std::cout << "Windows level: (" << entryId << ") ";
			level = std::stod(tagvalue.c_str());
			std::cout << " is: " << level << std::endl;
		}
	}

	entryId = "0028|1051";
	tagItr = dictionary.Find(entryId);

	// If the entry is actually found in the Dictionary, then we can attempt to
	// convert it to a string entry by using a \code{dynamic\_cast}.
	if (tagItr != end)
	{
		MetaDataStringType::ConstPointer entryvalue =
			dynamic_cast<const MetaDataStringType *>(
				tagItr->second.GetPointer());

		if (entryvalue)
		{
			std::string tagvalue = entryvalue->GetMetaDataObjectValue();
			std::cout << "Windows width: " << entryId << ") ";
			windows = std::stod(tagvalue.c_str());
			std::cout << " is: " << windows << std::endl;
		}
	}
(Ziv Yaniv) #2

Use the ImageIO directly to get the image information without reading it (pure virtual method from ImageIOBase):

dicomIO->ReadImageInformation()
3 Likes
(esme) #3

Thank you very much !! It works !!

1 Like