Cropping images#
When working with microscopy images, it often makes limited sense to process the whole image. We typically crop out interesting regions and process them in detail.
from skimage.io import imread, imshow
#from microfilm.microplot import microshow
image = imread("data/blobs.tif")
Before we can crop an image, we may want to know its precise shape (dimensions):
image.shape
(254, 256)
Recap: Visualization using imshow
:
imshow(image);
![../_images/7f09ea48a7882e44d9111b5580aa7628aab84628936bbb0fd025c565cb75a27f.png](../_images/7f09ea48a7882e44d9111b5580aa7628aab84628936bbb0fd025c565cb75a27f.png)
Cropping images works exactly like cropping lists and tuples, by using indices to specify the range of elements to use:
cropped_image1 = image[0:128]
imshow(cropped_image1);
![../_images/e2b7ebe3bd44422822ee4dcf0ec734131f59a6bfddcc227c517b0531f22d7c95.png](../_images/e2b7ebe3bd44422822ee4dcf0ec734131f59a6bfddcc227c517b0531f22d7c95.png)
To crop the image in the second dimension as well, we add a ,
in the square brackets:
cropped_image2 = image[0:128, 128:]
imshow(cropped_image2);
![../_images/8a62f704a062280113f533864195fd214a1f153eb83ca529ff25e64d62ddac6e.png](../_images/8a62f704a062280113f533864195fd214a1f153eb83ca529ff25e64d62ddac6e.png)
Sub-sampling images#
Also step sizes can be specified as if we would process lists and tuples. Technically, we are sub-sampling the image in this case. We sample a subset of the original pixels for example in steps of 5:
sampled_image = image[::5, ::5]
imshow(sampled_image);
![../_images/388174550364828d133e44ca6c290b52fa0bcd94faf8b9cd6ee2f186232756b5.png](../_images/388174550364828d133e44ca6c290b52fa0bcd94faf8b9cd6ee2f186232756b5.png)
Flipping images#
Negative step sizes flip the image.
flipped_image = image[::, ::-1]
imshow(flipped_image);
![../_images/7e1cc878a73e70f355ebef6395c2844343fa618027daf2483956a4bfb26da5ba.png](../_images/7e1cc878a73e70f355ebef6395c2844343fa618027daf2483956a4bfb26da5ba.png)
Exercise#
Open the banana020.tif
data set and crop out the region where the banana slice is located.