예제 #1
0
        /// <summary>
        /// Unity coroutine used to create all textures using multiple threads.
        /// </summary>
        /// <param name="threadGroupState">Thread safe thread-state used to observe progress of one or multiple threads.</param>
        /// <returns>IEnumerator for usage as a coroutine</returns>
        private IEnumerator CreateTextures(ThreadGroupState threadGroupState)
        {
#if PRINT_USAGE
            Debug.Log(Time.time +
                      $" : Started Creating Textures with Window (Center {WindowCenter}, Width {WindowWidth})");
#endif

            _transversalTexture2Ds = new Texture2D[DicomFiles.Length];
            _frontalTexture2Ds     = new Texture2D[Height];
            _sagittalTexture2Ds    = new Texture2D[Width];

            var transTextureColors = new Color32[DicomFiles.Length][];
            var frontTextureColors = new Color32[Height][];
            var sagTextureColors   = new Color32[Width][];

            var transProgress = new ConcurrentQueue <int>();
            var frontProgress = new ConcurrentQueue <int>();
            var sagProgress   = new ConcurrentQueue <int>();

            StartCreatingTransTextures(threadGroupState, transProgress, _data, DicomFiles, transTextureColors, WindowWidth, WindowCenter, 2);
            StartCreatingFrontTextures(threadGroupState, frontProgress, _data, DicomFiles, frontTextureColors, WindowWidth, WindowCenter, 2);
            StartCreatingSagTextures(threadGroupState, sagProgress, _data, DicomFiles, sagTextureColors, WindowWidth, WindowCenter, 2);

            while (threadGroupState.Working > 0 || !(transProgress.IsEmpty && frontProgress.IsEmpty && sagProgress.IsEmpty))
            {
                int current;
                if (transProgress.TryDequeue(out current))
                {
                    CreateTexture2D(Width, Height, transTextureColors, _transversalTexture2Ds, current);
                    OnTextureUpdate.Invoke(SliceType.Transversal, current);
                    threadGroupState.IncrementProgress();
                }

                if (frontProgress.TryDequeue(out current))
                {
                    CreateTexture2D(Width, DicomFiles.Length, frontTextureColors, _frontalTexture2Ds, current);
                    OnTextureUpdate.Invoke(SliceType.Frontal, current);
                    threadGroupState.IncrementProgress();
                }

                if (sagProgress.TryDequeue(out current))
                {
                    CreateTexture2D(Height, DicomFiles.Length, sagTextureColors, _sagittalTexture2Ds, current);
                    OnTextureUpdate.Invoke(SliceType.Sagittal, current);
                    threadGroupState.IncrementProgress();
                }

                yield return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Fills the target array with 3D data while applying basic preprocessing.
        /// </summary>
        /// <param name="groupState">synchronized Threadstate used to observe progress of one or multiple threads.</param>
        /// <param name="files">all the DICOM files.</param>
        /// <param name="width">width of a DICOM slice.</param>
        /// <param name="height">height of a DICOM slice.</param>
        /// <param name="target">1D array receiving the 3D data.</param>
        /// <param name="start">Start index used to determine partition of images to be computed</param>
        /// <param name="end">End index used to determine upper bound of partition of images to be computed</param>
        private static void PreProcess(ThreadGroupState groupState, IReadOnlyList <DiFile> files, int width, int height,
                                       IList <int> target, int start, int end)
        {
            var storedBytes = new byte[4];

            for (var layer = start; layer < end; ++layer)
            {
                var  currentDiFile = files[layer];
                var  pixelData     = currentDiFile.RemoveElement(0x7FE0, 0x0010);
                uint mask          = ~(0xFFFFFFFF << (currentDiFile.GetHighBit() + 1));
                int  allocated     = currentDiFile.GetBitsAllocated() / 8;

                var baseOffset = layer * width * height;

                using (var pixels = new MemoryStream(pixelData.GetValues()))
                {
                    for (var y = 0; y < height; ++y)
                    {
                        for (var x = 0; x < width; ++x)
                        {
                            //get current Int value
                            pixels.Read(storedBytes, 0, allocated);
                            var value      = BitConverter.ToInt32(storedBytes, 0);
                            var currentPix = GetPixelIntensity((int)(value & mask), currentDiFile);
                            target[baseOffset + y * width + x] = currentPix;
                        }
                    }
                }

                groupState.IncrementProgress();
                Thread.Sleep(10);
            }

            groupState.Done();
        }
예제 #3
0
        /// <summary>
        /// Unity coroutine for loading the selected folder of files.
        /// </summary>
        /// <param name="folderPath">Path of the folder containing the DICOM files</param>
        /// <param name="threadGroupState">Thread safe thread-state used to observe progress of one or multiple threads.</param>
        /// <returns>IEnumerator for usage as a coroutine</returns>
        private IEnumerator InitFiles(string folderPath, ThreadGroupState threadGroupState)
        {
            threadGroupState.Register();
            //string[] filePaths = Directory.GetFiles(folderPath);

            //filePaths = Array.FindAll(filePaths, HasNoExtension);
            var fileNames = GetFiles(folderPath);

            DicomFiles = new DiFile[fileNames.Count];
            threadGroupState.TotalProgress = fileNames.Count;

            yield return(null);

            var zeroBased = true;

            foreach (var path in fileNames)
            {
                var diFile = new DiFile();
                diFile.InitFromFile(path);

                if (zeroBased && diFile.GetImageNumber() == DicomFiles.Length)
                {
                    ShiftLeft(DicomFiles);
                    zeroBased = false;
                }

                if (zeroBased)
                {
                    DicomFiles[diFile.GetImageNumber()] = diFile;
                }
                else
                {
                    DicomFiles[diFile.GetImageNumber() - 1] = diFile;
                }

                threadGroupState.IncrementProgress();
                yield return(null);
            }

            Width  = DicomFiles[0].GetImageWidth();
            Height = DicomFiles[0].GetImageHeight();

            _data = new int[DicomFiles.Length * Width * Height];

            VolumeTexture = null;

            WindowCenterPresets = DicomFiles[0].GetElement(0x0028, 0x1050)?.GetDoubles() ?? new[] { double.MinValue };
            WindowWidthPresets  = DicomFiles[0].GetElement(0x0028, 0x1051)?.GetDoubles() ?? new[] { double.MinValue };

            WindowCenter = WindowCenterPresets[0];
            WindowWidth  = WindowWidthPresets[0];

            MinPixelIntensity = (int)(DicomFiles[0].GetElement(0x0028, 0x1052)?.GetDouble() ?? 0d);
            MaxPixelIntensity = (int)((DicomFiles[0].GetElement(0x0028, 0x1053)?.GetDouble() ?? 1d) * (Math.Pow(2, DicomFiles[0].GetBitsStored()) - 1) + MinPixelIntensity);

            threadGroupState.Done();
        }
예제 #4
0
        /// <summary>
        /// Fills the given 3D color array using the given 3D pixel intensity array of same size.
        /// </summary>
        /// <param name="groupState">synchronized Threadstate used to observe progress of one or multiple threads.</param>
        /// <param name="data">pixel intensity values in a 3D Array mapped to a 1D Array.</param>
        /// <param name="dicomFiles">all the DICOM files.</param>
        /// <param name="width">width of a transversal image.</param>
        /// <param name="height">height of a transversal image.</param>
        /// <param name="target">§D color array mapped to 1D Array.</param>
        /// <param name="windowWidth">Option to set custom windowWidth, Double.MinValue to not use it</param>
        /// <param name="windowCenter">Option to set custom windowCenter, Double.MinValue to not use it</param>
        /// <param name="start">Start index used to determine partition of images to be computed</param>
        /// <param name="end">End index used to determine upper bound of partition of images to be computed</param>
        private void CreateVolume(ThreadGroupState groupState, IReadOnlyList <int> data, IReadOnlyList <DiFile> dicomFiles, int width, int height,
                                  IList <Color32> target, double windowWidth, double windowCenter, int start, int end)
        {
            var idx = start * width * height;

            for (var z = start; z < end; ++z)
            {
                var idxPartZ = z * width * height;
                for (var y = 0; y < height; ++y)
                {
                    var idxPart = idxPartZ + y * width;
                    for (var x = 0; x < width; ++x, ++idx)
                    {
                        target[idx] = TransferFunction.DYN_ALPHA(GetRGBValue(data[idxPart + x], dicomFiles[z], windowWidth, windowCenter));
                    }
                }

                Thread.Sleep(5);
                groupState.IncrementProgress();
            }

            groupState.Done();
        }