| 1 |
# ========================================================================== |
| 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 |
# https://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 |
# ==========================================================================*/ |
| 18 |
|
| 19 |
import itk |
| 20 |
import sys |
| 21 |
|
| 22 |
itk.auto_progress(2) |
| 23 |
|
| 24 |
PixelType = itk.ctype("float") |
| 25 |
Dimension = 2 |
| 26 |
ImageType = itk.Image[PixelType, Dimension] |
| 27 |
reader = itk.ImageFileReader[ImageType].New() |
| 28 |
reader.SetFileName(sys.argv[1]) |
| 29 |
|
| 30 |
diffusion = itk.GradientAnisotropicDiffusionImageFilter.New(Input=reader.GetOutput()) |
| 31 |
diffusion.SetTimeStep(0.0625) |
| 32 |
diffusion.SetConductanceParameter(9.0) |
| 33 |
diffusion.SetNumberOfIterations(5) |
| 34 |
|
| 35 |
gradient = itk.GradientMagnitudeImageFilter.New(Input=diffusion.GetOutput()) |
| 36 |
|
| 37 |
watershed = itk.WatershedImageFilter.New(Input=gradient.GetOutput()) |
| 38 |
watershed.SetThreshold(0.01) |
| 39 |
watershed.SetLevel(0.2) |
| 40 |
|
| 41 |
# If `cmake` has `-DITK_WRAP_signed_char:BOOL=ON` at build time then the output image type for this |
| 42 |
# use of `RelabelComponentImageFilter` defaults to `itk.Image[itk.SC,2]`, but that does not provide |
| 43 |
# sufficiently many output labels, giving a runtime error for this test. So, we explicitly override |
| 44 |
# the output image type here to use `itk.Image[itk.SS,2]`. |
| 45 |
relabel = itk.RelabelComponentImageFilter[ |
| 46 |
type(watershed.GetOutput()), itk.Image[itk.SS, 2] |
| 47 |
].New(Input=watershed.GetOutput()) |
| 48 |
|
| 49 |
cast = itk.CastImageFilter[ |
| 50 |
relabel.GetOutput().__class__, itk.Image[itk.ctype("unsigned char"), Dimension] |
| 51 |
].New(Input=relabel.GetOutput()) |
| 52 |
|
| 53 |
writer = itk.ImageFileWriter.New(Input=cast.GetOutput()) |
| 54 |
writer.SetFileName(sys.argv[2]) |
| 55 |
writer.Update() |