SimpleITK  1.2.4
ImageGridManipulation/ImageGridManipulation.py
1 #!/usr/bin/env python
2 #=========================================================================
3 #
4 # Copyright Insight Software Consortium
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 from __future__ import print_function
21 
22 import SimpleITK as sitk
23 import sys
24 
25 if len ( sys.argv ) < 3:
26  print( "Usage: " +sys.argv[0]+ " <input-1> <input-2>" )
27  sys.exit ( 1 )
28 
29 # Two vector images of same pixel type and dimension expected
30 image_1 = sitk.ReadImage(sys.argv[1])
31 image_2 = sitk.ReadImage(sys.argv[2])
32 
33 # Join two N-D Vector images to form an (N+1)-D image
35 joined_image = join.Execute(image_1, image_2)
36 
37 # Extract first three channels of joined image (assuming RGB)
39 channel1_image = select.Execute(joined_image, 0, sitk.sitkUInt8)
40 channel2_image = select.Execute(joined_image, 1, sitk.sitkUInt8)
41 channel3_image = select.Execute(joined_image, 2, sitk.sitkUInt8)
42 
43 # Recompose image (should be same as joined_image)
44 compose = sitk.ComposeImageFilter()
45 composed_image = compose.Execute(channel1_image, channel2_image, channel3_image)
46 
47 # Select same subregion using image slicing operator
48 sliced_image = composed_image[100:400, 100:400, 0]
49 
50 # Select same subregion using ExtractImageFilter
51 extract = sitk.ExtractImageFilter()
52 extract.SetSize([300, 300, 0])
53 extract.SetIndex([100, 100, 0])
54 extracted_image = extract.Execute(composed_image)
55 
56 # Select same subregion using CropImageFilter (NOTE: CropImageFilter cannot reduce dimensions
57 # unlike ExtractImageFilter, so cropped_image is a three dimensional image with depth of 1)
58 crop = sitk.CropImageFilter()
59 crop.SetLowerBoundaryCropSize([100, 100, 0])
60 crop.SetUpperBoundaryCropSize([composed_image.GetWidth()-400, composed_image.GetHeight()-400, 1])
61 cropped_image = crop.Execute(composed_image)