forked from OpenGATE/GateTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_crop.py
198 lines (175 loc) · 6.86 KB
/
image_crop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -----------------------------------------------------------------------------
# Copyright (C): OpenGATE Collaboration
# This software is distributed under the terms
# of the GNU Lesser General Public Licence (LGPL)
# See LICENSE.md for further details
# -----------------------------------------------------------------------------
"""
This module provides a function to crop image
"""
import itk
import gatetools as gt
import numpy as np
import logging
logger=logging.getLogger(__name__)
def image_auto_crop(img, bg=0):
"""
Crop an image according to a background value. Only for integer PixelType
"""
# Img image type
ImageType = type(img)
Dim = 3
dims = np.array(img.GetLargestPossibleRegion().GetSize())
if len(dims) != 3:
logger.error('only 3D image supported')
exit(0)
# check image type: LabelImageToLabelMapFilter only allow unsigned short/char
# for float -> cannot do it
# for int -> cannot do it
# for char/short -> offset if negative value
PixelType = itk.template(img)[1][0]
if PixelType == itk.ctype('float'):
logger.error('Cannot crop for float or double PixelType. Only char/short supported')
exit(0)
if PixelType == itk.ctype('int'):
logger.error('Cannot crop for int PixelType. Only char/short supported')
exit(0)
# special case for negative value
img_array = itk.array_view_from_image(img)
minv = np.min(img_array)
bg = int(bg)
if minv<0:
img_array -= minv
bg -= minv
# force to integer
bg = int(bg)
#print(itk.LabelImageToLabelMapFilter.GetTypes())
#print(itk.StatisticsLabelObject.GetTypes())
# Cast to an image type which is compatible (ushort)
OutputPixelType = itk.ctype('unsigned short')
OutputImageType = itk.Image[OutputPixelType, Dim]
caster = itk.CastImageFilter[ImageType, OutputImageType].New()
caster.SetInput(img)
caster.Update()
img = caster.GetOutput()
TempImageType = type(img)
# create filters for crop
LabelType = itk.ctype('unsigned long')
LabelObjectType = itk.StatisticsLabelObject[LabelType, Dim]
LabelMapType = itk.LabelMap[LabelObjectType]
converter = itk.LabelImageToLabelMapFilter[TempImageType, LabelMapType].New()
converter.SetBackgroundValue(bg);
converter.SetInput(img);
autocrop = itk.AutoCropLabelMapFilter[LabelMapType].New()
autocrop.SetInput(converter.GetOutput())
remap = itk.LabelMapToLabelImageFilter[LabelMapType, TempImageType].New()
remap.SetInput(autocrop.GetOutput())
# recast to initial type
endcaster = itk.CastImageFilter[TempImageType, ImageType].New()
endcaster.SetInput(remap.GetOutput())
# Go !
endcaster.Update()
output = endcaster.GetOutput()
# Offset if negative
if minv<0:
img_array = itk.array_view_from_image(output)
img_array += minv
return output
def image_crop_with_bb(img, bb):
"""
Crop an image according to a bounding box (see "bounding_box" module).
"""
dims = np.array(img.GetLargestPossibleRegion().GetSize())
if len(dims) != 3:
logger.error('only 3D image supported')
exit(0)
#inclusive
from_index = np.maximum(np.zeros(3,dtype=int),np.array(img.TransformPhysicalPointToIndex(bb.mincorner)))
#exclusive
to_index = np.minimum(dims,np.array(img.TransformPhysicalPointToIndex(bb.maxcorner))+1)
cropper = itk.RegionOfInterestImageFilter.New(Input=img)
region = cropper.GetRegionOfInterest()
indx=region.GetIndex()
size=region.GetSize()
for j in range(3):
indx.SetElement(j,int(from_index[j]))
size.SetElement(j,int(to_index[j]-from_index[j]))
region.SetIndex(indx)
region.SetSize(size)
cropper.SetRegionOfInterest(region)
cropper.Update()
return cropper.GetOutput()
#####################################################################################
import unittest
from .logging_conf import LoggedTestCase
class Test_Crop(LoggedTestCase):
def test_auto_crop(self):
Dimension = 3
PixelType = itk.ctype('unsigned char')
ImageType = itk.Image[PixelType, Dimension]
image = ImageType.New()
start = itk.Index[Dimension]()
start[0] = 0
start[1] = 0
start[2] = 0
size = itk.Size[Dimension]()
size[0] = 200
size[1] = 200
size[2] = 200
region = itk.ImageRegion[Dimension]()
region.SetSize(size)
region.SetIndex(start)
image.SetRegions(region)
image.Allocate()
image.FillBuffer(0)
npView = itk.array_from_image(image)
npView[10:52, 42:192, 124:147] =1
image = itk.image_from_array(npView)
autoCrop = image_auto_crop(image)
autoCropSize = autoCrop.GetLargestPossibleRegion().GetSize()
self.assertTrue(np.allclose(autoCropSize[0], 23))
self.assertTrue(np.allclose(autoCropSize[1], 150))
self.assertTrue(np.allclose(autoCropSize[2], 42))
self.assertTrue(np.allclose(itk.array_from_image(autoCrop)[0, 0, 0], 1))
def test_auto_crop_negative(self):
Dimension = 3
PixelType = itk.ctype('short')
ImageType = itk.Image[PixelType, Dimension]
image = ImageType.New()
start = itk.Index[Dimension]()
start[0] = 0
start[1] = 0
start[2] = 0
size = itk.Size[Dimension]()
size[0] = 200
size[1] = 200
size[2] = 200
region = itk.ImageRegion[Dimension]()
region.SetSize(size)
region.SetIndex(start)
image.SetRegions(region)
image.Allocate()
image.FillBuffer(0)
npView = itk.array_from_image(image)
npView[10:52, 42:192, 124:147] =1
npView[110:152, 42:192, 124:147] =-1
image = itk.image_from_array(npView)
autoCrop = image_auto_crop(image)
autoCropSize = autoCrop.GetLargestPossibleRegion().GetSize()
self.assertTrue(np.allclose(autoCropSize[0], 23))
self.assertTrue(np.allclose(autoCropSize[1], 150))
self.assertTrue(np.allclose(autoCropSize[2], 142))
self.assertTrue(np.allclose(itk.array_from_image(autoCrop)[0, 0, 0], 1))
self.assertTrue(np.allclose(itk.array_from_image(autoCrop)[-1, 0, 0], -1))
def test_crop(self):
x = np.arange(-10, 10, 0.1)
y = np.arange(-12, 15, 0.1)
z = np.arange(-13, 10, 0.1)
xx, yy, zz = np.meshgrid(x, y, z)
image = itk.image_from_array(np.float32(xx))
croppedImage = image_crop_with_bb(image, gt.bounding_box(xyz=[10.0, 12.0, 0.0, 7.0, 6.0, 15.0]))
croppedImageSize = croppedImage.GetLargestPossibleRegion().GetSize()
self.assertTrue(np.allclose(croppedImageSize[0], 3))
self.assertTrue(np.allclose(croppedImageSize[1], 8))
self.assertTrue(np.allclose(croppedImageSize[2], 10))
self.assertTrue(np.allclose(itk.array_from_image(croppedImage)[0, 0, 0], -10))