If you are familiar with numpy, sliced index then this should be cake for the SimpleITK image. The Python standard slice interface for 1-D object:
Operation
Result
ith item of d, starting index 0
d[i:j]
slice of d from i to j
d[i:j:k]
slice of d from i to j with step k
img = sitk.GaborSource(size=[64]*3, frequency=0.05)
# Why does this produce an error?
myshow(img)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-a990f90669fe> in <module>()
3 # Why does this produce an error?
----> 4 myshow(img)
<ipython-input-4-8b0cddf8dfc0> in myshow(img)
1 def myshow(img):
2 nda = sitk.GetArrayFromImage(img)
----> 3 imshow(nda)
4 myshow(img)
/Users/blowekamp/.virtualenvs/sitkpy/lib/python2.7/site-packages/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
2890 vmax=vmax, origin=origin, extent=extent, shape=shape,
2891 filternorm=filternorm, filterrad=filterrad,
-> 2892 imlim=imlim, resample=resample, url=url, **kwargs)
2893 draw_if_interactive()
2894 finally:
/Users/blowekamp/.virtualenvs/sitkpy/lib/python2.7/site-packages/matplotlib/axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
7298 filterrad=filterrad, resample=resample, **kwargs)
-> 7300 im.set_data(X)
7301 im.set_alpha(alpha)
7302 self._set_artist_props(im)
/Users/blowekamp/.virtualenvs/sitkpy/lib/python2.7/site-packages/matplotlib/image.pyc in set_data(self, A)
427 if (self._A.ndim not in (2, 3) or
428 (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 429 raise TypeError("Invalid dimensions for image data")
431 self._imcache = None
TypeError: Invalid dimensions for image data
Mathematical Operators
Most python mathematical operators are overloaded to call the SimpleITK filter which does that same operation on a per-pixel basis. They can operate on a two images or an image and a scalar.
If two images are used then both must have the same pixel type. The output image type is ussually the same.
As these operators basically call ITK filter, which just use raw C++ operators, care must be taked to prevent overflow, and divide by zero etc.
Operators
img = sitk.ReadImage("Data/cthead1.png")
img = sitk.Cast(img,sitk.sitkFloat32)
myshow(img)
img[150,150]
Division Operators
All three Python division operators are implemented __floordiv__
, __truediv__
, and __div__
.
The true division's output is a double pixle type.
See PEP 238 to see why Python changed the division operator in Python 3.
Bitwise Logic Operators
Operators