ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/Colormap/CreateACustomColormap/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 sys
18 import itk
19 import argparse
20 
21 from distutils.version import StrictVersion as VS
22 
23 if VS(itk.Version.GetITKVersion()) < VS("4.8.0"):
24  print("ITK 4.8.0 is required (see example documentation).")
25  sys.exit(1)
26 
27 parser = argparse.ArgumentParser(description="Create A Custom Colormap.")
28 parser.add_argument("input_image")
29 parser.add_argument("output_image")
30 args = parser.parse_args()
31 
32 PixelType = itk.UC
33 Dimension = 2
34 
35 ImageType = itk.Image[PixelType, Dimension]
36 
37 RGBPixelType = itk.RGBPixel[PixelType]
38 RGBImageType = itk.Image[RGBPixelType, Dimension]
39 
40 ReaderType = itk.ImageFileReader[ImageType]
41 reader = ReaderType.New()
42 reader.SetFileName(args.input_image)
43 
44 ColormapType = itk.CustomColormapFunction[PixelType, RGBPixelType]
45 colormap = ColormapType.New()
46 
47 random = itk.MersenneTwisterRandomVariateGenerator.New()
48 random.SetSeed(0)
49 
50 redChannel = []
51 greenChannel = []
52 blueChannel = []
53 
54 for i in range(255):
55  redChannel.append(random.GetUniformVariate(0.0, 1.0))
56  greenChannel.append(random.GetUniformVariate(0.0, 1.0))
57  blueChannel.append(random.GetUniformVariate(0.0, 1.0))
58 
59 colormap.SetRedChannel(redChannel)
60 colormap.SetGreenChannel(greenChannel)
61 colormap.SetBlueChannel(blueChannel)
62 
63 ColormapFilterType = itk.ScalarToRGBColormapImageFilter[ImageType, RGBImageType]
64 colormapFilter1 = ColormapFilterType.New()
65 
66 colormapFilter1.SetInput(reader.GetOutput())
67 colormapFilter1.SetColormap(colormap)
68 
69 WriterType = itk.ImageFileWriter[RGBImageType]
70 writer = WriterType.New()
71 writer.SetFileName(args.output_image)
72 writer.SetInput(colormapFilter1.GetOutput())
73 
74 writer.Update()
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::Version::GetITKVersion
static const char * GetITKVersion()
itk::ScalarToRGBColormapImageFilter
Implements pixel-wise intensity->rgb mapping operation on one image.
Definition: itkScalarToRGBColormapImageFilter.h:130
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88