ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageFusion/OverlayLabelMapOnTopOfAnImage/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 if len(sys.argv) != 4:
21  print("Usage: " + sys.argv[0] +
22  " <InputFileName> <LabelMap> <OutputFileName>")
23  sys.exit(1)
24 
25 inputFileName = sys.argv[1]
26 labelFileName = sys.argv[2]
27 outputFileName = sys.argv[3]
28 
29 PixelType = itk.ctype('unsigned char')
30 Dimension = 2
31 
32 ImageType = itk.Image[PixelType, Dimension]
33 
34 
35 reader = itk.ImageFileReader[ImageType].New()
36 reader.SetFileName(inputFileName)
37 
38 labelReader = itk.ImageFileReader[ImageType].New()
39 labelReader.SetFileName(labelFileName)
40 
41 
42 LabelType = itk.ctype('unsigned long')
43 LabelObjectType = itk.StatisticsLabelObject[LabelType, Dimension]
44 LabelMapType = itk.LabelMap[LabelObjectType]
45 
46 converter = itk.LabelImageToLabelMapFilter[ImageType, LabelMapType].New()
47 converter.SetInput(labelReader)
48 
49 RGBImageType = itk.Image[itk.RGBPixel[PixelType], Dimension]
50 overlayFilter = itk.LabelMapOverlayImageFilter[LabelMapType, ImageType, RGBImageType].New()
51 overlayFilter.SetInput(converter.GetOutput())
52 overlayFilter.SetFeatureImage(reader.GetOutput())
53 overlayFilter.SetOpacity(0.5)
54 
55 
56 itk.imwrite(overlayFilter.GetOutput(), outputFileName)
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::LabelImageToLabelMapFilter
convert a labeled image to a label collection image
Definition: itkLabelImageToLabelMapFilter.h:49
itk::StatisticsLabelObject
A Label object to store the common attributes related to the statistics of the object.
Definition: itkStatisticsLabelObject.h:41
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::LabelMapOverlayImageFilter
Apply a colormap to a label map and superimpose it on an image.
Definition: itkLabelMapOverlayImageFilter.h:59