1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#!/usr/bin/env python
from vtkmodules.vtkCommonCore import vtkLookupTable
from vtkmodules.vtkIOImage import (
vtkBMPReader,
vtkTIFFReader,
)
from vtkmodules.vtkImagingCore import (
vtkImageAppendComponents,
vtkImageBlend,
vtkImageMapToColors,
vtkImageShrink3D,
)
from vtkmodules.vtkImagingColor import vtkImageLuminance
from vtkmodules.vtkImagingSources import vtkImageCanvasSource2D
from vtkmodules.vtkRenderingCore import (
vtkActor2D,
vtkImageMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.test.Testing
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
# This script calculates the luminance of an image
renWin = vtkRenderWindow()
renWin.SetSize(512, 256)
# Image pipeline
image1 = vtkTIFFReader()
image1.SetFileName(VTK_DATA_ROOT + "/Data/beach.tif")
# "beach.tif" image contains ORIENTATION tag which is
# ORIENTATION_TOPLEFT (row 0 top, col 0 lhs) type. The TIFF
# reader parses this tag and sets the internal TIFF image
# orientation accordingly. To overwrite this orientation with a vtk
# convention of ORIENTATION_BOTLEFT (row 0 bottom, col 0 lhs ), invoke
# SetOrientationType method with parameter value of 4.
image1.SetOrientationType(4)
image2 = vtkBMPReader()
image2.SetFileName(VTK_DATA_ROOT + "/Data/masonry.bmp")
# shrink the images to a reasonable size
color = vtkImageShrink3D()
color.SetInputConnection(image1.GetOutputPort())
color.SetShrinkFactors(2, 2, 1)
backgroundColor = vtkImageShrink3D()
backgroundColor.SetInputConnection(image2.GetOutputPort())
backgroundColor.SetShrinkFactors(2, 2, 1)
# create a greyscale version
luminance = vtkImageLuminance()
luminance.SetInputConnection(color.GetOutputPort())
backgroundLuminance = vtkImageLuminance()
backgroundLuminance.SetInputConnection(backgroundColor.GetOutputPort())
# create an alpha mask
table = vtkLookupTable()
table.SetTableRange(220, 255)
table.SetValueRange(1, 0)
table.SetSaturationRange(0, 0)
table.Build()
alpha = vtkImageMapToColors()
alpha.SetInputConnection(luminance.GetOutputPort())
alpha.SetLookupTable(table)
alpha.SetOutputFormatToLuminance()
# make luminanceAlpha and colorAlpha versions
luminanceAlpha = vtkImageAppendComponents()
luminanceAlpha.AddInputConnection(luminance.GetOutputPort())
luminanceAlpha.AddInputConnection(alpha.GetOutputPort())
colorAlpha = vtkImageAppendComponents()
colorAlpha.AddInputConnection(color.GetOutputPort())
colorAlpha.AddInputConnection(alpha.GetOutputPort())
# create pseudo alpha values for background
bmask = vtkImageCanvasSource2D()
bmask.SetScalarTypeToUnsignedChar()
bmask.SetNumberOfScalarComponents(1)
bmask.SetExtent(0, 127, 0, 127, 0, 0)
bmask.SetDrawColor(0, 0, 0, 0)
bmask.FillBox(0, 127, 0, 127)
bmask.SetDrawColor(255, 0, 0, 0)
bmask.DrawCircle(64, 64, 40)
bmask.FillPixel(64, 64)
backgroundAlpha = vtkImageAppendComponents()
backgroundAlpha.AddInputConnection(backgroundColor.GetOutputPort())
backgroundAlpha.AddInputConnection(bmask.GetOutputPort())
foregrounds = ["luminance", "luminanceAlpha", "color", "colorAlpha"]
backgrounds = ["backgroundAlpha", "backgroundColor", "backgroundLuminance"]
deltaX = 1.0 / 4.0
deltaY = 1.0 / 3.0
blend = dict()
mapper = dict()
actor = dict()
imager = dict()
for row, bg in enumerate(backgrounds):
for column, fg in enumerate(foregrounds):
blend.update({bg:{fg:vtkImageBlend()}})
blend[bg][fg].AddInputConnection(eval(bg + '.GetOutputPort()'))
blend[bg][fg].SetBlendModeToCompound()
if bg == "backgroundAlpha" or bg == "backgroundColor":
blend[bg][fg].AddInputConnection(eval(fg + '.GetOutputPort()'))
if bg == "backgroundAlpha":
blend[bg][fg].SetCompoundAlpha(True)
blend[bg][fg].SetOpacity(0, 0.5)
blend[bg][fg].SetOpacity(1, 0.5)
else:
blend[bg][fg].SetOpacity(1, 0.8)
elif fg == "luminance" or fg == "luminanceAlpha":
blend[bg][fg].AddInputConnection(eval(fg + '.GetOutputPort()'))
blend[bg][fg].SetOpacity(1, 0.8)
mapper.update({bg:{fg:vtkImageMapper()}})
mapper[bg][fg].SetInputConnection(blend[bg][fg].GetOutputPort())
mapper[bg][fg].SetColorWindow(255)
mapper[bg][fg].SetColorLevel(127.5)
actor.update({bg:{fg:vtkActor2D()}})
actor[bg][fg].SetMapper(mapper[bg][fg])
imager.update({bg:{fg:vtkRenderer()}})
imager[bg][fg].AddViewProp(actor[bg][fg])
imager[bg][fg].SetViewport(column * deltaX, row * deltaY, (column + 1) * deltaX, (row + 1) * deltaY)
imager[bg][fg].SetBackground(0.3, 0.3, 0.3)
renWin.AddRenderer(imager[bg][fg])
column += 1
# render and interact with data
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
renWin.Render()
iren.Start()
|