/// <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); }
/// <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)); }