7
7
using openDicom . DataStructure ;
8
8
using System . Collections . Generic ;
9
9
using openDicom . Image ;
10
+ using System . Linq ;
10
11
11
12
namespace UnityVolumeRendering
12
13
{
@@ -15,9 +16,9 @@ public class DICOMImporter : DatasetImporterBase
15
16
private class DICOMSliceFile
16
17
{
17
18
public AcrNemaFile file ;
18
- public float location ;
19
- public float intercept ;
20
- public float slope ;
19
+ public float location = 0 ;
20
+ public float intercept = 0.0f ;
21
+ public float slope = 1.0f ;
21
22
}
22
23
23
24
private string diroctoryPath ;
@@ -44,20 +45,42 @@ public override VolumeDataset Import()
44
45
return null ;
45
46
}
46
47
48
+ IEnumerable < string > fileCandidates = Directory . EnumerateFiles ( diroctoryPath , "*.*" , recursive ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly )
49
+ . Where ( p => p . EndsWith ( ".dcm" ) || p . EndsWith ( ".dicom" ) || p . EndsWith ( ".dicm" ) ) ;
47
50
List < DICOMSliceFile > files = new List < DICOMSliceFile > ( ) ;
48
- foreach ( string filePath in Directory . EnumerateFiles ( diroctoryPath , "*.*" , recursive ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ) )
51
+ foreach ( string filePath in fileCandidates )
49
52
{
50
53
AcrNemaFile file = LoadFile ( filePath ) ;
51
54
if ( file != null && file . HasPixelData )
52
55
{
53
56
DICOMSliceFile slice = new DICOMSliceFile ( ) ;
54
57
slice . file = file ;
55
- DataElement elemLoc = file . DataSet [ new Tag ( "(0020,1041)" ) ] ;
56
- DataElement elemIntercept = file . DataSet [ new Tag ( "(0028,1052)" ) ] ;
57
- DataElement elemSlope = file . DataSet [ new Tag ( "(0028,1053)" ) ] ;
58
- slice . location = ( float ) Convert . ToDouble ( elemLoc . Value [ 0 ] ) ;
59
- slice . intercept = ( float ) Convert . ToDouble ( elemIntercept . Value [ 0 ] ) ;
60
- slice . slope = ( float ) Convert . ToDouble ( elemSlope . Value [ 0 ] ) ;
58
+ // Read location
59
+ Tag locTag = new Tag ( "(0020,1041)" ) ;
60
+ if ( file . DataSet . Contains ( locTag ) )
61
+ {
62
+ DataElement elemLoc = file . DataSet [ locTag ] ;
63
+ slice . location = ( float ) Convert . ToDouble ( elemLoc . Value [ 0 ] ) ;
64
+ }
65
+ else
66
+ {
67
+ Debug . LogError ( $ "Missing location tag in file: { filePath } .\n The file will not be imported") ;
68
+ continue ;
69
+ }
70
+ // Read intercept
71
+ Tag interceptTag = new Tag ( "(0028,1052)" ) ;
72
+ if ( file . DataSet . Contains ( interceptTag ) )
73
+ {
74
+ DataElement elemIntercept = file . DataSet [ interceptTag ] ;
75
+ slice . intercept = ( float ) Convert . ToDouble ( elemIntercept . Value [ 0 ] ) ;
76
+ }
77
+ // Read slope
78
+ Tag slopeTag = new Tag ( "(0028,1053)" ) ;
79
+ if ( file . DataSet . Contains ( slopeTag ) )
80
+ {
81
+ DataElement elemSlope = file . DataSet [ slopeTag ] ;
82
+ slice . slope = ( float ) Convert . ToDouble ( elemSlope . Value [ 0 ] ) ;
83
+ }
61
84
files . Add ( slice ) ;
62
85
}
63
86
}
@@ -106,20 +129,6 @@ public override VolumeDataset Import()
106
129
}
107
130
}
108
131
109
- /*foreach (openDicom.DataStructure.DataSet.DataElement element in file.DataSet)
110
- {
111
- Debug.Log(file.DataSet.StreamPosition);
112
- Debug.Log(element.Tag.ToString() + " - " + element.VR.Tag.GetDictionaryEntry().Description);
113
- }*/
114
-
115
- /*
116
- dataset.dimX = file.DataSet.GetEnumerator;
117
- dataset.dimY = dimY;
118
- dataset.dimZ = dimZ;
119
-
120
- int uDimension = dimX * dimY * dimZ;
121
- dataset.data = new int[uDimension];*/
122
-
123
132
return dataset ;
124
133
}
125
134
@@ -137,7 +146,7 @@ private AcrNemaFile LoadFile(string filePath)
137
146
}
138
147
catch ( Exception dicomFileException )
139
148
{
140
- Debug . LogError ( "Problems processing DICOM file:\n " + dicomFileException ) ;
149
+ Debug . LogError ( $ "Problems processing the DICOM file { filePath } :\n { dicomFileException } " ) ;
141
150
return null ;
142
151
}
143
152
return file ;
0 commit comments