Slicing and cropping#
When working with larger image data, it often makes sense to crop out regions and focus on them for further analysis. For cropping images, we use the same “:”-syntax, we used when indexing in lists and exploring multi-dimensional image data.
import numpy as np
from skimage.io import imread, imshow
We start by loading a 3D image and printing its size.
image = imread("../../data/Haase_MRT_tfl3d1.tif")
image.shape
(192, 256, 256)
Slicing#
For visualizing 3D images using scikit-image’s imshow
, we need to select a slice to visualize. For example, a Z-slice:
slice_image = image[100]
imshow(slice_image)
<matplotlib.image.AxesImage at 0x2b54f73d340>
![../_images/b9ec53895c9942461dd35e1b88b95a9557441ceb09c074bd865d8bb6ddfdb74b.png](../_images/b9ec53895c9942461dd35e1b88b95a9557441ceb09c074bd865d8bb6ddfdb74b.png)
We can also select a plane where all pixels have the same Y-position. We just need to specify, that we would like to keep all pixels in Z using the :
syntax.
slice_image = image[:, 100]
imshow(slice_image)
<matplotlib.image.AxesImage at 0x2b54f836af0>
![../_images/ad292cbffaab643c3d9273781ac2d7a8cb9aeed56d351d1c201b74c79fcbef31.png](../_images/ad292cbffaab643c3d9273781ac2d7a8cb9aeed56d351d1c201b74c79fcbef31.png)
Cropping#
We can also select a sub-stack using indexing in the square brackets.
sub_stack = image[50:150]
sub_stack.shape
(100, 256, 256)
We can also select a sub-region in X. If we want to keep all pixels along Z and Y (the first two dimensions), we just specify :
to keep all.
sub_region_x = image[:, :, 100:200]
imshow(sub_region_x[100])
<matplotlib.image.AxesImage at 0x2b54f8ae850>
![../_images/38728fe7d44860c0fac1767af8007785db40a1042ac2162ae73b3055cddc2023.png](../_images/38728fe7d44860c0fac1767af8007785db40a1042ac2162ae73b3055cddc2023.png)
For selectinng all pixels in one direction above a given value, we just need to specify the start before :
.
sub_region_y = image[:, 100:]
imshow(sub_region_y[100])
<matplotlib.image.AxesImage at 0x2b54f90ae20>
![../_images/97bd447a765eff73cd22753868fe6c2e3f250a63db6ed468f116ff74bd9615b7.png](../_images/97bd447a765eff73cd22753868fe6c2e3f250a63db6ed468f116ff74bd9615b7.png)
Similarly, we can select all pixels up to a given position.
sub_region_x2 = image[:, :, :50]
imshow(sub_region_x2[100])
<matplotlib.image.AxesImage at 0x2b550943d30>
![../_images/b1b6f01794a399ac2db4297973bb8e34233be52f6b5d98b70d683d3a5cd5f8df.png](../_images/b1b6f01794a399ac2db4297973bb8e34233be52f6b5d98b70d683d3a5cd5f8df.png)
Last but not least, this is how a cropped cube is specified.
cropped_cube = image[80:130, 120:170, :50]
cropped_cube.shape
(50, 50, 50)
imshow(cropped_cube[20])
<matplotlib.image.AxesImage at 0x2b5509a4a00>
![../_images/474a83bfd6b87d735ccfa7e5e30c1fdae98c834ef6ced02f2a06bc9f939c8ef0.png](../_images/474a83bfd6b87d735ccfa7e5e30c1fdae98c834ef6ced02f2a06bc9f939c8ef0.png)
And this is how a maximum-intensity projections of this cropped cube look like.
maximum_intensity_projection_along_z = np.max(cropped_cube, axis=0)
imshow(maximum_intensity_projection_along_z)
<matplotlib.image.AxesImage at 0x2b550a03f40>
![../_images/b60a8d4d3c16d68ffeb90787bbe80d7455732026daee8c574df5a01ff6518f14.png](../_images/b60a8d4d3c16d68ffeb90787bbe80d7455732026daee8c574df5a01ff6518f14.png)
maximum_intensity_projection_along_y = np.max(cropped_cube, axis=1)
imshow(maximum_intensity_projection_along_y)
<matplotlib.image.AxesImage at 0x2b550a5e8e0>
![../_images/6a8879bc26fa9b800fb1edf6cccbd641a2638dd0796f9c417c3015f452f385ce.png](../_images/6a8879bc26fa9b800fb1edf6cccbd641a2638dd0796f9c417c3015f452f385ce.png)