Exemple #1
0
        /// <summary>
        /// Reads the image asynchronous.
        /// </summary>
        /// <param name="path">The location of the image on the file system.</param>
        /// <param name="ct">The cancellation token.</param>
        /// <returns>
        /// The ITK image.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// path
        /// or
        /// ct
        /// </exception>
        public async Task <Image> ReadImageAsync(string path, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (ct == null)
            {
                throw new ArgumentNullException(nameof(ct));
            }

            return(await Task.Run(
                       () =>
            {
                // TODO:
                // - check if path is a directory
                // - check if path contains multiple files
                // - determine file format
                // read DICOM image series
                // https://simpleitk.readthedocs.io/en/master/Examples/DicomSeriesReader/Documentation.html
                using (ImageFileReader reader = new ImageFileReader())
                {
                    try
                    {
                        ct.Register(() =>
                        {
                            reader.Abort();
                        });

                        ct.ThrowIfCancellationRequested();

                        reader.SetFileName(path);

                        Image image = reader.Execute();
                        image = ApplyRescaleIntensityImageFilter(image);
                        image = ApplyCastImageFilter(image);
                        return image;
                    }
                    catch (OperationCanceledException e)
                    {
                        throw new AmiException("The reading of the ITK image has been cancelled.", e);
                    }
                    catch (Exception e)
                    {
                        throw new AmiException("The ITK image could not be read.", e);
                    }
                }
            }, ct));
        }