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
23import sys
24import SimpleITK as sitk
25
26if 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
31image_1 = sitk.ReadImage(sys.argv[1])
32image_2 = sitk.ReadImage(sys.argv[2])
33
34# Join two N-D Vector images to form an (N+1)-D image
36joined_image = join.Execute(image_1, image_2)
37
38# Extract first three channels of joined image (assuming RGB)
40channel1_image = select.Execute(joined_image, 0, sitk.sitkUInt8)
41channel2_image = select.Execute(joined_image, 1, sitk.sitkUInt8)
42channel3_image = select.Execute(joined_image, 2, sitk.sitkUInt8)
43
44# Recompose image (should be same as joined_image)
46composed_image = compose.Execute(channel1_image, channel2_image, channel3_image)
47
48# Select same subregion using image slicing operator
49sliced_image = composed_image[100:400, 100:400, 0]
50
51# Select same subregion using ExtractImageFilter
53extract.SetSize([300, 300, 0])
54extract.SetIndex([100, 100, 0])
55extracted_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)
61crop.SetLowerBoundaryCropSize([100, 100, 0])
62crop.SetUpperBoundaryCropSize(
63 [composed_image.GetWidth() - 400, composed_image.GetHeight() - 400, 1]
64)
65cropped_image = crop.Execute(composed_image)
ComposeImageFilter combine several scalar images into a multicomponent image.
Decrease the image size by cropping the image by an itk::Size at both the upper and lower bounds of t...
Decrease the image size by cropping the image to the selected region bounds.
Join N-D images into an (N+1)-D image.
Extracts the selected index of the vector that is the input pixel type.
SITKIO_EXPORT Image ReadImage(const PathType &filename, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageFileReader class which is convenient for most image r...