ITK  6.0.0
Insight Toolkit
SphinxExamples/src/IO/ImageBase/GenerateSlicesFromVolume/Code.py
1 #!/usr/bin/env python
2 
3 # Copyright NumFOCUS
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # https://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import itk
18 import argparse
19 
20 itk.auto_progress(2)
21 
22 parser = argparse.ArgumentParser(description="Generate Slices From Volume.")
23 parser.add_argument("input_image")
24 parser.add_argument("output_image")
25 parser.add_argument("extension", nargs="?")
26 args = parser.parse_args()
27 
28 if args.extension:
29  extension = args.extension
30 else:
31  extension = ".png"
32 
33 fileNameFormat = args.output_image + "-%d" + extension
34 
35 Dimension = 3
36 
37 PixelType = itk.UC
38 InputImageType = itk.Image[PixelType, Dimension]
39 
40 ReaderType = itk.ImageFileReader[InputImageType]
41 reader = ReaderType.New()
42 reader.SetFileName(args.input_image)
43 
44 OutputPixelType = itk.UC
45 RescaleImageType = itk.Image[OutputPixelType, Dimension]
46 
47 RescaleFilterType = itk.RescaleIntensityImageFilter[InputImageType, RescaleImageType]
48 rescale = RescaleFilterType.New()
49 rescale.SetInput(reader.GetOutput())
50 rescale.SetOutputMinimum(0)
51 rescale.SetOutputMaximum(255)
52 rescale.UpdateLargestPossibleRegion()
53 
54 region = reader.GetOutput().GetLargestPossibleRegion()
55 size = region.GetSize()
56 
58 fnames.SetStartIndex(0)
59 fnames.SetEndIndex(size[2] - 1)
60 fnames.SetIncrementIndex(1)
61 fnames.SetSeriesFormat(fileNameFormat)
62 
63 OutputImageType = itk.Image[OutputPixelType, 2]
64 
65 WriterType = itk.ImageSeriesWriter[RescaleImageType, OutputImageType]
66 writer = WriterType.New()
67 writer.SetInput(rescale.GetOutput())
68 writer.SetFileNames(fnames.GetFileNames())
69 
70 writer.Update()
itk::ImageSeriesWriter
Writes image data to a series of data files.
Definition: itkImageSeriesWriter.h:85
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::NumericSeriesFileNames::New
static Pointer New()
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:133
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88