The Image Basics Notebook was straight forward and closely follows ITK's C++ interface (albeit without those cumbersome templates).
Sugar is great it gives your energy to get things done faster! SimpleITK has applied a generous about of syntactic sugar to help get things done faster too.
import SimpleITK as sitk
Let us begin by developing a convenient method for displaying images in our notebooks.
img = sitk.GaussianSource(size=[64]*2)
imshow(sitk.GetArrayFromImage(img))
img = sitk.GaborSource(size=[64]*2, frequency=.03)
imshow(sitk.GetArrayFromImage(img))
def myshow(img):
nda = sitk.GetArrayFromImage(img)
imshow(nda)
myshow(img)
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 |
d[i] | 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 |
With this convient syntax many basic tasks can be easily done.
img[24,24]
myshow(img[16:48,:])
myshow(img[:,16:-16])
myshow(img[:32,:32])
img_corner = img[:32,:32]
myshow(img_corner)
myshow(img_corner[::-1,:])
myshow(sitk.Tile(img_corner, img_corner[::-1,::],img_corner[::,::-1],img_corner[::-1,::-1], [2,2]))
A 2D image can be extracted from a 3D one.
img = sitk.GaborSource(size=[64]*3, frequency=0.05)
# Why does this produce an error?
myshow(img)
myshow(img[:,:,32])
myshow(img[16,:,:])
myshow(img[:,::3,32])
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 |
+ |
- |
* |
/ |
// |
** |
Operators |
& |
| |
^ |
~ |
Operators |
> |
>= |
< |
<= |
== |