Sitk C# Scale Image Up

Hi all, I want to scale up an image twice as big using Sitk C#:

var img = Sitk.ReadImage(file);
var scale = new ScaleTransform(2, new VectorDouble(2) { 0.5, 0.5 });
var imageScaled = Sitk.Resample(img, scale, InterpolatorEnum.sitkBSpline, 0.0 );
Sitk.WriteImage(imageScaled, dirOut + "resampled.png");

The imageScaled has the same width & height and spacing as the input image, it shows quarter of the original content, twice as big. How can I resize the whole image, so the result has width * 2, height * 2, and still show the full content? Seems a very simple problem but I just couldn’t get it work…

Thanks for help!

The simplest way would be to use the ExandImageFilter, which increases the resolution by a fixed integer.

Alternatively please review the model ITK uses to represent the Image and its geometry:
https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html#images

Commonly it is desirable to maintain the physical location of the image data while “scaling” up the image. This could be done by using the input image (img) as a reference image for the ResampleImageFilter, and then change the spacing and origin to match the desired resampling resolution and location.

2 Likes

Thanks! So does that mean Resample() etc. are designed to keep the physical location and change the spacing, other than those common paint or photo editing software just change the number of pixels?

Yes, the Resample filter ( as with most of ITK ) is designed to operate in physical space and not just pixel space the way photo editing software is.

2 Likes

Thank you Blowekamp!

Just for the completeness of this post, so the simplest way to do it in C#:
var imgExpand = Sitk.Expand(img, new VectorUInt32(new uint[] { 2, 2 }), InterpolatorEnum.sitkLinear);

2 Likes