GDCM: ReWriteSCAsMR.py

GDCM 3.2.7

ReWriteSCAsMR.py
1
14
15"""
16GDCM 1.x would write out MR Image Storage as Secondary Capture Object while still setting Rescale Slope/Intercept
17and saving the Pixel Spacing in (0028,0030)
18"""
19
20import gdcm
21import sys,os
22
23def CheckSecondaryCaptureObjectIsMRImageStorage(r):
24 ds = r.GetFile().GetDataSet()
25 # Check Source Image Sequence
26 if ds.FindDataElement( gdcm.Tag(0x0008,0x2112) ):
27 sis = ds.GetDataElement( gdcm.Tag(0x0008,0x2112) )
28 sqsis = sis.GetSequenceOfItems()
29 if sqsis.GetNumberOfItems():
30 item1 = sqsis.GetItem(1)
31 nestedds = item1.GetNestedDataSet()
32 if nestedds.FindDataElement( gdcm.Tag(0x0008,0x1150) ):
33 ReferencedSOPClassUID = nestedds.GetDataElement( gdcm.Tag(0x0008,0x1150) )
34 raw = ReferencedSOPClassUID.GetByteValue().GetPointer()
35 uids = gdcm.UIDs()
36 # what is the actual object we are looking at ?
37 ms = gdcm.MediaStorage()
38 ms.SetFromDataSet(ds)
39 msuid = ms.GetString()
40 uids.SetFromUID( msuid )
41 msuidname = uids.GetName() # real Media Storage Name
42 uids.SetFromUID( raw )
43 sqmsuidname = uids.GetName() # Source Image Sequence Media Storage Name
44 # If object is SC and Source derivation is MRImageStorage then we can assume 'Pixel Spacing' is correct
45 if( sqmsuidname == 'MR Image Storage' and msuidname == 'Secondary Capture Image Storage' ):
46 return True
47 # in all other case simply return the currentspacing:
48 return False
49
50if __name__ == "__main__":
51 r = gdcm.ImageReader()
52 filename = sys.argv[1]
53 r.SetFileName( filename )
54 if not r.Read():
55 sys.exit(1)
56 f = r.GetFile()
57
58 if( CheckSecondaryCaptureObjectIsMRImageStorage(r) ):
59 # Special handling of the spacing:
60 # GDCM 1.2.0 would not rewrite correctly DICOM Object and would always set them as 'Secondary Capture Image Storage'
61 # while we would rather have 'MR Image Storage'
62 gdcm.ImageHelper.SetForcePixelSpacing( True )
63 mrspacing = gdcm.ImageHelper.GetSpacingValue( r.GetFile() )
64 # TODO: I cannot do simply the following:
65 #image.SetSpacing( mrspacing )
66 image.SetSpacing(0, mrspacing[0] )
67 image.SetSpacing(1, mrspacing[1] )
68 image.SetSpacing(2, mrspacing[2] )
69 gdcm.ImageHelper.SetForceRescaleInterceptSlope( True )
70 ris = gdcm.ImageHelper.GetRescaleInterceptSlopeValue( r.GetFile() )
71 image.SetIntercept( ris[0] )
72 image.SetSlope( ris[1] )
73
74 outfilename = sys.argv[2]
75 w = gdcm.ImageWriter()
76 w.SetFileName( outfilename )
77 w.SetFile( r.GetFile() )
78 w.SetImage( image )
79 if not w.Write():
80 sys.exit(1)
81
82 sys.exit(0)
gdcm::ImageHelper::GetRescaleInterceptSlopeValue
static std::vector< double > GetRescaleInterceptSlopeValue(File const &f)
gdcm::ImageHelper::SetForcePixelSpacing
static void SetForcePixelSpacing(bool)
gdcm::ImageHelper::GetSpacingValue
static std::vector< double > GetSpacingValue(File const &f)
Set/Get Spacing from/to a File.
gdcm::ImageHelper::SetForceRescaleInterceptSlope
static void SetForceRescaleInterceptSlope(bool)
gdcm::ImageReader
ImageReader.
Definition gdcmImageReader.h:34
gdcm::ImageWriter
ImageWriter.
Definition gdcmImageWriter.h:33
gdcm::MediaStorage
MediaStorage.
Definition gdcmMediaStorage.h:44
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::UIDs
all known uids
Definition gdcmUIDs.h:31