ITK  5.2.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 # http://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 
20 from distutils.version import StrictVersion as VS
21 if VS(itk.Version.GetITKVersion()) < VS("4.8.0"):
22  print("ITK 4.8.0 is required (see example documentation).")
23  sys.exit(1)
24 
25 if len(sys.argv) != 3:
26  print("Usage: " + sys.argv[0] + " <inputImage> <outputImage>")
27  sys.exit(1)
28 
29 inputImage = sys.argv[1]
30 outputImage = sys.argv[2]
31 PixelType = itk.UC
32 Dimension = 2
33 
34 ImageType = itk.Image[PixelType, Dimension]
35 
36 RGBPixelType = itk.RGBPixel[PixelType]
37 RGBImageType = itk.Image[RGBPixelType, Dimension]
38 
39 ReaderType = itk.ImageFileReader[ImageType]
40 reader = ReaderType.New()
41 reader.SetFileName(inputImage)
42 
43 ColormapType = itk.CustomColormapFunction[PixelType, RGBPixelType]
44 colormap = ColormapType.New()
45 
46 random = itk.MersenneTwisterRandomVariateGenerator.New()
47 random.SetSeed(0)
48 
49 redChannel = []
50 greenChannel = []
51 blueChannel = []
52 
53 for i in range(255):
54  redChannel.append(random.GetUniformVariate(0.0, 1.0))
55  greenChannel.append(random.GetUniformVariate(0.0, 1.0))
56  blueChannel.append(random.GetUniformVariate(0.0, 1.0))
57 
58 colormap.SetRedChannel(redChannel)
59 colormap.SetGreenChannel(greenChannel)
60 colormap.SetBlueChannel(blueChannel)
61 
62 ColormapFilterType = itk.ScalarToRGBColormapImageFilter[
63  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(outputImage)
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:87
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86