File: TestHybridMedian2D.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,992 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 185; javascript: 165; objc: 153; tcl: 59
file content (94 lines) | stat: -rwxr-xr-x 3,136 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
from vtkmodules.vtkImagingCore import vtkImageThreshold
from vtkmodules.vtkImagingGeneral import (
    vtkImageHybridMedian2D,
    vtkImageMedian3D,
)
from vtkmodules.vtkImagingMath import vtkImageMathematics
from vtkmodules.vtkImagingSources import (
    vtkImageCanvasSource2D,
    vtkImageNoiseSource,
)
from vtkmodules.vtkInteractionImage import vtkImageViewer
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Show the constant kernel.  Smooth an impulse function.
imageCanvas = vtkImageCanvasSource2D()
imageCanvas.SetScalarTypeToDouble()
imageCanvas.SetExtent(1, 256, 1, 256, 0, 0)
# back ground zero
imageCanvas.SetDrawColor(0)
imageCanvas.FillBox(1, 256, 1, 256)
imageCanvas.SetDrawColor(255)
imageCanvas.FillBox(30, 225, 30, 225)
imageCanvas.SetDrawColor(0)
imageCanvas.FillBox(60, 195, 60, 195)
imageCanvas.SetDrawColor(255)
imageCanvas.FillTube(100, 100, 154, 154, 40.0)
imageCanvas.SetDrawColor(0)
imageCanvas.DrawSegment(45, 45, 45, 210)
imageCanvas.DrawSegment(45, 210, 210, 210)
imageCanvas.DrawSegment(210, 210, 210, 45)
imageCanvas.DrawSegment(210, 45, 45, 45)
imageCanvas.DrawSegment(100, 150, 150, 100)
imageCanvas.DrawSegment(110, 160, 160, 110)
imageCanvas.DrawSegment(90, 140, 140, 90)
imageCanvas.DrawSegment(120, 170, 170, 120)
imageCanvas.DrawSegment(80, 130, 130, 80)
imageCanvas.Update()

shotNoiseAmplitude = 255.0
shotNoiseFraction = 0.1

# set shotNoiseExtent "1 256 1 256 0 0"
shotNoiseSource = vtkImageNoiseSource()
shotNoiseSource.SetWholeExtent(1, 256, 1, 256, 0, 0)
# $shotNoiseExtent
shotNoiseSource.SetMinimum(0.0)
shotNoiseSource.SetMaximum(1.0)
shotNoiseSource.ReleaseDataFlagOff()

shotNoiseThresh1 = vtkImageThreshold()
shotNoiseThresh1.SetInputConnection(shotNoiseSource.GetOutputPort())
shotNoiseThresh1.ThresholdByLower(1.0 - shotNoiseFraction)
shotNoiseThresh1.SetInValue(0)
shotNoiseThresh1.SetOutValue(shotNoiseAmplitude)
shotNoiseThresh1.Update()

shotNoiseThresh2 = vtkImageThreshold()
shotNoiseThresh2.SetInputConnection(shotNoiseSource.GetOutputPort())
shotNoiseThresh2.ThresholdByLower(shotNoiseFraction)
shotNoiseThresh2.SetInValue(-shotNoiseAmplitude)
shotNoiseThresh2.SetOutValue(0.0)
shotNoiseThresh2.Update()

shotNoise = vtkImageMathematics()
shotNoise.SetInput1Data(shotNoiseThresh1.GetOutput())
shotNoise.SetInput2Data(shotNoiseThresh2.GetOutput())
shotNoise.SetOperationToAdd()
shotNoise.Update()

add = vtkImageMathematics()
add.SetInput1Data(shotNoise.GetOutput())
add.SetInput2Data(imageCanvas.GetOutput())
add.SetOperationToAdd()

median = vtkImageMedian3D()
median.SetInputConnection(add.GetOutputPort())
median.SetKernelSize(3, 3, 1)

hybrid1 = vtkImageHybridMedian2D()
hybrid1.SetInputConnection(add.GetOutputPort())
hybrid2 = vtkImageHybridMedian2D()
hybrid2.SetInputConnection(hybrid1.GetOutputPort())

viewer = vtkImageViewer()
viewer.SetInputConnection(hybrid1.GetOutputPort())
viewer.SetColorWindow(256)
viewer.SetColorLevel(127.5)
viewer.GetRenderWindow().SetSize(256, 256)
viewer.Render()