1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmScanner.h"
#include "gdcmTesting.h"
#include "gdcmIPPSorter.h"
#include "gdcmDirectionCosines.h"
#include <cmath>
/*
* The following example is a basic sorted which should work in generic cases.
* It sort files based on:
* Study Instance UID
* Series Instance UID
* Frame of Reference UID
* Image Orientation (Patient)
* Image Position (Patient) (Sorting based on IPP + IOP)
*/
namespace gdcm {
const Tag t1(0x0020,0x000d); // Study Instance UID
const Tag t2(0x0020,0x000e); // Series Instance UID
const Tag t3(0x0020,0x0052); // Frame of Reference UID
const Tag t4(0x0020,0x0037); // Image Orientation (Patient)
class DiscriminateVolume
{
private:
std::vector< Directory::FilenamesType > SortedFiles;
std::vector< Directory::FilenamesType > UnsortedFiles;
Directory::FilenamesType GetAllFilenamesFromTagToValue(
Scanner const & s, Directory::FilenamesType const &filesubset, Tag const &t, const char *valueref)
{
Directory::FilenamesType theReturn;
if( valueref )
{
size_t len = strlen( valueref );
Directory::FilenamesType::const_iterator file = filesubset.begin();
for(; file != filesubset.end(); ++file)
{
const char *filename = file->c_str();
const char * value = s.GetValue(filename, t);
if( value && strncmp(value, valueref, len ) == 0 )
{
theReturn.push_back( filename );
}
}
}
return theReturn;
}
void ProcessAIOP(Scanner const & , Directory::FilenamesType const & subset, const char *iopval)
{
std::cout << "IOP: " << iopval << std::endl;
IPPSorter ipp;
ipp.SetComputeZSpacing( true );
ipp.SetZSpacingTolerance( 1e-3 ); // ??
bool b = ipp.Sort( subset );
if( !b )
{
// If you reach here this means you need one more parameter to discriminiat this
// series. Eg. T1 / T2 intertwinted. Multiple Echo (0018,0081)
std::cerr << "Failed to sort: " << subset.begin()->c_str() << std::endl;
for(
Directory::FilenamesType::const_iterator file = subset.begin();
file != subset.end(); ++file)
{
std::cerr << *file << std::endl;
}
UnsortedFiles.push_back( subset );
return ;
}
ipp.Print( std::cout );
SortedFiles.push_back( ipp.GetFilenames() );
}
void ProcessAFrameOfRef(Scanner const & s, Directory::FilenamesType const & subset, const char * frameuid)
{
// In this subset of files (belonging to same series), let's find those
// belonging to the same Frame ref UID:
Directory::FilenamesType files = GetAllFilenamesFromTagToValue(
s, subset, t3, frameuid);
std::set< std::string > iopset;
for(
Directory::FilenamesType::const_iterator file = files.begin();
file != files.end(); ++file)
{
//std::cout << *file << std::endl;
const char * value = s.GetValue(file->c_str(), gdcm::t4 );
assert( value );
iopset.insert( value );
}
size_t n = iopset.size();
if ( n == 0 )
{
assert( files.empty() );
return;
}
std::cout << "Frame of Ref: " << frameuid << std::endl;
if ( n == 1 )
{
ProcessAIOP(s, files, iopset.begin()->c_str() );
}
else
{
const char *f = files.begin()->c_str();
std::cerr << "More than one IOP: " << f << std::endl;
// Make sure that there is actually 'n' different IOP
gdcm::DirectionCosines ref;
gdcm::DirectionCosines dc;
for(
std::set< std::string >::const_iterator it = iopset.begin();
it != iopset.end(); ++it )
{
ref.SetFromString( it->c_str() );
for(
Directory::FilenamesType::const_iterator file = files.begin();
file != files.end(); ++file)
{
std::string value = s.GetValue(file->c_str(), gdcm::t4 );
if( value != it->c_str() )
{
dc.SetFromString( value.c_str() );
const double crossdot = ref.CrossDot(dc);
const double eps = std::fabs( 1. - crossdot );
if( eps < 1e-6 )
{
std::cerr << "Problem with IOP discrimination: " << file->c_str()
<< " " << it->c_str() << std::endl;
return;
}
}
}
}
// If we reach here this means there is actually 'n' different IOP
for(
std::set< std::string >::const_iterator it = iopset.begin();
it != iopset.end(); ++it )
{
const char *iopvalue = it->c_str();
Directory::FilenamesType iopfiles = GetAllFilenamesFromTagToValue(
s, files, t4, iopvalue );
ProcessAIOP(s, iopfiles, iopvalue );
}
}
}
void ProcessASeries(Scanner const & s, const char * seriesuid)
{
std::cout << "Series: " << seriesuid << std::endl;
// let's find all files belonging to this series:
Directory::FilenamesType seriesfiles = GetAllFilenamesFromTagToValue(
s, s.GetFilenames(), t2, seriesuid);
gdcm::Scanner::ValuesType vt3 = s.GetValues(t3);
for(
gdcm::Scanner::ValuesType::const_iterator it = vt3.begin()
; it != vt3.end(); ++it )
{
ProcessAFrameOfRef(s, seriesfiles, it->c_str());
}
}
void ProcessAStudy(Scanner const & s, const char * studyuid)
{
std::cout << "Study: " << studyuid << std::endl;
gdcm::Scanner::ValuesType vt2 = s.GetValues(t2);
for(
gdcm::Scanner::ValuesType::const_iterator it = vt2.begin()
; it != vt2.end(); ++it )
{
ProcessASeries(s, it->c_str());
}
}
public:
void Print( std::ostream & os )
{
os << "Sorted Files: " << std::endl;
for(
std::vector< Directory::FilenamesType >::const_iterator it = SortedFiles.begin();
it != SortedFiles.end(); ++it )
{
os << "Group: " << std::endl;
for(
Directory::FilenamesType::const_iterator file = it->begin();
file != it->end(); ++file)
{
os << *file << std::endl;
}
}
os << "Unsorted Files: " << std::endl;
for(
std::vector< Directory::FilenamesType >::const_iterator it = UnsortedFiles.begin();
it != UnsortedFiles.end(); ++it )
{
os << "Group: " << std::endl;
for(
Directory::FilenamesType::const_iterator file = it->begin();
file != it->end(); ++file)
{
os << *file << std::endl;
}
}
}
std::vector< Directory::FilenamesType > const & GetSortedFiles() const { return SortedFiles; }
std::vector< Directory::FilenamesType > const & GetUnsortedFiles() const { return UnsortedFiles; }
void ProcessIntoVolume( Scanner const & s )
{
gdcm::Scanner::ValuesType vt1 = s.GetValues( gdcm::t1 );
for(
gdcm::Scanner::ValuesType::const_iterator it = vt1.begin()
; it != vt1.end(); ++it )
{
ProcessAStudy( s, it->c_str() );
}
}
};
} // namespace gdcm
int main(int argc, char *argv[])
{
std::string dir1;
if( argc < 2 )
{
const char *extradataroot = NULL;
#ifdef GDCM_BUILD_TESTING
extradataroot = gdcm::Testing::GetDataExtraRoot();
#endif
if( !extradataroot )
{
return 1;
}
dir1 = extradataroot;
dir1 += "/gdcmSampleData/ForSeriesTesting/VariousIncidences/ST1";
}
else
{
dir1 = argv[1];
}
gdcm::Directory d;
d.Load( dir1.c_str(), true ); // recursive !
gdcm::Scanner s;
s.AddTag( gdcm::t1 );
s.AddTag( gdcm::t2 );
s.AddTag( gdcm::t3 );
s.AddTag( gdcm::t4 );
bool b = s.Scan( d.GetFilenames() );
if( !b )
{
std::cerr << "Scanner failed" << std::endl;
return 1;
}
gdcm::DiscriminateVolume dv;
dv.ProcessIntoVolume( s );
dv.Print( std::cout );
return 0;
}
|