SimpleITK  
ImageGridManipulation/ImageGridManipulation.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # =========================================================================
19 
20 """ A SimpleITK example demonstrating various image grid manipulation
21  techniques. """
22 
23 import sys
24 import SimpleITK as sitk
25 
26 if len(sys.argv) < 3:
27  print("Usage: " + sys.argv[0] + " <input-1> <input-2>")
28  sys.exit(1)
29 
30 # Two vector images of same pixel type and dimension expected
31 image_1 = sitk.ReadImage(sys.argv[1])
32 image_2 = sitk.ReadImage(sys.argv[2])
33 
34 # Join two N-D Vector images to form an (N+1)-D image
36 joined_image = join.Execute(image_1, image_2)
37 
38 # Extract first three channels of joined image (assuming RGB)
40 channel1_image = select.Execute(joined_image, 0, sitk.sitkUInt8)
41 channel2_image = select.Execute(joined_image, 1, sitk.sitkUInt8)
42 channel3_image = select.Execute(joined_image, 2, sitk.sitkUInt8)
43 
44 # Recompose image (should be same as joined_image)
45 compose = sitk.ComposeImageFilter()
46 composed_image = compose.Execute(channel1_image, channel2_image, channel3_image)
47 
48 # Select same subregion using image slicing operator
49 sliced_image = composed_image[100:400, 100:400, 0]
50 
51 # Select same subregion using ExtractImageFilter
52 extract = sitk.ExtractImageFilter()
53 extract.SetSize([300, 300, 0])
54 extract.SetIndex([100, 100, 0])
55 extracted_image = extract.Execute(composed_image)
56 
57 # Select same sub-region using CropImageFilter (NOTE: CropImageFilter cannot
58 # reduce dimensions unlike ExtractImageFilter, so cropped_image is a three
59 # dimensional image with depth of 1)
60 crop = sitk.CropImageFilter()
61 crop.SetLowerBoundaryCropSize([100, 100, 0])
62 crop.SetUpperBoundaryCropSize(
63  [composed_image.GetWidth() - 400, composed_image.GetHeight() - 400, 1]
64 )
65 cropped_image = crop.Execute(composed_image)
itk::simple::JoinSeriesImageFilter
Join N-D images into an (N+1)-D image.
Definition: sitkJoinSeriesImageFilter.h:47
itk::simple::ExtractImageFilter
Decrease the image size by cropping the image to the selected region bounds.
Definition: sitkExtractImageFilter.h:72
itk::simple::ComposeImageFilter
ComposeImageFilter combine several scalar images into a multicomponent image.
Definition: sitkComposeImageFilter.h:63
itk::simple::VectorIndexSelectionCastImageFilter
Extracts the selected index of the vector that is the input pixel type.
Definition: sitkVectorIndexSelectionCastImageFilter.h:46
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< PathType > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...
itk::simple::CropImageFilter
Decrease the image size by cropping the image by an itk::Size at both the upper and lower bounds of t...
Definition: sitkCropImageFilter.h:43