]> Creatis software - gdcm.git/blob - Example/exCTPET.cxx
ENH: Adding a CT-PET matcher
[gdcm.git] / Example / exCTPET.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exCTPET.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/02 21:54:23 $
7   Version:   $Revision: 1.1 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmSerieHelper.h"
20 #include "gdcmDirList.h"
21 #include "gdcmUtil.h"
22 #include "gdcmDataEntry.h"
23
24 int main(int argc, char *argv[])
25 {
26   if(argc < 2 )
27     {
28     std::cerr << "RTFM" << std::endl;
29     return 1;
30     }
31
32   // Get the directory name
33   const char *directory = argv[1];
34 #if 0
35   gdcm::SerieHelper *s;
36    std::cout << "Dir Name :[" << directory << "]" << std::endl;
37
38    s = gdcm::SerieHelper::New();
39    s->SetLoadMode(gdcm::LD_NOSHADOW | gdcm::LD_NOSEQ);
40    // 0008|0060 [CS] [Modality] [CT]   
41    gdcm::TagKey modality(0x0008,0x0060);
42    s->AddRestriction(modality, "CT", gdcm::GDCM_EQUAL); // Keep only files where
43                                               // restriction is true
44    s->SetDirectory(directory, true); // true : recursive exploration
45
46 //   std::cout << " ---------------------------------------- Finish parsing :["
47 //             << directory << "]" << std::endl;
48 //
49 //   s->Print();
50 //   std::cout << " ---------------------------------------- Finish printing (1)"
51 //             << std::endl;
52
53    int nbFiles;
54    // For all the Single SerieUID Files Sets of the gdcm::Serie
55    gdcm::FileList *l = s->GetFirstSingleSerieUIDFileSet();
56    while (l)
57    { 
58       nbFiles = l->size() ;
59       //if ( l->size() > 3 ) // Why not ? Just an example, for testing
60       {
61          //std::cout << "Sort list : " << nbFiles << " long" << std::endl;
62          s->OrderFileList(l);  // sort the list
63       }
64       l = s->GetNextSingleSerieUIDFileSet();
65    } 
66   s->Delete();
67 #endif
68
69   // Open a file A
70   // Open another file B
71   // Same Serie/Study ?
72   // No -> return
73   // Yes -> continue
74   // Same Frame of Reference
75   // No -> Return
76   // Yes -> continue
77   // A is CT and B is PT (PET)
78   // No -> return
79   // Yes -> continue
80   // Same Image Position (no string comparison !!)
81   // Even if floating point comparison are dangerous, string comp will not work in general case
82   // No -> Return
83   // Yes: We found a match
84
85   const char filename1[] = "/tmp/PETCT/case1/WHOLEB001_CT001.dcm";
86   const char filename2[] = "/tmp/PETCT/case1/CT_PET001_CT100.dcm";
87   gdcm::File *fileRef = gdcm::File::New();
88   fileRef->SetFileName( filename2 );
89   fileRef->Load();
90   // 0008 0060 CS 1 Modality
91   std::string modalityRef = fileRef->GetEntryString(0x0008,0x0060);
92   if( modalityRef == gdcm::GDCM_UNFOUND ) return 1;
93   if ( !gdcm::Util::DicomStringEqual(modalityRef, "CT") ) return 1;
94     // 0020 000d UI 1 Study Instance UID
95     // 0020 000e UI REL Series Instance UID
96   std::string series_uid_ref = fileRef->GetEntryString(0x0020, 0x000e);
97   // 0020 0052 UI 1 Frame of Reference UID
98   std::string frame_uid_ref = fileRef->GetEntryString(0x0020, 0x0052);
99   // 0020 0032 DS 3 Image Position (Patient)
100   gdcm::DataEntry *imagePosRef = fileRef->GetDataEntry(0x0020,0x0032);
101   assert( imagePosRef->GetValueCount() == 3 );
102
103   gdcm::DirList dirList( directory, true );
104   const gdcm::DirListType filenames = dirList.GetFilenames();
105   gdcm::DirListType::const_iterator it = filenames.begin();
106   gdcm::File *file = gdcm::File::New();
107   for( ; it != filenames.end(); ++it)
108     {
109     file->SetFileName( *it );
110     file->Load();
111     std::string modality   = file->GetEntryString(0x0008,0x0060);
112     // This is a dual modality: modality should be *different*
113     if( modality == modalityRef ) continue;
114     if ( !gdcm::Util::DicomStringEqual(modality, "PT") ) continue;
115     std::string series_uid = file->GetEntryString(0x0020, 0x000e);
116     // Not same series !
117     if( series_uid == series_uid_ref ) continue;
118     std::string frame_uid = file->GetEntryString(0x0020, 0x0052);
119     if( frame_uid_ref != frame_uid ) continue;
120     gdcm::DataEntry *imagePos = file->GetDataEntry(0x0020,0x0032);
121     assert( imagePos->GetValueCount() == 3 );
122     if( imagePos->GetValue(0) == imagePosRef->GetValue(0)
123       && imagePos->GetValue(1) == imagePosRef->GetValue(1)
124       && imagePos->GetValue(2) == imagePosRef->GetValue(2) )
125       {
126       std::cerr << "We found a match: " << *it << std::endl;
127       }
128     }
129   return 0;
130 }
131