MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / ConvertImage

Method ConvertImage

Examples/DicomConvert/DicomConvert.cs:112–153  ·  view source on GitHub ↗
(string inputFile, string outputFile, int? newWidth)

Source from the content-addressed store, hash-verified

110 }
111
112 static bool ConvertImage(string inputFile, string outputFile, int? newWidth)
113 {
114 try
115 {
116 var reader = new ImageFileReader();
117 // Only read DICOM images
118 reader.SetImageIO("GDCMImageIO");
119 reader.SetFileName(inputFile);
120 var image = reader.Execute();
121 var imageSize = image.GetSize();
122 // If 3D image with single slice, treat as 2D
123 if (imageSize.Count == 3 && imageSize[2] == 1) {
124 ExtractImageFilter extractFilter = new ExtractImageFilter();
125 extractFilter.SetIndex(new VectorInt32( new int[] { 0, 0, 0 }));
126 imageSize[2] = 0;
127 extractFilter.SetSize(imageSize);
128 image = extractFilter.Execute(image);
129 }
130 // Resample if new width is specified
131 if (newWidth.HasValue)
132 {
133 var originalSize = image.GetSize();
134 var originalSpacing = image.GetSpacing();
135 double newSpacing = ((originalSize[0] - 1) * originalSpacing[0]) / (newWidth.Value - 1);
136 long newHeight = (long)(((originalSize[1] - 1) * originalSpacing[1]) / newSpacing);
137 image = SimpleITK.Resample(image, new VectorUInt32(new long[] { newWidth.Value, newHeight }), new Transform(), InterpolatorEnum.sitkLinear,
138 image.GetOrigin(), new VectorDouble(new double[] { newSpacing, newSpacing }), image.GetDirection(), 0, image.GetPixelID());
139 }
140 // If a single channel image, rescale to [0,255] and cast to UInt8.
141 if (image.GetNumberOfComponentsPerPixel() == 1)
142 {
143 image = SimpleITK.RescaleIntensity(image, 0, 255);
144 image = SimpleITK.Cast(image, PixelIDValueEnum.sitkUInt8);
145 }
146 SimpleITK.WriteImage(image, outputFile);
147 return true;
148 }
149 catch
150 {
151 return false;
152 }
153 }
154 }
155}

Callers

nothing calls this directly

Calls 10

SetSizeMethod · 0.80
GetSpacingMethod · 0.80
GetOriginMethod · 0.80
GetDirectionMethod · 0.80
SetImageIOMethod · 0.45
SetFileNameMethod · 0.45
ExecuteMethod · 0.45
GetSizeMethod · 0.45
GetPixelIDMethod · 0.45

Tested by

no test coverage detected