public VolumeDataset Import() { // Check that the file exists if (!File.Exists(filePath)) { Debug.LogError("The file does not exist: " + filePath); return(null); } FileStream fs = new FileStream(filePath, FileMode.Open); BinaryReader reader = new BinaryReader(fs); // Check that the dimension does not exceed the file size long expectedFileSize = (long)(dimX * dimY * dimZ) * GetSampleFormatSize(contentFormat) + skipBytes; if (fs.Length < expectedFileSize) { Debug.LogError($"The dimension({dimX}, {dimY}, {dimZ}) exceeds the file size. Expected file size is {expectedFileSize} bytes, while the actual file size is {fs.Length} bytes"); reader.Close(); fs.Close(); return(null); } VolumeDataset dataset = new VolumeDataset(); dataset.datasetName = Path.GetFileName(filePath); dataset.filePath = filePath; dataset.dimX = dimX; dataset.dimY = dimY; dataset.dimZ = dimZ; // Skip header (if any) if (skipBytes > 0) { reader.ReadBytes(skipBytes); } int uDimension = dimX * dimY * dimZ; dataset.data = new float[uDimension]; // Read the data/sample values for (int i = 0; i < uDimension; i++) { dataset.data[i] = (float)ReadDataValue(reader); } Debug.Log("Loaded dataset in range: " + dataset.GetMinDataValue() + " - " + dataset.GetMaxDataValue()); reader.Close(); fs.Close(); dataset.FixDimensions(); return(dataset); }
public VolumeDataset Import() { if (!Directory.Exists(directoryPath)) { throw new NullReferenceException("No directory found: " + directoryPath); } List <string> imagePaths = GetSortedImagePaths(); Vector3Int dimensions = GetVolumeDimensions(imagePaths); int[] data = FillSequentialData(dimensions, imagePaths); VolumeDataset dataset = FillVolumeDataset(data, dimensions); dataset.FixDimensions(); return(dataset); }
public VolumeDataset ImportDICOMSeries(DICOMSeries series) { List <DICOMSliceFile> files = series.dicomFiles; // Check if the series is missing the slice location tag bool needsCalcLoc = false; foreach (DICOMSliceFile file in files) { needsCalcLoc |= file.missingLocation; } // Calculate slice location from "Image Position" (0020,0032) if (needsCalcLoc) { CalcSliceLocFromPos(files); } // Sort files by slice location files.Sort((DICOMSliceFile a, DICOMSliceFile b) => { return(a.location.CompareTo(b.location)); }); Debug.Log($"Importing {files.Count} DICOM slices"); if (files.Count <= 1) { Debug.LogError("Insufficient number of slices."); return(null); } // Create dataset VolumeDataset dataset = new VolumeDataset(); dataset.datasetName = Path.GetFileName(datasetName); dataset.dimX = files[0].file.PixelData.Columns; dataset.dimY = files[0].file.PixelData.Rows; dataset.dimZ = files.Count; int dimension = dataset.dimX * dataset.dimY * dataset.dimZ; dataset.data = new float[dimension]; for (int iSlice = 0; iSlice < files.Count; iSlice++) { DICOMSliceFile slice = files[iSlice]; PixelData pixelData = slice.file.PixelData; int[] pixelArr = ToPixelArray(pixelData); if (pixelArr == null) // This should not happen { pixelArr = new int[pixelData.Rows * pixelData.Columns]; } for (int iRow = 0; iRow < pixelData.Rows; iRow++) { for (int iCol = 0; iCol < pixelData.Columns; iCol++) { int pixelIndex = (iRow * pixelData.Columns) + iCol; int dataIndex = (iSlice * pixelData.Columns * pixelData.Rows) + (iRow * pixelData.Columns) + iCol; int pixelValue = pixelArr[pixelIndex]; float hounsfieldValue = pixelValue * slice.slope + slice.intercept; dataset.data[dataIndex] = Mathf.Clamp(hounsfieldValue, -1024.0f, 3071.0f); } } } if (files[0].pixelSpacing > 0.0f) { dataset.scaleX = files[0].pixelSpacing * dataset.dimX; dataset.scaleY = files[0].pixelSpacing * dataset.dimY; dataset.scaleZ = Mathf.Abs(files[files.Count - 1].location - files[0].location); } dataset.FixDimensions(); return(dataset); }