[Insight-users] Re: Dicom Serie

Markus Weigert m.weigert at fz-juelich.de
Wed Feb 22 06:13:28 EST 2006


Hi Aneta,

if I understand you the right way, you want to save a part of a volume, 
which was
loaded from a serie of dicom files to disk (also as a serie of dicom files).
Maybe, the following code can help you here (you may just take what you 
need).
The code works for me but I can not guarantee that it will work in all 
possible cases.

Regards,
Markus


void RIPApplicationBase::writeImage(ImageType::Pointer imageToWrite,
                                                          std::string 
imageRole,
                                                          std::string 
imageExtension,
                                                          std::string 
directory,
                                                          std::string 
filename)
{

 
    //Write DICOM Series
    if(imageExtension =="dcm")
    {
        typedef itk::Image<PixelType,2>                            
Image2DType;
        typedef itk::ImageSeriesWriter< ImageType, Image2DType > 
SeriesWriterType;
        typedef itk::NumericSeriesFileNames      NameGeneratorType;
  
        std::string format  = filename + std::string("%03d") + 
std::string(".dcm");
   
        m_swriter->SetInput( imageToWrite );         //Image series 
writer classmember (you can instantiate a new one here)
  
        NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
        ImageType::RegionType region = 
imageToWrite->GetLargestPossibleRegion();
        ImageType::IndexType start = region.GetIndex();
        ImageType::SizeType size = region.GetSize();
        const unsigned int firstSlice = start[2];
        const unsigned int lastSlice = start[2] + size[2] - 1;
        nameGenerator->SetStartIndex(firstSlice);
        nameGenerator->SetEndIndex(lastSlice);
        nameGenerator->SetIncrementIndex(1);

        nameGenerator->SetSeriesFormat(format.c_str());
        m_swriter->SetFileNames( nameGenerator->GetFileNames()  );
   
        const std::vector<DictionaryType*>* dictArray;
        int imageSliceOrientation = 0;


        if(imageRole==IMAGE_ROLE_MOVING)
        {
            if(!m_moveImageIsDICOMType) return;
            dictArray = m_dictArrayMovingImage;
            imageSliceOrientation = m_movingImageSliceOrientation;
        }   

        if(imageRole==IMAGE_ROLE_REF)
        {
            if(!m_refImageIsDICOMType) return;
            dictArray = m_dictArrayRefImage;
            imageSliceOrientation = m_refImageSliceOrientation; //deal 
with axial, coronal and sagittal slices
        }

        //Get some tags which need to be changed after the image has 
been resampled e.g.

        std::string entryId1("0028|0010"); //Rows
        std::string entryId2("0028|0011"); //Columns
        std::string entryId3("0028|0030"); //Pixel Spacing
        std::string entryId4("0018|0050"); //Slice Thickness
        std::string entryId5("0018|0088"); //Spacing between Slices
        std::string entryId6("0020|0032"); //Image Position Patient
        std::string entryId7("0020|1041"); //Slice Location

       
    char szSpacing[50], szRows[20], szCols[20], szThickness[20], 
szSliceSpacing[20], szImagePosPat[256], szSliceLoc[20];       
    itk::Vector<double,Dimension> spacing = imageToWrite->GetSpacing();
    itk::Point<double,Dimension> origin = imageToWrite->GetOrigin();
    
    int cols = size[0];
    int rows = size[1];
    int slices = size[2];


    sprintf((char*)szSpacing,"%01.5f\\%01.5f",spacing[1],spacing[0]);
    sprintf((char*)szThickness,"%01.3f",spacing[2]);
    sprintf((char*)szRows,"%4i",rows);
    sprintf((char*)szCols,"%4i",cols);
    sprintf((char*)szSliceSpacing,"%01.3f",spacing[2]);
       
    std::string value1(szRows);
    std::string value2(szCols);;
    std::string value3(szSpacing);
    std::string value4(szThickness);
    std::string value5(szSliceSpacing);
       
       
    for(int i=0;i<dictArray->size();i++){
            DictionaryType* dictionary = dictArray->at(i);   
       
            if(imageSliceOrientation == 0)
            {
                
sprintf((char*)szImagePosPat,"%01.8f\\%01.8f\\%01.8f",origin[0] + 
(double) i * spacing[2], origin[1],origin[2]);
           
                sprintf((char*)szSliceLoc,"%01.8f", origin[0] + (double) 
i * spacing[2]);
            }
           
            if(imageSliceOrientation == 1)
            {
                
sprintf((char*)szImagePosPat,"%01.8f\\%01.8f\\%01.8f",origin[0], 
origin[1] + (double) i * spacing[2],origin[2]);
           
                sprintf((char*)szSliceLoc,"%01.8f", origin[1] + (double) 
i * spacing[2]);
            }
           
            if(imageSliceOrientation == 2)
            {
                sprintf((char*)szImagePosPat,"%01.8f\\%01.8f\\%01.8f", 
origin[0], origin[1],
                         origin[2] + (double) i * spacing[2]);
           
                sprintf((char*)szSliceLoc,"%01.8f", origin[2] + (double) 
i * spacing[2]);
            }
           
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId1, 
value1);
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId2, 
value2);
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId3, 
value3);
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId4, 
value4);
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId5, 
value5);
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId6, 
std::string(szImagePosPat));
            itk::EncapsulateMetaData<std::string>(*dictionary,entryId7, 
std::string(szSliceLoc));
       
        m_swriter->SetImageIO(m_gdcmIO);
        m_swriter->SetMetaDataDictionaryArray(dictArray);
   
        try
        {
            m_swriter->Update();
            return;
        }
       
        catch (itk::ExceptionObject& err)
        {
            cout << "Exception caught!" << endl;
            cout << err << endl;
  
        }

    } //end if(imageExtension =="dcm")
}

>Message: 8
>Date: Wed, 22 Feb 2006 11:39:55 +0100
>From: "Markova Aneta" <amarkova at etro.vub.ac.be>
>Subject: [Insight-users] Dicom serie ?
>To: <insight-users at itk.org>
>Message-ID:
>	<CC62D4D1B25C444FAD26DA1F8A6EA4290B49A0 at etropcs03.etro.vub.ac.be>
>Content-Type: text/plain; charset="us-ascii"
>
>Dear all,
>
>I would like to save as a volume only part of a dicom series. 
>Could you please give me a hint how I can resize the dicom serie or how
>to set a new file name container.
>
>
>Thanks,
>Aneta
>
>--------------------------------------------
>Aneta Markova
>
>Dept. ETRO, Vrije Universiteit Brussel
>Pleinlaan 2, B-1050 Brussel
>Belgium 
>
>p. +32 2 629 1892 
>f. +32 2 629 1889
>amarkova at etro.vub.ac.be
>--------------------------------------------
>  
>



More information about the Insight-users mailing list