ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixel/Code.py
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 # 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 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")
itk::SobelOperator
A NeighborhoodOperator for performing a directional Sobel edge-detection operation at a pixel locatio...
Definition: itkSobelOperator.h:98
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:70
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:71
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88