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 
21 import SimpleITK as sitk
22 import sys
23 
24 if len(sys.argv) < 3:
25  print("Usage: " + sys.argv[0] + " <input-1> <input-2>")
26  sys.exit(1)
27 
28 # Two vector images of same pixel type and dimension expected
29 image_1 = sitk.ReadImage(sys.argv[1])
30 image_2 = sitk.ReadImage(sys.argv[2])
31 
32 # Join two N-D Vector images to form an (N+1)-D image
34 joined_image = join.Execute(image_1, image_2)
35 
36 # Extract first three channels of joined image (assuming RGB)
38 channel1_image = select.Execute(joined_image, 0, sitk.sitkUInt8)
39 channel2_image = select.Execute(joined_image, 1, sitk.sitkUInt8)
40 channel3_image = select.Execute(joined_image, 2, sitk.sitkUInt8)
41 
42 # Recompose image (should be same as joined_image)
43 compose = sitk.ComposeImageFilter()
44 composed_image = compose.Execute(
45  channel1_image, channel2_image, channel3_image
46 )
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::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< std::string > &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::ExtractImageFilter
Decrease the image size by cropping the image to the selected region bounds.
Definition: sitkExtractImageFilter.h:71
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::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