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
31import os
32import sys
33import InsightToolkit as itk
34import ConnectVTKITKPython as CVIPy
35import vtk
36
37
38reader = vtk.vtkPNGReader()
39reader.SetFileName("../../Testing/Data/Input/cthead1.png")
40
41
42lum = vtk.vtkImageLuminance()
43lum.SetInput(reader.GetOutput())
44
45
46imageCast = vtk.vtkImageCast()
47imageCast.SetOutputScalarTypeToFloat()
48imageCast.SetInput(lum.GetOutput())
49
50
51vtkExporter = vtk.vtkImageExport()
52vtkExporter.SetInput(imageCast.GetOutput())
53
54
55
56itkImporter = itk.itkVTKImageImportF2_New()
57
58
59
60CVIPy.ConnectVTKToITKF2(vtkExporter, itkImporter.GetPointer())
61
62
63canny = itk.itkCannyEdgeDetectionImageFilterF2F2_New()
64rescaler = itk.itkRescaleIntensityImageFilterF2US2_New()
65canny.SetInput(itkImporter.GetOutput())
66rescaler.SetInput(canny.GetOutput())
67rescaler.SetOutputMinimum(0)
68rescaler.SetOutputMaximum(65535)
69
70
71
72def progressEvent():
73 print(f"{canny.GetProgress() * 100.0:.0f}{'%'} done...")
74
75
76pc = itk.itkPyCommand_New()
77pc.SetCommandCallable(progressEvent)
78canny.AddObserver(itk.itkProgressEvent(), pc.GetPointer())
79
80
81
82itkExporter = itk.itkVTKImageExportUS2_New()
83itkExporter.SetInput(rescaler.GetOutput())
84
85
86vtkImporter = vtk.vtkImageImport()
87
88
89CVIPy.ConnectITKUS2ToVTK(itkExporter, vtkImporter)
90
91
92writer = vtk.vtkPNGWriter()
93writer.SetFileName("./testout.png")
94writer.SetInput(vtkImporter.GetOutput())
95
96
97
98
99
100
101
102rescaler.Update()
103
104
105writer.Write()
106
107print("\n\nWrote testout.png to current directory.")