[Insight-users] Re: MetadataDictionary

marquis2 at etu.unige.ch marquis2 at etu.unige.ch
Wed Feb 23 05:47:45 EST 2005


mathieu,

> Samuel,
> 
> 	Could you send us the c++ code you used that does not work. 

the whole of it is a bit long and complicated .

First if image is dicom I do:
typedef itk::GDCMImageIO ImageIOType;
gdcmImageIO = ImageIOType::New();

then here are my IO functions (i work mainly whit unsigned char images):

typedef itk::Image<unsigned char, 2> ImageType;
typedef itk::Image< short, 2 > ShortImageType;

ShortImageType::Pointer LungSegmentation::readImage(string inputFilename) {
	typedef itk::ImageFileReader< ShortImageType >  ReaderType;
	ReaderType::Pointer reader;
	reader = ReaderType::New();
	reader->SetFileName( inputFilename.c_str()  );
	if (gdcmImageIO != 0)
		reader->SetImageIO(gdcmImageIO);
	try {
		reader->Update();
	} catch(...) {
		cout << "exception while update\n";
		exit(0);
	}
	return reader->GetOutput();
}

ShortImageType::Pointer LungSegmentation::uchar2short(ImageType::Pointer arg) {
	typedef itk::CastImageFilter< ImageType , ShortImageType > CastFilterType;
	CastFilterType::Pointer caster = CastFilterType::New();
	caster->SetInput(arg);
	caster->Update();
	return caster->GetOutput();
}

ImageType::Pointer LungSegmentation::short2uchar(ShortImageType::Pointer arg) {
	typedef itk::CastImageFilter< ShortImageType , ImageType > CastFilterType;
	CastFilterType::Pointer caster = CastFilterType::New();
	caster->SetInput(arg);
	caster->Update();
	return caster->GetOutput();
}

void LungSegmentation::writeImage(ImageType::Pointer img, string imgFileName)
{
	if (gdcmImageIO != 0) {
		writeDicomImage(img, imgFileName);
		return;
	}

	typedef itk::ImageFileWriter< ImageType >  WriterType;
	WriterType::Pointer writer = WriterType::New();

	writer->SetFileName(imgFileName.c_str() ); // set file name
	writer->SetInput(img); // set the image to write
	try
	{
		writer->Update(); // writing
	}
	catch(itk::ExceptionObject & err)
	{
		cout << "ExceptionObject caught !" << endl;
		cout << err << endl;
	}
}

void LungSegmentation::writeDicomImage(ShortImageType::Pointer img, string
imgFileName) {
	typedef itk::ImageFileWriter< ShortImageType >  WriterType;
	WriterType::Pointer writer = WriterType::New();

	writer->UseInputMetaDataDictionaryOff();
	writer->SetImageIO( gdcmImageIO );

	writer->SetFileName(imgFileName.c_str() ); // set file name
	writer->SetInput(img); // set the image to write
	try
	{
		writer->Update(); // writing
	}
	catch(itk::ExceptionObject & err)
	{
		cout << "ExceptionObject caught !" << endl;
		cout << err << endl;
	}
}

void LungSegmentation::writeDicomImage(ImageType::Pointer img, string
imgFileName) {
	writeDicomImage(uchar2short(img), imgFileName);
}

===========
I read only 512*512 (one slice) dicom images; then I segment it, create a
new Image<unsigned char, 2> with smaller size and copy the interseting region
from the read dicom on it. 
When I write it to disk, the header has row:512 and col:512, which values
are taken from the read image.

If I cast the smaller image into a
<short, 2> Image and write it to disk and try to open it with Mricro, il
complains "the foreign image is not large enough to contain the described
slices; slices:1" and rows=cols=512 in the header.

An idea?
TIA
Samuel




More information about the Insight-users mailing list