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
import stackview
image = imread("data/zebrafish_eye.tif")
Before we can crop an image, we may want to know its precise shape (dimensions):
image.shape
(1024, 1024)
This is how we can visualize an image. The example image showing nuclei staining of cells in a zebrafish eye is a courtesy from Mauricio Rocha Martins, Norden lab, MPI CBG Dresden.
stackview.insight(image)
|
|
|
To get a feeling how to crop an image, we start with an interactive interface for cropping. Obviously, x goes from left to right and y is along the up-down axis.
stackview.crop(image, zoom_factor=0.5)
To crop an image programmatically, you need to define the y- and the x-range from where the image should be cropped. After cropping interactively in the interface above, enter the minimum and maximum values of Y and X like this:
ìmage[256:512, 512:768]
First, we start by just cropping one dimension. Note: The first dimensions is Y!
cropped_image1 = image[0:512]
stackview.insight(cropped_image1)
|
|
|
To crop the image in the second dimension as well, we add a , in the square brackets:
cropped_image2 = image[0:512, 512:]
stackview.insight(cropped_image2)
|
|
|
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[::10, ::10]
stackview.insight(sampled_image)
|
|
|
Flipping images#
Negative step sizes flip the image.
flipped_image = image[::, ::-1]
stackview.insight(flipped_image)
|
|
|
Exercise#
Crop out the center part of the zebrafish eye, its lens, and visualize it.
Exercise#
Open the data/banana/banana020.tif data set and crop out the region where the banana slice is located.