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

Method convertImage

Examples/DicomConvert/DicomConvert.java:92–127  ·  view source on GitHub ↗
(String inputFile, String outputFile, Integer newWidth)

Source from the content-addressed store, hash-verified

90 }
91
92 static boolean convertImage(String inputFile, String outputFile, Integer newWidth) {
93 try {
94 ImageFileReader reader = new ImageFileReader();
95 // Only read DICOM images
96 reader.setImageIO("GDCMImageIO");
97 reader.setFileName(inputFile);
98 Image image = reader.execute();
99 // If 3D image with single slice, treat as 2D
100 VectorUInt32 imageSize = image.getSize();
101 if (imageSize.size() == 3 && imageSize.get(2).longValue() == 1) {
102 ExtractImageFilter extractFilter = new ExtractImageFilter();
103 extractFilter.setIndex(new VectorInt32(new int[]{0, 0, 0}));
104 imageSize.set(2, 0L);
105 extractFilter.setSize(imageSize);
106 image = extractFilter.execute(image);
107 }
108 // Resample if new width is specified
109 if (newWidth != null) {
110 VectorUInt32 originalSize = image.getSize();
111 VectorDouble originalSpacing = image.getSpacing();
112 double newSpacing = ((originalSize.get(0) - 1) * originalSpacing.get(0)) / (newWidth - 1);
113 long newHeight = (long)(((originalSize.get(1) - 1) * originalSpacing.get(1)) / newSpacing);
114 image = SimpleITK.resample(image, new VectorUInt32(new long[]{newWidth, newHeight}), new Transform(), InterpolatorEnum.sitkLinear,
115 image.getOrigin(), new VectorDouble(new double[]{newSpacing, newSpacing}), image.getDirection(), 0, image.getPixelID());
116 }
117 // If a single channel image, rescale to [0,255] and cast to UInt8.
118 if (image.getNumberOfComponentsPerPixel() == 1) {
119 image = SimpleITK.rescaleIntensity(image, 0, 255);
120 image = SimpleITK.cast(image, PixelIDValueEnum.sitkUInt8);
121 }
122 SimpleITK.writeImage(image, outputFile);
123 return true;
124 } catch (Exception e) {
125 return false;
126 }
127 }
128}

Callers 1

mainMethod · 0.95

Calls 1

executeMethod · 0.45

Tested by

no test coverage detected