示例#1
0
        /// <inheritdoc/>
        public Image ResampleImage2D(Image image, int outputSize)
        {
            Ensure.ArgumentNotNull(image, nameof(image));

            var dimension = image.GetDimension();

            if (dimension != 2)
            {
                throw new NotSupportedException($"The dimension ({dimension}) of the provided image is not supported.");
            }

            var inputSize      = image.GetSize();
            var inputSpacing   = image.GetSpacing();
            var inputDirection = image.GetDirection();
            var inputOrigin    = image.GetOrigin();

            double oldw = inputSize[0] * inputSpacing[0];
            double oldh = inputSize[1] * inputSpacing[1];
            double neww, newh = 0;
            double rw = oldw / outputSize;
            double rh = oldh / outputSize;

            if (rw > rh)
            {
                newh = oldh / rw;
                neww = outputSize;
            }
            else
            {
                neww = oldw / rh;
                newh = outputSize;
            }

            var outputSpacing = new VectorDouble()
            {
                inputSpacing[0] * inputSize[0] / neww,
                inputSpacing[1] * inputSize[1] / newh
            };

            var actualSize = new VectorUInt32()
            {
                Convert.ToUInt32(neww),
                Convert.ToUInt32(newh)
            };

            // https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAnImage/Documentation.html
            ResampleImageFilter filter = new ResampleImageFilter();

            filter.SetReferenceImage(image);
            filter.SetSize(actualSize);
            filter.SetOutputSpacing(outputSpacing);
            Image output = filter.Execute(image);

            filter.Dispose();

            return(output);
        }
示例#2
0
        /// <inheritdoc/>
        public Image ResampleImage3D(Image image, int outputSize)
        {
            Ensure.ArgumentNotNull(image, nameof(image));

            var dimension = image.GetDimension();

            if (dimension != 3)
            {
                throw new NotSupportedException($"The dimension ({dimension}) of the provided image is not supported.");
            }

            var outputSizeConverted = Convert.ToUInt32(outputSize);
            var inputSize           = image.GetSize();
            var inputSpacing        = image.GetSpacing();

            var desiredSize = new VectorUInt32()
            {
                outputSizeConverted,
                outputSizeConverted,
                outputSizeConverted
            };

            var outputSpacing = new VectorDouble()
            {
                inputSpacing[0] * inputSize[0] / desiredSize[0],
                inputSpacing[1] * inputSize[1] / desiredSize[1],
                inputSpacing[2] * inputSize[2] / desiredSize[2]
            };

            // https://itk.org/Wiki/ITK/Examples/ImageProcessing/ResampleImageFilter
            ResampleImageFilter filter = new ResampleImageFilter();

            filter.SetReferenceImage(image);
            filter.SetTransform(new ScaleTransform(image.GetDimension()));
            filter.SetInterpolator(InterpolatorEnum.sitkLinear);
            filter.SetSize(desiredSize);
            filter.SetOutputSpacing(outputSpacing);
            Image output = filter.Execute(image);

            filter.Dispose();

            return(output);
        }
示例#3
0
        /// <summary>
        /// Resamples the three-dimensional ITK image to the desired size.
        /// </summary>
        /// <param name="image">The three-dimensional ITK image.</param>
        /// <param name="desiredSize">The desired size.</param>
        /// <returns>
        /// The resampled three-dimensional ITK image.
        /// </returns>
        /// <exception cref="ArgumentNullException">image</exception>
        /// <exception cref="NotSupportedException">The dimension ({dimension}) of the provided image is not supported.</exception>
        public Image ResampleImage3D(Image image, uint desiredSize)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var dimension = image.GetDimension();

            if (dimension != 3)
            {
                throw new NotSupportedException($"The dimension ({dimension}) of the provided image is not supported.");
            }

            var inputSize    = image.GetSize();
            var inputSpacing = image.GetSpacing();

            var outputSize = new VectorUInt32()
            {
                desiredSize,
                desiredSize,
                desiredSize
            };

            var outputSpacing = new VectorDouble()
            {
                inputSpacing[0] * inputSize[0] / outputSize[0],
                inputSpacing[1] * inputSize[1] / outputSize[1],
                inputSpacing[2] * inputSize[2] / outputSize[2]
            };

            // https://itk.org/Wiki/ITK/Examples/ImageProcessing/ResampleImageFilter
            var filter = new ResampleImageFilter();

            filter.SetReferenceImage(image);
            filter.SetTransform(new ScaleTransform(image.GetDimension()));
            filter.SetInterpolator(InterpolatorEnum.sitkLinear);
            filter.SetSize(outputSize);
            filter.SetOutputSpacing(outputSpacing);
            return(filter.Execute(image));
        }
示例#4
0
        /// <summary>
        /// Resamples the two-dimensional ITK image to the desired size.
        /// </summary>
        /// <param name="image">The two-dimensional ITK image.</param>
        /// <param name="desiredSize">The desired size.</param>
        /// <returns>
        /// The resampled two-dimensional ITK image.
        /// </returns>
        /// <exception cref="ArgumentNullException">image</exception>
        /// <exception cref="NotSupportedException">The dimension ({dimension}) of the provided image is not supported.</exception>
        public Image ResampleImage2D(Image image, uint desiredSize)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var dimension = image.GetDimension();

            if (dimension != 2)
            {
                throw new NotSupportedException($"The dimension ({dimension}) of the provided image is not supported.");
            }

            var inputSize      = image.GetSize();
            var inputSpacing   = image.GetSpacing();
            var inputDirection = image.GetDirection();
            var inputOrigin    = image.GetOrigin();

            var outputSize = new VectorUInt32()
            {
                desiredSize,
                desiredSize
            };

            double oldw = inputSize[0] * inputSpacing[0];
            double oldh = inputSize[1] * inputSpacing[1];
            double neww, newh = 0;
            double rw = oldw / desiredSize;
            double rh = oldh / desiredSize;

            if (rw > rh)
            {
                newh = oldh / rw;
                neww = desiredSize;
            }
            else
            {
                neww = oldw / rh;
                newh = desiredSize;
            }

            var outputSpacing = new VectorDouble()
            {
                inputSpacing[0] * inputSize[0] / neww,
                inputSpacing[1] * inputSize[1] / newh
            };

            var actualSize = new VectorUInt32()
            {
                (uint)neww,
                (uint)newh
            };

            // https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAnImage/Documentation.html
            var resampleFilter = new ResampleImageFilter();

            resampleFilter.SetReferenceImage(image);
            resampleFilter.SetSize(actualSize);
            resampleFilter.SetOutputSpacing(outputSpacing);

            var outputImage = resampleFilter.Execute(image);

            return(outputImage);
        }