ITK  4.13.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Filtering/Colormap/CreateACustomColorMap/Code.py
1 #!/usr/bin/env python
2 
3 #==========================================================================
4 #
5 # Copyright Insight Software Consortium
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0.txt
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 #==========================================================================*/
20 
21 import sys
22 import itk
23 
24 from distutils.version import StrictVersion as VS
25 if VS(itk.Version.GetITKVersion()) < VS("4.8.0"):
26  print("ITK 4.8.0 is required (see example documentation).")
27  sys.exit(1)
28 
29 if len(sys.argv) != 3:
30  print("Usage: " + sys.argv[0] + " <inputImage> <outputImage>")
31  sys.exit(1)
32 
33 inputImage = sys.argv[1]
34 outputImage = sys.argv[2]
35 PixelType = itk.UC
36 Dimension = 2
37 
38 ImageType = itk.Image[PixelType, Dimension]
39 
40 RGBPixelType = itk.RGBPixel[PixelType]
41 RGBImageType = itk.Image[RGBPixelType, Dimension]
42 
43 ReaderType = itk.ImageFileReader[ImageType]
44 reader = ReaderType.New()
45 reader.SetFileName(inputImage)
46 
47 ColormapType = itk.CustomColormapFunction[PixelType, RGBPixelType]
48 colormap = ColormapType.New()
49 
50 random = itk.MersenneTwisterRandomVariateGenerator.New()
51 random.SetSeed(0)
52 
53 redChannel = []
54 greenChannel = []
55 blueChannel = []
56 
57 for i in range(255):
58  redChannel.append(random.GetUniformVariate(0.0, 1.0))
59  greenChannel.append(random.GetUniformVariate(0.0, 1.0))
60  blueChannel.append(random.GetUniformVariate(0.0, 1.0))
61 
62 colormap.SetRedChannel(redChannel)
63 colormap.SetGreenChannel(greenChannel)
64 colormap.SetBlueChannel(blueChannel)
65 
66 ColormapFilterType = itk.ScalarToRGBColormapImageFilter[
67  ImageType, RGBImageType]
68 colormapFilter1 = ColormapFilterType.New()
69 
70 colormapFilter1.SetInput(reader.GetOutput())
71 colormapFilter1.SetColormap(colormap)
72 
73 WriterType = itk.ImageFileWriter[RGBImageType]
74 writer = WriterType.New()
75 writer.SetFileName(outputImage)
76 writer.SetInput(colormapFilter1.GetOutput())
77 
78 writer.Update()