From 62bf45c6ecbc689e6187a171e5223603d591c44c Mon Sep 17 00:00:00 2001 From: Roman Fenioux Date: Fri, 20 May 2022 15:33:43 +0200 Subject: [PATCH 1/3] ENH: add SmoothImageWithDiscreteGaussianFilter python example --- .../Code.py | 44 +++++++++++++++++++ .../Documentation.rst | 7 +++ 2 files changed, 51 insertions(+) create mode 100755 src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py diff --git a/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py new file mode 100755 index 000000000..38637040f --- /dev/null +++ b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +# Copyright NumFOCUS +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0.txt +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import itk +import argparse + +parser = argparse.ArgumentParser( + description="Smoothing Image With Discrete Gaussian Filter." +) +parser.add_argument("input_image") +parser.add_argument("output_image") +parser.add_argument("sigma", type=float) +args = parser.parse_args() + +PixelType = itk.UC +Dimension = 2 + +ImageType = itk.Image[PixelType, Dimension] + +reader = itk.ImageFileReader[ImageType].New() +reader.SetFileName(args.input_image) + +smoother = itk.DiscreteGaussianImageFilter[ImageType, ImageType].New() +smoother.SetInput(reader.GetOutput()) +smoother.SetSigma(args.sigma) + +writer = itk.ImageFileWriter[ImageType].New() +writer.SetFileName(args.output_image) +writer.SetInput(smoother.GetOutput()) + +writer.Update() diff --git a/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Documentation.rst b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Documentation.rst index 8ed28e370..185094516 100644 --- a/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Documentation.rst +++ b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Documentation.rst @@ -28,6 +28,13 @@ Results Code ---- +Python +...... + +.. literalinclude:: Code.py + :language: python + :lines: 1, 16- + C++ ... From e8947d73f55cccdaa7088ab5699eda210cbcc36f Mon Sep 17 00:00:00 2001 From: Roman Fenioux Date: Fri, 20 May 2022 16:29:05 +0200 Subject: [PATCH 2/3] STYLE: use pythonic filters --- .../Code.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py index 38637040f..e3f5b5cfc 100755 --- a/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py +++ b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/Code.py @@ -25,20 +25,8 @@ parser.add_argument("sigma", type=float) args = parser.parse_args() -PixelType = itk.UC -Dimension = 2 +image = itk.imread(args.input_image) -ImageType = itk.Image[PixelType, Dimension] +smooth_image = itk.discrete_gaussian_image_filter(image, sigma=args.sigma) -reader = itk.ImageFileReader[ImageType].New() -reader.SetFileName(args.input_image) - -smoother = itk.DiscreteGaussianImageFilter[ImageType, ImageType].New() -smoother.SetInput(reader.GetOutput()) -smoother.SetSigma(args.sigma) - -writer = itk.ImageFileWriter[ImageType].New() -writer.SetFileName(args.output_image) -writer.SetInput(smoother.GetOutput()) - -writer.Update() +itk.imwrite(smooth_image, args.output_image) From afa5c631fc61f48e50c5cca6bf83a97c3feb1558 Mon Sep 17 00:00:00 2001 From: Roman Fenioux Date: Fri, 20 May 2022 18:44:54 +0200 Subject: [PATCH 3/3] ENH: add image for test --- .../SmoothImageWithDiscreteGaussianFilterPython.png.sha512 | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/SmoothImageWithDiscreteGaussianFilterPython.png.sha512 diff --git a/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/SmoothImageWithDiscreteGaussianFilterPython.png.sha512 b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/SmoothImageWithDiscreteGaussianFilterPython.png.sha512 new file mode 100644 index 000000000..e635ffd95 --- /dev/null +++ b/src/Filtering/Smoothing/SmoothImageWithDiscreteGaussianFilter/SmoothImageWithDiscreteGaussianFilterPython.png.sha512 @@ -0,0 +1 @@ +14b645d89824bf4c084f1e9daa88b4049b5ff984d2ce900e9208b442d71c4181a834699e94bd58b78a2ecb45e56e65b863dbea057b744f7aef5550b7a0bf3c8b