From 473c1c27c09fb3ce87016e877d3194999955cf16 Mon Sep 17 00:00:00 2001 From: Kian Weimer Date: Sun, 6 Jun 2021 11:15:51 -0500 Subject: [PATCH] ENH: Added ApplyKernelToEveryPixel Python script --- .../ApplyKernelToEveryPixel/CMakeLists.txt | 5 ++ .../ApplyKernelToEveryPixel/Code.py | 64 +++++++++++++++++++ .../ApplyKernelToEveryPixel/Documentation.rst | 7 ++ 3 files changed, 76 insertions(+) create mode 100644 src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Code.py diff --git a/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/CMakeLists.txt b/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/CMakeLists.txt index 0db24d44d..d953f8c7b 100644 --- a/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/CMakeLists.txt +++ b/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/CMakeLists.txt @@ -23,3 +23,8 @@ install(FILES Code.cxx CMakeLists.txt enable_testing() add_test(NAME ApplyKernelToEveryPixelTest COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ApplyKernelToEveryPixel) + +if( ITK_WRAP_PYTHON ) + add_test( NAME ApplyKernelToEveryPixelTestPython + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py) +endif() diff --git a/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Code.py b/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Code.py new file mode 100644 index 000000000..dedab5b6c --- /dev/null +++ b/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Code.py @@ -0,0 +1,64 @@ +#!/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 sys +import itk + +dimension = 2 +float_image_type = itk.Image[itk.F, dimension] +unsigned_char_image_type = itk.Image[itk.UC, dimension] + +start = itk.Index[dimension]() +start.Fill(0) + +size = itk.Size[dimension]() +size.Fill(100) + +region = itk.ImageRegion[dimension]() +region.SetIndex(start) +region.SetSize(size) + +image = float_image_type.New(Regions=region) +image.Allocate() +image.FillBuffer(0) + +image[20:80, 20:80] = 15 + +input_image = itk.rescale_intensity_image_filter( + image, + output_minimum=0, + output_maximum=255, + ttype=(float_image_type, unsigned_char_image_type), +) + +itk.imwrite(input_image, "inputPython.png") + +sobel_operator = itk.SobelOperator[itk.F, dimension]() +radius = itk.Size[dimension]() +radius.Fill(1) # a radius of 1x1 creates a 3x3 operator +sobel_operator.SetDirection(0) # Create the operator for the X axis derivative +sobel_operator.CreateToRadius(radius) + +image = itk.neighborhood_operator_image_filter(image, operator=sobel_operator) + +output_image = itk.rescale_intensity_image_filter( + image, + output_minimum=0, + output_maximum=255, + ttype=(float_image_type, unsigned_char_image_type), +) + +itk.imwrite(output_image, "outputPython.png") diff --git a/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Documentation.rst b/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Documentation.rst index 5cf69acce..0797b8bd4 100644 --- a/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Documentation.rst +++ b/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Documentation.rst @@ -30,6 +30,13 @@ Results Code ---- +Python +...... + +.. literalinclude:: Code.py + :language: python + :lines: 1, 16- + C++ ...