Skip to content

Commit 473c1c2

Browse files
kian-weimerdzenanz
authored andcommitted
ENH: Added ApplyKernelToEveryPixel Python script
1 parent 0c6e1af commit 473c1c2

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ install(FILES Code.cxx CMakeLists.txt
2323
enable_testing()
2424
add_test(NAME ApplyKernelToEveryPixelTest
2525
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ApplyKernelToEveryPixel)
26+
27+
if( ITK_WRAP_PYTHON )
28+
add_test( NAME ApplyKernelToEveryPixelTestPython
29+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py)
30+
endif()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
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+
# http://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+
import sys
18+
import itk
19+
20+
dimension = 2
21+
float_image_type = itk.Image[itk.F, dimension]
22+
unsigned_char_image_type = itk.Image[itk.UC, dimension]
23+
24+
start = itk.Index[dimension]()
25+
start.Fill(0)
26+
27+
size = itk.Size[dimension]()
28+
size.Fill(100)
29+
30+
region = itk.ImageRegion[dimension]()
31+
region.SetIndex(start)
32+
region.SetSize(size)
33+
34+
image = float_image_type.New(Regions=region)
35+
image.Allocate()
36+
image.FillBuffer(0)
37+
38+
image[20:80, 20:80] = 15
39+
40+
input_image = itk.rescale_intensity_image_filter(
41+
image,
42+
output_minimum=0,
43+
output_maximum=255,
44+
ttype=(float_image_type, unsigned_char_image_type),
45+
)
46+
47+
itk.imwrite(input_image, "inputPython.png")
48+
49+
sobel_operator = itk.SobelOperator[itk.F, dimension]()
50+
radius = itk.Size[dimension]()
51+
radius.Fill(1) # a radius of 1x1 creates a 3x3 operator
52+
sobel_operator.SetDirection(0) # Create the operator for the X axis derivative
53+
sobel_operator.CreateToRadius(radius)
54+
55+
image = itk.neighborhood_operator_image_filter(image, operator=sobel_operator)
56+
57+
output_image = itk.rescale_intensity_image_filter(
58+
image,
59+
output_minimum=0,
60+
output_maximum=255,
61+
ttype=(float_image_type, unsigned_char_image_type),
62+
)
63+
64+
itk.imwrite(output_image, "outputPython.png")

src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Documentation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ Results
3030
Code
3131
----
3232

33+
Python
34+
......
35+
36+
.. literalinclude:: Code.py
37+
:language: python
38+
:lines: 1, 16-
39+
3340
C++
3441
...
3542

0 commit comments

Comments
 (0)