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
30image_1 = sitk.ReadImage(sys.argv[1])
31image_2 = sitk.ReadImage(sys.argv[2])
32
33# Join two N-D Vector images to form an (N+1)-D image
35joined_image = join.Execute(image_1, image_2)
36
37# Extract first three channels of joined image (assuming RGB)
39select.SetOutputPixelType(sitk.sitkUInt8)
40
41select.SetIndex(0)
42channel1_image = select.Execute(joined_image)
43select.SetIndex(1)
44channel2_image = select.Execute(joined_image)
45select.SetIndex(2)
46channel3_image = select.Execute(joined_image)
47
48# Recompose image (should be same as joined_image)
50composed_image = compose.Execute(channel1_image, channel2_image, channel3_image)
51
52# Select same subregion using image slicing operator
53sliced_image = composed_image[10:40, 10:40, 0]
54
55# Select same subregion using ExtractImageFilter
57extract.SetSize([30, 30, 0])
58extract.SetIndex([10, 10, 0])
59extracted_image = extract.Execute(composed_image)
60
61# Select same sub-region using CropImageFilter (NOTE: CropImageFilter cannot
62# reduce dimensions unlike ExtractImageFilter, so cropped_image is a three
63# dimensional image with depth of 1)
65crop.SetLowerBoundaryCropSize([10, 10, 0])
66crop.SetUpperBoundaryCropSize(
67 [composed_image.GetWidth() - 40, composed_image.GetHeight() - 40, 1]
68)
69cropped_image = crop.Execute(composed_image)
70
71# Print image information to demonstrate variable usage
72print(f"Sliced image size: {sliced_image.GetWidth()}x{sliced_image.GetHeight()}x{sliced_image.GetDepth()}")
73print(f"Extracted image size: {extracted_image.GetWidth()}x{extracted_image.GetHeight()}x{extracted_image.GetDepth()}")
74print(f"Cropped image size: {cropped_image.GetWidth()}x{cropped_image.GetHeight()}x{cropped_image.GetDepth()}")
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...