X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkImage2DicomDoseGenericFilter.txx;h=df105fadfd1a75e1838d057dbd39ee6b9fdb0edb;hb=543b72e23ad001ac2a7743b9beacf48e2d0054ac;hp=4b865f60e0c0872b9adc41ea6954e13b8dd70c28;hpb=af9a6251e337d999b569f152dfc1b3b9f0da2759;p=clitk.git diff --git a/tools/clitkImage2DicomDoseGenericFilter.txx b/tools/clitkImage2DicomDoseGenericFilter.txx index 4b865f6..df105fa 100644 --- a/tools/clitkImage2DicomDoseGenericFilter.txx +++ b/tools/clitkImage2DicomDoseGenericFilter.txx @@ -31,6 +31,9 @@ #include "clitkImage2DicomDoseGenericFilter.h" #include "clitkCommon.h" +#include "itkMinimumMaximumImageCalculator.h" +#include "itkImageRegionIterator.h" +#include "itkImageRegionConstIterator.h" #if GDCM_MAJOR_VERSION >= 2 #include "gdcmUIDGenerator.h" #include @@ -135,15 +138,27 @@ void Image2DicomDoseGenericFilter::UpdateWithDimAndPixelType() { -#if GDCM_MAJOR_VERSION == 2 +#if GDCM_MAJOR_VERSION >= 2 // ImageTypes typedef itk::Image InputImageType; - typedef itk::Image OutputImageType; + typedef unsigned short int OutputPixelType; + typedef itk::Image OutputImageType; typedef itk::ImageFileReader< InputImageType > ReaderType; typedef itk::ImageSeriesReader< InputImageType > ReaderSeriesType; + typedef itk::ImageSeriesWriter WriterSerieType; typedef itk::ImageRegionIterator< InputImageType > IteratorType; + typedef itk::MinimumMaximumImageCalculator ImageCalculatorFilterType; typedef itk::GDCMImageIO ImageIOType; + //----------------------------------------------------------------------------- + // opening image input file + typename ReaderType::Pointer reader = ReaderType::New(); + const char * filename = m_ArgsInfo.input_arg; + reader->SetFileName( filename ); + reader->Update(); + typename InputImageType::Pointer image = reader->GetOutput(); + std::ostringstream value; + // Read Dicom model file typename ReaderSeriesType::Pointer readerSeries = ReaderSeriesType::New(); ImageIOType::Pointer gdcmIO = ImageIOType::New(); @@ -161,270 +176,141 @@ Image2DicomDoseGenericFilter::UpdateWithDimAndPixelType() std::cerr << excp << std::endl; } - - -//----------------------------------------------------------------------------------- -// opening dicom input file - gdcm::Reader reader2; - reader2.SetFileName( m_ArgsInfo.DicomInputFile_arg ); - reader2.Read(); - gdcm::File &mDCMFile = reader2.GetFile(); - gdcm::DataSet &ds = mDCMFile.GetDataSet(); -//mDCMFile.SetMaxSizeLoadEntry(1006384); // important size required, otherwise some data are not loaded -//mDCMFile.AddForceLoadElement(0x7fe0,0x0010); //Load pixel data no matter its size - -std::cout << "File: "<< m_ArgsInfo.DicomInputFile_arg << " loaded !"<< std::endl; - - - -//----------------------------------------------------------------------------- -// opening image input file -typename ReaderType::Pointer reader = ReaderType::New(); -const char * filename = m_ArgsInfo.input_arg; -reader->SetFileName( filename ); -reader->Update(); -typename InputImageType::Pointer image = reader->GetOutput(); - -// origin -typename InputImageType::PointType origin = image->GetOrigin(); -DD(origin); - -// size -typename InputImageType::SizeType imageSize = image->GetLargestPossibleRegion().GetSize(); -//DD(imageSize); -int NbCols=imageSize[0]; // col -int NbRows=imageSize[1]; // row -int NbFrames=imageSize[2]; // frame -DD(NbCols); -DD(NbRows); -DD(NbFrames); - -// spacing -typename InputImageType::SpacingType Spacing = image->GetSpacing(); -DD(Spacing); - -// scaling -float highestValue=pow(10,-10); -IteratorType out( image, image->GetRequestedRegion() ); -for (out.GoToBegin(); !out.IsAtEnd(); ++out){ -//DD(out.Get()); - if (out.Get()>highestValue) highestValue=out.Get(); -} -double doseScaling = highestValue/(pow(2,16)-1); -DD(doseScaling); - -// image data -std::vector ImageData; -typename InputImageType::IndexType pixelIndex; -int l=0; -unsigned short int pixelValue; -//DD(highestValue); -for (int i=0; iGetPixel(pixelIndex)/doseScaling; -if(float(image->GetPixel(pixelIndex)/doseScaling)>(pow(2,16)-1.)) { -std::cout<<"\n!!!!! WARNING !!!!! pixel index: "< Highest value may become 0"<GetPixel(pixelIndex)); -//DD(image->GetPixel(pixelIndex)/doseScaling); -DD(pixelValue); -std::cout<<"Pixel Value should be equal to "<<(pow(2,16)-1)<<" but should not be 0"<GetMetaDataDictionaryArray()))[0]; typename ReaderSeriesType::DictionaryArrayType outputArray; typename ReaderSeriesType::DictionaryRawPointer outputDict = new typename ReaderSeriesType::DictionaryType; CopyDictionary (*inputDict, *outputDict); - outputArray.push_back(outputDict); + + // origin + typename InputImageType::PointType origin = image->GetOrigin(); + value.str(""); + value<(*outputDict, "0020|0032", value.str()); + DD(origin); + + // orientation + typename InputImageType::DirectionType direction = image->GetDirection(); + value.str(""); + value<(*outputDict, "0020|0037", value.str()); + DD(direction); + + // size + typename InputImageType::SizeType imageSize = image->GetLargestPossibleRegion().GetSize(); + //DD(imageSize); + int NbCols=imageSize[0]; // col + int NbRows=imageSize[1]; // row + int NbFrames=imageSize[2]; // frame + itk::EncapsulateMetaData(*outputDict, "0028|0008", NumberToString(NbFrames)); + itk::EncapsulateMetaData(*outputDict, "0028|0010", NumberToString(NbRows)); + itk::EncapsulateMetaData(*outputDict, "0028|0011", NumberToString(NbCols)); + DD(NbCols); + DD(NbRows); + DD(NbFrames); + + // spacing + typename InputImageType::SpacingType Spacing = image->GetSpacing(); + value.str(""); + value<(*outputDict, "0028|0030", value.str()); + value.str(""); + value<(*outputDict, "0018|0050", value.str()); + DD(Spacing); + + // offset + float offset = 0.; + value.str(""); + value << offset; + for (int i=1; i(*outputDict, "3004|000c", value.str()); + DD(value.str()); + + // scaling + typename ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New(); + imageCalculatorFilter->SetImage(image); + imageCalculatorFilter->ComputeMaximum(); + float highestValue=imageCalculatorFilter->GetMaximum(); + double doseScaling = highestValue/(pow(2,16)-1); + value.str(""); + value<(*outputDict, "3004|000e", value.str()); + DD(value.str()); + + // image data + typename OutputImageType::Pointer imageOutput = OutputImageType::New(); + imageOutput->SetRegions(image->GetLargestPossibleRegion()); + imageOutput->SetSpacing(image->GetSpacing()); + imageOutput->SetOrigin(image->GetOrigin()); + imageOutput->SetDirection(image->GetDirection()); + imageOutput->Allocate(); + + typename itk::ImageRegionConstIterator imageIterator(image,image->GetLargestPossibleRegion()); + typename itk::ImageRegionIterator imageOutputIterator(imageOutput,imageOutput->GetLargestPossibleRegion()); + while(!imageIterator.IsAtEnd()) { + // Set the current pixel to white + imageOutputIterator.Set((signed short int)(imageIterator.Get()/doseScaling)); + + ++imageIterator; + ++imageOutputIterator; + } // Output directory and filenames - typedef itk::ImageSeriesWriter WriterSerieType; typename WriterSerieType::Pointer writerSerie = WriterSerieType::New(); - writerSerie->SetInput( image ); + outputArray.push_back(outputDict); + writerSerie->SetInput( imageOutput ); writerSerie->SetImageIO( gdcmIO ); typename ReaderSeriesType::FileNamesContainer fileNamesOutput; fileNamesOutput.push_back(filename_out); writerSerie->SetFileNames( fileNamesOutput ); writerSerie->SetMetaDataDictionaryArray(&outputArray); - - - - - - - - - - - - - - - - -//-------------------------------------------------------------- -std::cout<<"\nECRITURE DU FICHIER DICOM !"<Update(); + } catch( itk::ExceptionObject & excp ) { + std::cerr << "Error: Exception thrown while writing the series!!" << std::endl; + std::cerr << excp << std::endl; } -DE.SetVR( gdcm::VR::US ); -DE.SetByteValue(Value.c_str(), 1); -ds.Insert(DE); -DD(Value); -Value = ""; - -/* -// NbCols -b = ((gdcm::SQItem*)mDCMFile)->GetValEntry(0x0028,0x0011); -Value<SetValue(Value.str()); -DD(Value.str()); -Value.str(""); - -// NbRows -b = ((gdcm::SQItem*)mDCMFile)->GetValEntry(0x0028,0x0010); -Value<SetValue(Value.str()); -DD(Value.str()); -Value.str(""); -//DD(Value.str()); - -// NbFrames -b = ((gdcm::SQItem*)mDCMFile)->GetValEntry(0x0028,0x0008); -Value<SetValue(Value.str()); -DD(Value.str()); -Value.str(""); - -// doseScaling -b = ((gdcm::SQItem*)mDCMFile)->GetValEntry(0x3004,0x000e); -Value<SetValue(Value.str()); -DD(Value.str()); -Value.str(""); - -// Spacing X Y -b = ((gdcm::SQItem*)mDCMFile)->GetValEntry(0x0028, 0x0030); -Value<SetValue(Value.str()); -DD(Value.str()); -Value.str(""); - -// Spacing Z ([Grid Frame Offset Vector]) -b = ((gdcm::SQItem*)mDCMFile)->GetValEntry(0x3004, 0x000c); -float offset=0.; -Value<SetValue(Value.str()); -DD(Value.str()); -Value.str(""); -*/ -//ImageData -//bool data = mDCMFile->SetBinEntry(reinterpret_cast( &(ImageData[0]) ) , (int)(sizeof(unsigned short int) * ImageData.size()) , 0x7fe0, 0x0010); -//if (data) std::cout<<"\n DICOM dose data written !"<GetFileType(); -//type=(gdcm::FileType)2; -bool ecriture = mDCMFile->Write (args_info.OutputFile_arg, type); -if (ecriture) std::cout<<"\n DICOM File written !"<Delete(); //--------------------------------------------------------------------------------------- @@ -547,8 +433,8 @@ mDCMFile->Write (args_info.OutputFile_arg, type); //((gdcm::SQItem*)mDCMFile)->RemoveEntry(a); //----------------------------------------------------------------------------------- -std::cout <<"\n## DICOM Image to RT DOSE application is ended..."< std::string NumberToString ( T Number ) +{ + std::ostringstream ss; + ss << Number; + return ss.str(); +} +//--------------------------------------------------------------------------- + + }//end clitk #endif //#define clitkImage2DicomDoseGenericFilter_txx