Skip to content

Commit 247224b

Browse files
committed
Fix for DICOM import failing on missing "slope" or "intercept" elements. Also made it ignore non-DICOM files.
1 parent 1651e84 commit 247224b

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

Assets/Scripts/Importing/DICOMImporter.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using openDicom.DataStructure;
88
using System.Collections.Generic;
99
using openDicom.Image;
10+
using System.Linq;
1011

1112
namespace UnityVolumeRendering
1213
{
@@ -15,9 +16,9 @@ public class DICOMImporter : DatasetImporterBase
1516
private class DICOMSliceFile
1617
{
1718
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;
2122
}
2223

2324
private string diroctoryPath;
@@ -44,20 +45,42 @@ public override VolumeDataset Import()
4445
return null;
4546
}
4647

48+
IEnumerable<string> fileCandidates = Directory.EnumerateFiles(diroctoryPath, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
49+
.Where(p => p.EndsWith(".dcm") || p.EndsWith(".dicom") || p.EndsWith(".dicm"));
4750
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)
4952
{
5053
AcrNemaFile file = LoadFile(filePath);
5154
if(file != null && file.HasPixelData)
5255
{
5356
DICOMSliceFile slice = new DICOMSliceFile();
5457
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+
}
6184
files.Add(slice);
6285
}
6386
}
@@ -106,20 +129,6 @@ public override VolumeDataset Import()
106129
}
107130
}
108131

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-
123132
return dataset;
124133
}
125134

@@ -137,7 +146,7 @@ private AcrNemaFile LoadFile(string filePath)
137146
}
138147
catch (Exception dicomFileException)
139148
{
140-
Debug.LogError("Problems processing DICOM file:\n" + dicomFileException);
149+
Debug.LogError($"Problems processing the DICOM file {filePath} :\n {dicomFileException}");
141150
return null;
142151
}
143152
return file;

0 commit comments

Comments
 (0)